ETCD Backup & Restore
ETCD Backup & Restore
TL;DR
etcdis the single source of truth for cluster state — every Deployment, Secret, RBAC rule, and Service object lives there. Loss ofetcdwith no backup means total cluster state loss, even if workloads keep running briefly on their own.etcdctl snapshot saveproduces a point-in-time backup file;etcdctl snapshot restorenever modifies the running data directory — it always creates a new one that must be pointed at explicitly.- All snapshot operations require mutual TLS certificates (
--cacert,--cert,--key) becauseetcdctltalks toetcdexactly like any other cluster component. - A restore reverts the cluster to the exact state at snapshot time — anything created, changed, or deleted afterward is permanently lost.
- Verifying a snapshot is valid (
snapshot status) is not optional — asavecommand can exit successfully while producing a corrupted or truncated file. - This is one of the most heavily tested CKA topics.
Core Concepts
What etcd Stores and Why Backup Matters
etcd is a distributed, consistent key-value store and the backing database for the entire Kubernetes API — every object (Pods, Deployments, Secrets, ConfigMaps, RBAC bindings, Services) is serialized and stored there. The API server is stateless with respect to cluster data; it reads and writes through to etcd. If etcd’s data is lost with no backup, the cluster’s desired-state record is gone — existing running workloads may continue operating briefly since kubelets keep executing already-scheduled pods, but no reconciliation, scheduling, or API operations can function correctly, and recovery without a backup means rebuilding every object definition from scratch.
Taking a Snapshot
etcdctl snapshot save produces a single file capturing the entire keyspace at that instant. Because etcdctl communicates with the etcd server over mutual TLS, exactly like kube-apiserver does, every snapshot operation requires three certificate flags: --cacert (trusted CA), --cert (client certificate), and --key (client private key). In a stacked-etcd kubeadm cluster these live under /etc/kubernetes/pki/etcd/.
A snapshot file existing on disk does not guarantee it is usable. etcdctl snapshot status <file> --write-out=table independently reads and validates the file’s internal consistency (hash, revision, key count) — this check should run immediately after every snapshot, not just be assumed from the save command’s exit code.
Restoring From a Snapshot
etcdctl snapshot restore never touches the currently running etcd data directory. It always creates a brand-new data directory populated from the snapshot contents. To make etcd actually use the restored data, the static pod manifest’s hostPath volume for the etcd data directory must be edited to point at the new path. Because etcd runs as a static pod (managed directly by the kubelet from a manifest file, not through the API server), saving the edited manifest causes the kubelet to automatically detect the change and recreate the pod — no manual pod deletion is required.
Full Disaster Recovery Sequence
- Stop the API server temporarily, or accept that it will error until
etcdis back — commonly done by moving static pod manifests out of/etc/kubernetes/manifests/momentarily. - Restore the snapshot to a new data directory with
etcdctl snapshot restore. - Update
etcd.yaml’s volume path to point at the new directory. - Move manifests back (or let the kubelet recreate the static pods automatically).
- Verify cluster health:
kubectl get nodes,kubectl get pods -A, and confirm objects created after the snapshot are indeed gone — this is expected, not a bug.
Restore Consequences
A restore reverts the cluster to the exact state captured at snapshot time. Any Deployments, scaling events, Secret rotations, or deletions that happened after the snapshot was taken are completely gone, and the cluster will actively reconcile back toward that older desired state — for example, scaling a Deployment back down if it was scaled up after the snapshot. This makes a restore a recovery to a known-good point-in-time, not a seamless continuation; the data-loss window must be communicated and accounted for operationally.
etcd Snapshot vs Full Cluster Backup
| Aspect | etcd Snapshot | Full Cluster / Velero-style Backup |
|---|---|---|
| Captures | Entire etcd keyspace (all API objects) |
Kubernetes objects (via API) plus optionally PV data |
| Granularity | Whole-cluster, point-in-time only | Can be namespace- or resource-scoped |
| Restore target | New etcd data directory |
Live cluster via API server (apply-based) |
| Requires cluster downtime to restore | Effectively yes (control plane briefly unavailable) | No — restores are typically applied while cluster is live |
| Captures persistent volume data | No | Yes, with volume snapshot integration |
| Primary use case | Full disaster recovery, control-plane loss | Selective recovery, migration, namespace-level restore |
Command / Configuration Reference
Take a snapshot:
ETCDCTL_API=3 etcdctl snapshot save /opt/backups/etcd-snapshot-$(date +%Y%m%d-%H%M).db \
--endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key
Verify the snapshot is valid:
ETCDCTL_API=3 etcdctl snapshot status /opt/backups/etcd-snapshot-20260707.db --write-out=table
Restore from a snapshot into a new data directory:
ETCDCTL_API=3 etcdctl snapshot restore /opt/backups/etcd-snapshot-20260707.db \
--data-dir=/var/lib/etcd-restored
Point etcd at the restored directory:
# Edit /etc/kubernetes/manifests/etcd.yaml
# Change hostPath volume for etcd-data from /var/lib/etcd to /var/lib/etcd-restored
Check current etcd cluster health and member list:
ETCDCTL_API=3 etcdctl endpoint health --endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key
ETCDCTL_API=3 etcdctl member list --endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key
Find the exact etcd endpoint and cert paths from the running static pod manifest:
cat /etc/kubernetes/manifests/etcd.yaml | grep -E "cert-file|key-file|trusted-ca-file|listen-client-urls"
Automate periodic snapshots via cron:
0 * * * * ETCDCTL_API=3 etcdctl snapshot save /opt/backups/etcd-$(date +\%Y\%m\%d-\%H\%M).db --cacert=... --cert=... --key=...
Common Pitfalls
- Pitfall: A cron-scheduled snapshot job reports success for months while actually producing unusable files. Why:
snapshot savecan exit with a success code while still producing a subtly corrupted or truncated file due to disk space exhaustion, permission issues mid-write, or interrupted I/O — the exit code alone does not guarantee integrity. Fix: Runetcdctl snapshot statusafter every snapshot and alert on failures; periodically test-restore a snapshot into a scratch environment to confirm the entire pipeline works end-to-end. - Pitfall: Running
etcdctl snapshot savewith the wrong endpoint or mismatched/expired certificates and silently discarding the error in a wrapper script. Why: TLS handshake failures are clear at the command line but get swallowed when scripts redirect or ignore stderr, masking the failure until an actual disaster reveals the backups never worked. Fix: Never suppressetcdctloutput in automation; surface and alert on non-zero exit codes explicitly. - Pitfall: Assuming
snapshot restoreupdates the running cluster automatically. Why:snapshot restoreonly creates a new data directory on disk — it never touches the liveetcdprocess or its currently configured data path. Fix: Explicitly edit the static pod manifest’shostPathto point at the new directory, which the kubelet then picks up automatically. - Pitfall: Expecting post-snapshot changes to survive a restore. Why: A restore is a point-in-time revert; the cluster reconciles back toward the exact state captured at snapshot time, discarding everything after. Fix: Take snapshots frequently enough to bound acceptable data loss, and communicate the snapshot’s timestamp as the recovery point objective during an incident.
Interview Questions
- You restore an etcd snapshot taken two hours ago after a corruption incident. What state does the cluster come back in, and what are the operational consequences? — Tests understanding of point-in-time recovery and reconciliation behavior.
- Why is it critical to verify a snapshot immediately after taking it, rather than trusting
snapshot save’s exit code alone? — Tests awareness that backup validity is not implied by command success. - Why does
etcdctl snapshot saverequire--cacert,--cert, and--key? — Tests understanding of etcd’s mutual-TLS security model. - Does
etcdctl snapshot restoremodify the running etcd data directory? If not, what additional step is required? — Tests hands-on familiarity with the actual restore mechanics and static pod behavior. - How would you design an automated backup strategy for etcd that guarantees backups are actually restorable, not just present? — Tests operational maturity beyond rote command knowledge.