Control plane
Backing up and restoring etcd
Snapshot, restore, reconcile
Every object in the cluster lives in etcd, and nowhere else. At 02:00 a snapshot of it is taken. At 10:41 someone deletes the shop namespace, and with it the Deployments, Services, Secrets and Pods of the shop app. You restore the 02:00 snapshot. Follow what a snapshot holds, how a restore replaces etcd's data under a stopped API server, why the cluster then sees time go backwards, and what happens to everything that changed in between.
- Snapshot
- 02:00 · revision 2,814,307
- Accident
- 10:41 · kubectl delete namespace shop
- Restore
- etcdutl snapshot restore · a new data directory
- Lost
- every change after 02:00
Read the whole lesson as text
1. What etcd holds
etcd stores each object as one key under /registry: /registry/pods/shop/cart-…, /registry/secrets/shop/db-password, and so on. Every write gets a number, the revision, which is also the resourceVersion clients see. That is all a snapshot captures: the objects. Container images live in registries, volume data on disks, logs on the nodes. And Secrets encrypted at rest are only readable with the API server's encryption configuration and keys, which aren't in etcd and need a backup of their own.
- Every API object is one key in etcd. Writing a Pod writes its key and bumps the revision.
2. Snapshot
At 02:00 a CronJob on cp-1 runs etcdctl snapshot save, with etcd's own CA and a client certificate. etcd streams a copy of its whole database as it stood at one revision, so the snapshot is consistent even while writes continue. etcdutl snapshot status shows its hash, revision, key count and size. A snapshot that stays on cp-1 dies with cp-1, so it's copied off the machine.
- etcdctl asks etcd for a snapshot and writes it to a file.
- etcdutl reads the file and prints what's in it.
- The snapshot is uploaded somewhere that doesn't share cp-1's fate.
3. The accident
At 10:41 someone runs kubectl delete namespace shop on the wrong cluster. The namespace turns Terminating, and the namespace controller in the controller manager deletes everything inside it: Deployments, ReplicaSets, Services, ConfigMaps, Secrets, Pods. The kubelets stop the shop Pods. When nothing is left, the controller removes the namespace's kubernetes finalizer and the namespace itself is gone. Nothing in Kubernetes can undo it.
- The namespace is marked for deletion and turns Terminating.
- The namespace controller deletes every object in shop, kind by kind.
- The kubelets see their shop Pods deleted and stop them.
- With nothing left inside, the controller removes the finalizer and the namespace disappears.
4. Restore
Restoring replaces etcd's data, so nothing may be writing to it. On cp-1 you move the kube-apiserver manifest out of /etc/kubernetes/manifests, and the kubelet stops the API server. etcdutl snapshot restore builds a new data directory from the snapshot, with a new cluster ID. You point the etcd manifest's data volume at that directory, the kubelet restarts etcd on it, and you move the API server manifest back.
- You move the API server's manifest aside. cp-1's kubelet stops it, and the cluster has no API.
- etcdutl writes a fresh data directory from the snapshot.
- You point etcd's manifest at the new directory, and the kubelet restarts etcd on it.
- You move the API server's manifest back. It starts and reads the restored etcd.
5. Time went backwards
Every client that watches the API had seen revisions up to 2,951,822. The restored etcd is at the 02:00 state. Without the extra flags, new writes would reuse revision numbers clients had already seen for other changes, and watches could quietly miss things. With --mark-compacted, those watches end with an error, the API server answers 410 Gone, and each client lists everything again and builds a fresh cache. The docs also recommend restarting the controller manager, the scheduler and the kubelets so none of them acts on stale data.
- node-1's kubelet resumes its watch from the revision it last saw. That revision is now compacted.
- The kubelet lists its Pods from scratch and starts a new watch from the new revision.
- You restart the controller manager and the scheduler, and they take their leader Leases and list everything again.
6. Reconcile
The restored etcd holds the shop namespace and everything in it, including the Pod objects, still bound to their nodes. The kubelets find Pods they should be running and start their containers again. Controllers compare the old desired state with what's on the nodes and settle the rest. Everything that changed after 02:00 is gone from the API: a ConfigMap edited at 09:15 is back to its old value, and a debug Pod created at 08:30 is unknown, so its kubelet stops it.
- node-1's kubelet finds cart-…-9fh2k assigned to it and starts it again.
- node-1 also runs a Pod created at 08:30, after the snapshot. The API has never heard of it, so the kubelet stops it.
- The shop Pods are Running and Ready again. The cluster matches 02:00, plus whatever controllers did since.
7. The other classic: expired certificates
The other way a kubeadm control plane stops answering is time. kubeadm's certificates are valid for one year, the CAs for ten. If nothing renews them, kubectl one day fails with x509: certificate has expired, and the API server can no longer reach etcd or the kubelets. kubeadm upgrade renews them, so clusters upgraded at least once a year never see this. Otherwise: kubeadm certs check-expiration shows what's left, kubeadm certs renew all renews them, then restart the control plane Pods and copy the new admin.conf.
- In a cluster nobody upgraded for a year, kubectl's TLS handshake fails.
- On cp-1, kubeadm shows what has expired and renews it.
Summary
etcd holds the cluster's objects and nothing else, so a snapshot of it is the whole cluster's state at one revision. Restoring means stopping the API server, building a new data directory with etcdutl and starting etcd on it. The hard part is time: every client is ahead of the restored data, so marking revisions compacted makes them all list again, and restarting the control plane drops what they cached. Whatever happened after the snapshot is gone, and a year-old kubeadm cluster has expired certificates to worry about too.
Next lesson: Graceful shutdown