etcd backup & restore
Snapshot etcd and restore the cluster from that snapshot.
By Invicly Tech, , in Kubernetes Administrator (CKA). About 8 minutes to read.
In short
etcd holds all cluster state, so a snapshot is a full cluster backup. You save a snapshot with etcdctl using the etcd certificates, and restore it into a new data directory. Then you point the etcd static Pod at that directory and the cluster returns to the snapshot state.
Explained simply
etcd is the cluster's memory. Every Deployment, Secret, Service and RBAC rule lives there as a key and value. If etcd's data is lost, the cluster forgets everything, even though containers may keep running for a while.
A snapshot is a photo of that memory at one moment. Restoring it is like turning the clock back: objects created after the photo disappear, and objects deleted after it come back. That makes snapshots a powerful undo button and a disaster recovery tool.
The exam loves this task because it mixes three skills: finding certificate paths, using etcdctl correctly, and editing a static Pod manifest without breaking it. Practise it until the steps feel automatic.
Press Play on a diagram to walk through it one step at a time.
- 1Find certs and endpoint
Read flags in /etc/kubernetes/manifests/etcd.yaml.
- 2Snapshot save
etcdctl with cacert, cert, key and endpoint.
- 3Verify snapshot
etcdutl snapshot status shows hash and keys.
- 4Restore to new dir
etcdutl snapshot restore --data-dir /var/lib/etcd-new.
- 5Update etcd manifest
Point the hostPath volume at the new directory.
- 6Wait and verify
etcd and API server restart; kubectl get works.
- Workloads
- deployments
- replicasets
- pods
- daemonsets
- Config
- configmaps
- secrets
- Networking
- services
- endpointslices
- networkpolicies
- Access
- roles
- rolebindings
- serviceaccounts
- Cluster
- namespaces
- nodes
- persistentvolumes
- leases
- Admin to etcdutl: snapshot restore --data-dir NEW1snapshot restore --data-dir NEW
- etcdutl to Admin: New data directory written2New data directory written
- Admin to kubelet: Edit etcd.yaml hostPath to NEW3Edit etcd.yaml hostPath to NEW
- kubelet to etcd Pod: Stop old etcd, start with new data4Stop old etcd, start with new data
- Admin to etcd Pod: kubectl get pods to verify5kubectl get pods to verify
In depth
Why etcdctl needs so many flags
etcd only accepts clients that prove who they are with a TLS certificate. So etcdctl needs four pieces: --endpoints (where etcd listens, usually https://127.0.0.1:2379), --cacert (to trust etcd's certificate), and --cert plus --key (your client identity).
On kubeadm nodes those files are in /etc/kubernetes/pki/etcd. The safest way to get exact paths is to read them from /etc/kubernetes/manifests/etcd.yaml: --trusted-ca-file is the CA, and --cert-file and --key-file are a pair that works for clients too. Exam clusters sometimes use non-default paths on purpose, so never assume.
- --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
- --listen-client-urls
- --endpoints (https://127.0.0.1:2379)
- --trusted-ca-file
- --cacert
- --cert-file
- --cert
- --key-file
- --key
Checking a snapshot is good
A snapshot file can be truncated if the disk fills up or the command is interrupted. Before trusting it, run etcdutl snapshot status FILE -w table. It prints a hash, the revision, the number of keys and the total size. A file with a sensible key count and size is a usable backup.
etcdutl works on files offline, so it needs no certificates. In recent etcd versions, status and restore moved from etcdctl to etcdutl; the old etcdctl forms still work in many installs but print deprecation warnings.
Restore without breaking the live cluster
etcdutl snapshot restore never touches the running etcd. It builds a brand-new data directory, for example /var/lib/etcd-from-backup. You then tell etcd to use it.
On kubeadm clusters the simplest change is in etcd.yaml: edit the hostPath of the volume named etcd-data to the new directory. The container still sees it at /var/lib/etcd, so the --data-dir flag can stay as it is. When you save, the kubelet notices the changed manifest and recreates etcd. The API server reconnects within a minute or two. For extra safety, many admins move the API server manifest out first and back afterwards, so nothing writes during the switch.
Snapshots in real life
In production, snapshots run on a schedule, for example a CronJob or systemd timer every hour, and copies go off the node to object storage. A backup stored only on the node it protects is lost together with that node.
Snapshots contain every Secret in the cluster, so treat the files as highly sensitive. Encrypt them at rest and restrict who can read them.
- Step 1Scheduled snapshot
CronJob or systemd timer, e.g. hourly
- Step 2Check it
etcdutl snapshot status
- Step 3Copy off the node
Object storage, not local disk only
- Step 4Protect it
Encrypt at rest; it holds every Secret
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Find etcd certificates and endpoints~45m
- 2Take a snapshot with etcdctl~45m
- 3Restore etcd from the snapshot~45m
- 4Verify the cluster after restore~45m
Key points
- etcdctl needs --endpoints, --cacert, --cert and --key for TLS.
- Copy cert paths from the etcd manifest instead of guessing.
- Restore always writes a fresh data directory; never overwrite the live one.
- etcdutl handles offline restore; etcdctl snapshot restore is deprecated.
- The kubelet recreates etcd automatically when its manifest changes.
Key terms
Tap a card to flip it. 0/5 known
Examples
export ETCDCTL_API=3
sudo etcdctl --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 \
snapshot save /opt/etcd-backup.db
sudo etcdutl snapshot status /opt/etcd-backup.db -w table
sudo etcdutl snapshot restore /opt/etcd-backup.db \
--data-dir /var/lib/etcd-restore
# Edit etcd.yaml: hostPath path -> /var/lib/etcd-restoreTaking and checking a snapshot
Task: save a snapshot to /opt/backup/etcd.db. Step 1: read the cert paths from the manifest. Step 2: run snapshot save with all four TLS flags. Step 3: check it with etcdutl snapshot status.
grep -E 'listen-client|trusted-ca|cert-file|key-file' /etc/kubernetes/manifests/etcd.yaml
sudo ETCDCTL_API=3 etcdctl --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 \
snapshot save /opt/backup/etcd.db
sudo etcdutl snapshot status /opt/backup/etcd.db -w tableUndoing a deleted namespace
Someone deleted namespace shop after your backup. Step 1: restore the snapshot into a new directory. Step 2: point the etcd-data hostPath at it. Step 3: wait for etcd and the API server to come back. Step 4: the namespace and its objects are back.
sudo etcdutl snapshot restore /opt/backup/etcd.db \
--data-dir /var/lib/etcd-from-backup
sudo vi /etc/kubernetes/manifests/etcd.yaml
# volumes:
# - name: etcd-data
# hostPath:
# path: /var/lib/etcd-from-backup # was /var/lib/etcd
watch sudo crictl ps --name etcd
kubectl get ns shopQuick reference
- Cert paths: read --trusted-ca-file, --cert-file, --key-file in /etc/kubernetes/manifests/etcd.yaml
- etcdctl snapshot save FILE with --endpoints --cacert --cert --key
- etcdutl snapshot status FILE -w table checks a snapshot offline
- etcdutl snapshot restore FILE --data-dir NEW_DIR (never the live dir)
- Point the etcd-data hostPath to NEW_DIR; kubelet restarts etcd automatically
- Store snapshot copies off the node; they contain every Secret
Common mistakes
- Restoring over /var/lib/etcd while etcd is running.
- Changing only --data-dir but not the hostPath volume, or vice versa.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Print the etcd client URL and the three certificate paths from the etcd manifest.
- Exercise 2medium
Save a snapshot to /srv/etcd-snap.db and record its key count to /srv/keys.txt.
- Exercise 3hard
Take a snapshot, delete a namespace with a Deployment in it, restore the snapshot, and prove the Deployment is back with Pods Running.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Do I need to stop etcd before taking a snapshot?
- No. snapshot save takes a consistent copy from a running member over the network.
- Does an etcd snapshot back up my application data?
- No. It holds Kubernetes objects only. Data inside PersistentVolumes needs its own backup, such as volume snapshots or a tool like Velero.
- What is ETCDCTL_API=3 for?
- Old etcdctl versions defaulted to the v2 API. Setting it to 3 is harmless today and guarantees the v3 commands are used.