123DevOps··7 min read

What is etcd? The database that holds Kubernetes state

Understand etcd, the distributed key-value store behind Kubernetes: Raft consensus, quorum, revisions and watches, backup and restore, maintenance, disk performance, and security.

Kubernetes

Every kubectl apply ends up in one place: etcd. Every Deployment, Secret, and ConfigMap, and the status of every pod in the cluster, lives there. If etcd loses data, the cluster forgets who it is; if etcd gets slow, the whole cluster gets slow. So even if you never operate it directly, it’s worth understanding what it does and how to take care of it.

What etcd is

etcd is a distributed, strongly consistent key-value store, built to hold small, critical data: configuration, coordination, and the state of distributed systems. It is written in Go, it is a Graduated CNCF project, and it usually runs as a cluster of a few members.

“Strongly consistent” means that once a write is acknowledged, any read (in the default, linearizable mode) sees that value — there is no “maybe the replica hasn’t received it yet.” For an orchestrator, this is essential: two controllers can’t make decisions based on different versions of the truth.

etcd’s role in Kubernetes

etcd is the cluster’s source of truth, and only one component talks to it: the kube-apiserver. The scheduler, the controller manager, kubelets, and kubectl always talk to the API server, which validates, authorizes, and then reads from or writes to etcd. Objects are stored under keys with the /registry/ prefix (for example, /registry/deployments/<namespace>/<name>), serialized as protobuf by default. The full flow is in how Kubernetes works.

This rule has a practical consequence: never edit etcd by hand. Writing to it directly skips validation, admission, and auditing.

Raft in simple terms

For several members to agree on the data, etcd uses the Raft consensus algorithm:

  • One member is elected leader; the others are followers.
  • Every write goes through the leader, which replicates it to the followers.
  • A write is only committed once a majority of members (the quorum) has persisted it to disk.
  • If the leader goes down, the followers notice the missing heartbeats and elect a new leader.

The quorum is floor(n / 2) + 1 — a strict majority of the members. Hence the rule of using an odd number of members:

Members Quorum Failures tolerated
1 1 0
3 2 1
4 3 1
5 3 2
7 4 3

With 4 members, you tolerate the same number of failures as with 3, just with more replication. In production, 3 members is the norm, and 5 is for those who need to survive two simultaneous failures. More members don’t make etcd faster: each write needs more acknowledgments. Without quorum, the cluster stops accepting writes.

Data model: keys, revisions, and watches

  • Keys and values: a flat key space, queried by exact key or by prefix.
  • Revisions (MVCC): every change increments a global revision number. etcd keeps previous versions of keys until they are compacted.
  • Leases: keys can expire along with a lease, which is useful for leader election and temporary registrations.
  • Watches: a client can watch a key or prefix from a given revision and receive every change in order.

Watches are what make Kubernetes reactive. The API server watches etcd and relays the events to its clients; controllers watch the API server and react to each change, instead of polling everything in a loop. That’s how a new ReplicaSet becomes pods within seconds.

Operating etcd

In clusters built with kubeadm, etcd runs as a static pod, and its certificates live in /etc/kubernetes/pki/etcd/. All the commands below use TLS:

export ETCDCTL_ENDPOINTS=https://127.0.0.1:2379
export ETCDCTL_CACERT=/etc/kubernetes/pki/etcd/ca.crt
export ETCDCTL_CERT=/etc/kubernetes/pki/etcd/server.crt
export ETCDCTL_KEY=/etc/kubernetes/pki/etcd/server.key

# health and state of each member (leader, DB size, Raft term)
etcdctl endpoint health --cluster
etcdctl endpoint status --cluster --write-out=table
etcdctl member list --write-out=table

# backup (consistent snapshot from a healthy member)
etcdctl snapshot save /backup/etcd-$(date +%F).db
etcdutl snapshot status /backup/etcd-$(date +%F).db --write-out=table

# alarms (for example, NOSPACE when the quota is exceeded)
etcdctl alarm list

Backup and restore

Take automatic, frequent snapshots, store them off the node (object storage, encrypted), and, above all, test the restore. In newer versions, restoring is done with etcdutl (etcdctl snapshot restore has been deprecated):

etcdutl snapshot restore /backup/etcd-2026-11-24.db \
  --data-dir /var/lib/etcd-restored

Then point etcd at the new directory (with kubeadm, by adjusting the hostPath in the manifest at /etc/kubernetes/manifests/etcd.yaml). In multi-member clusters, each member is restored from the same snapshot, with its own --name, --initial-cluster, and --initial-advertise-peer-urls. Remember: a restore takes the entire cluster back to the moment of the snapshot.

Compaction and defragmentation

The revision history grows without bound. Compaction discards old revisions — in Kubernetes, the API server itself triggers it periodically (--etcd-compaction-interval). But compaction doesn’t give space back to the disk: that’s what defragmentation is for, with etcdctl defrag. It blocks the member while it runs, so do one member at a time, leaving the leader for last.

Disk and storage quota

etcd fsyncs its log (WAL) on every committed write, which makes it very sensitive to disk latency. Use fast, dedicated SSDs, avoid disks shared with heavy workloads, and track metrics such as etcd_disk_wal_fsync_duration_seconds, etcd_disk_backend_commit_duration_seconds, and etcd_server_leader_changes_seen_total. Frequent leader changes usually point to slow disks or networking.

There is also a default storage quota, configurable with --quota-backend-bytes. When it is exceeded, etcd raises the NOSPACE alarm and only accepts reads and deletes — in practice, the cluster stops accepting changes. Compact, defragment, and only then disarm it with etcdctl alarm disarm. Check the default value and the recommended maximum in the documentation for your version.

What about managed Kubernetes?

On EKS, GKE, and AKS, etcd is part of the managed control plane: the provider handles quorum, backups, upgrades, and disks, and you have no direct access to it. Your responsibility becomes not abusing it — for example, by avoiding huge objects (giant ConfigMaps, CRDs writing status every second) and thousands of orphaned resources.

Security

etcd holds every Secret in the cluster, so whoever can read etcd can read everything:

  • TLS between clients and members and among the members themselves (ports 2379 and 2380), with certificate authentication.
  • Restricted access: only the API server should reach the client port; block everything else at the network level.
  • Encrypted backups: a snapshot is a complete copy of your Secrets.
  • Encryption at rest for Secrets, configured on the API server with --encryption-provider-config:
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
  - resources:
      - secrets
    providers:
      # in production, prefer the kms (v2) provider with an external KMS
      - secretbox:
          keys:
            - name: key1
              secret: <base64-encoded-32-byte-key>
      - identity: {}

After enabling it, rewrite the existing Secrets so they get encrypted:

kubectl get secrets --all-namespaces -o json | kubectl replace -f -

Checklist

  • etcd cluster with 3 (or 5) members across different failure zones
  • Automatic snapshots, stored off the node and encrypted
  • Restore tested periodically in a separate environment
  • Alerts for fsync latency, leader changes, and quota alarms
  • Planned defragmentation, one member at a time
  • TLS everywhere and port 2379 access restricted to the API server
  • Encryption at rest for Secrets, preferably with KMS

etcd is quiet when it’s healthy — and the whole cluster screams when it isn’t.

Get updates on new posts and stay in the loop!