Sub topic 6
etcd: where cluster state lives
This chapter assumes you know what the API server does, can read YAML, and have run basic kubectl commands. It covers what etcd stores, how it keeps data consistent, and the errors you are most likely to meet.
What etcd is
etcd is a distributed key-value store. Kubernetes uses it as the single source of truth for cluster state. Every object you create is saved there: Deployments, Pods, Services, ConfigMaps, Secrets, nodes, and more. If etcd loses its data, the cluster forgets what it was supposed to run.
Only the API server talks to etcd. The scheduler, controllers, and kubelets read and write state through the API server. This keeps validation, authentication, and authorization in one place.
How state is stored
etcd stores objects as keys under a prefix, which is /registry by default. A Pod named web-1 in the default namespace is stored under a key like /registry/pods/default/web-1. The value is the serialized object, usually in Protobuf format.
Each write gets a revision number. Kubernetes exposes this as an object's resourceVersion. Clients use it for optimistic concurrency. An update that carries a stale resourceVersion is rejected with a conflict error. Controllers also use it with watches: they ask the API server to stream every change after a given revision, which is how they react to new state without polling.
Consistency and quorum
etcd uses the Raft consensus algorithm. One member is the leader. A write counts as committed only after a majority (quorum) of members has stored it. For a cluster of N members, quorum is floor(N/2) + 1.
| Members | Quorum | Failures tolerated |
|---|---|---|
| 1 | 1 | 0 |
| 3 | 2 | 1 |
| 5 | 3 | 2 |
Use an odd number of members. Four members tolerate no more failures than three, and they add write overhead.
If quorum is lost, the API server cannot write. Running Pods usually keep running, because kubelets continue to manage containers they already know about. But you cannot create, change, or reschedule anything until quorum returns.
Inspecting etcd
On a kubeadm cluster, etcd runs as a static Pod in kube-system:
kubectl get pods -n kube-system -l component=etcdCommon errors and operational points
- Slow disks. etcd syncs writes to disk. High disk latency causes leader elections and API timeouts. Use fast, dedicated storage.
- Database size limit. The default quota is about 2 GiB. When it is exceeded, etcd raises an alarm and rejects writes. Compaction and defragmentation reclaim space.
- No backups. Take regular snapshots with
etcdctl snapshot save. Test restores, not only saves. - Secrets at rest. Secrets are stored base64-encoded by default, which is not encryption. Enable encryption at rest in the API server configuration.
- Direct access. Do not let other components or users reach etcd directly. Protect it with TLS client certificates and network rules.
Exercise
A cluster has a three-member etcd. Two members go down. What happens to the Pods already running, and what can you no longer do?
Show answer
Quorum (2 of 3) is lost, so the API server cannot write to etcd. Running Pods usually keep running because kubelets manage them locally. You cannot create, update, or reschedule objects until quorum is restored.
Exercise
Why does an update with an old resourceVersion fail, and why is that useful?
Show answer
etcd assigns a new revision on each write. The API server rejects an update based on a stale revision with a conflict. This prevents one client from silently overwriting another client's newer changes.