Sub topic 2
How etcd stores cluster state
This chapter explains what etcd holds and why that shapes how you back it up. It assumes you can use kubectl, know what a control plane is, and can SSH to a node.
What etcd is
etcd is a distributed key-value store. The Kubernetes API server uses it as its only persistent database. Every object you create, such as a Deployment, a Secret, a ConfigMap or a Node, is serialized and written to etcd. The other control plane components do not talk to etcd. They read and write through the API server.
If etcd is lost and you have no backup, the cluster forgets what it should be running. Your containers may keep running for a while, but you can no longer manage them through the API.
How objects are laid out
etcd keys are paths. Kubernetes places each object under a prefix, /registry, followed by the resource type, the namespace and the name.
| Object | Example key |
|---|---|
| Pod `web` in namespace `shop` | `/registry/pods/shop/web` |
| Deployment `api` in `shop` | `/registry/deployments/shop/api` |
| Namespace `shop` | `/registry/namespaces/shop` |
The value is the object, stored in a binary protobuf encoding by default. You do not edit these keys by hand. The layout matters for backup because a snapshot captures all of them at once, so the restored cluster is consistent.
Revisions and the write-ahead log
etcd keeps a global revision counter. Every change increments it, and each key remembers the revision at which it was created and last modified. The API server uses these revisions for watches and for optimistic concurrency (the resourceVersion field on objects).
Two kinds of files live in the data directory, which is /var/lib/etcd on a kubeadm cluster:
- The write-ahead log (WAL) records every change before it is applied, so etcd can recover after a crash.
- The backend database holds the current state of the keys.
Copying these files from a running etcd can produce an inconsistent copy. Use the snapshot command instead, which you will see in the next chapter.
Where etcd runs on kubeadm
On a kubeadm cluster, etcd runs as a static Pod. The kubelet starts it from a manifest file, not from the API. You can look at it on a control plane node:
sudo cat /etc/kubernetes/manifests/etcd.yaml
sudo ls /etc/kubernetes/pki/etcd/The manifest shows the flags etcd runs with, including --data-dir, --listen-client-urls and the certificate paths. etcd only accepts clients that present a valid certificate, so any backup command needs the CA certificate, a client certificate and its key. These live in /etc/kubernetes/pki/etcd/.
What etcd does not hold
etcd stores the definitions of your objects. It does not store the contents of your persistent volumes. A PersistentVolumeClaim object is in etcd, but the database files on the disk behind it are not. That data needs a separate backup, which Velero handles later in this tutorial.
Common mistakes
- Backing up the data directory with
cportarwhile etcd is running. - Running etcd commands without the TLS flags, which fails with a certificate or connection error.
- Assuming an etcd backup also protects application data in volumes.
- Storing snapshots only on the same node as etcd.
Exercise
A teammate says an etcd snapshot is enough to recover a database that runs in a Pod with a PersistentVolumeClaim. What is wrong with that?
Show answer
The snapshot restores the Pod, PVC and PersistentVolume objects, but not the data in the underlying volume. The database contents need a separate backup, such as a volume snapshot or a file system backup.
Exercise
Why should you take an etcd backup with a snapshot command instead of copying /var/lib/etcd?
Show answer
etcd writes to the WAL and the backend database while it runs, so a file copy can capture them at different points and be inconsistent. A snapshot gives a consistent point-in-time view.