Sub topic 5
Restoring etcd from a snapshot
This chapter assumes you can SSH to a kubeadm control plane node, know that etcd stores cluster state, and have a snapshot saved and checked with etcdutl snapshot status. It covers a single control plane node. Multi-member clusters are handled in a later chapter.
What a restore does
A restore does not load data into a running etcd. It builds a new data directory from the snapshot file. You then point etcd at that directory. The new directory gets fresh cluster metadata, so it is a new etcd cluster that holds the old data.
Restoring replaces the cluster state with the state at snapshot time. Objects created after the snapshot disappear. Objects deleted after it come back. Running containers are not touched, but the API server will now describe an older world, so expect some drift to clean up.
Step 1: Stop etcd and the API server
On a kubeadm cluster, etcd and the API server run as static Pods. The kubelet watches /etc/kubernetes/manifests and runs whatever manifests it finds there. Moving a manifest out of that directory stops the Pod.
sudo mkdir -p /etc/kubernetes/manifests-stopped
sudo mv /etc/kubernetes/manifests/kube-apiserver.yaml /etc/kubernetes/manifests-stopped/
sudo mv /etc/kubernetes/manifests/etcd.yaml /etc/kubernetes/manifests-stopped/Stopping the API server first prevents it from writing to etcd while you work. Wait until sudo crictl ps no longer lists either container.
Step 2: Restore into a new directory
Restore to a new path. Do not overwrite /var/lib/etcd, because it is your way back if the restore fails.
etcdutl snapshot restore /var/backups/etcd-snapshot.db \
--data-dir /var/lib/etcd-restoredStep 3: Point the static Pod at the new directory
Edit the manifest in the stopped directory. Change only the hostPath of the data volume.
# /etc/kubernetes/manifests/etcd.yaml (excerpt)
volumes:
- hostPath:
path: /var/lib/etcd-restored
type: DirectoryOrCreate
name: etcd-dataThe container's --data-dir flag stays /var/lib/etcd. The volume mount maps your new host directory to that path inside the container.
Step 4: Start and verify
Move both manifests back. The kubelet starts etcd, then the API server.
sudo mv /etc/kubernetes/manifests-stopped/etcd.yaml /etc/kubernetes/manifests/
sudo mv /etc/kubernetes/manifests-stopped/kube-apiserver.yaml /etc/kubernetes/manifests/
kubectl get nodes
kubectl get pods -ACheck that an object that existed at snapshot time is present, and that one you created afterwards is gone.
Common errors
| Symptom | Likely cause |
|---|---|
| etcd Pod restarts in a loop | `hostPath` points to a directory that is missing or has wrong ownership |
| Old data still appears | The manifest still points at `/var/lib/etcd` |
| `snapshot restore` fails with "data-dir exists" | The target directory is not empty. Choose a new path |
| `kubectl` times out | The API server manifest was not moved back, or etcd is not yet healthy |
Exercise
Why do you restore the snapshot into a new directory such as /var/lib/etcd-restored instead of into /var/lib/etcd, and what do you change in the etcd manifest afterwards?
Show answer
Restoring to a new path keeps the original data directory intact as a fallback, and etcdutl refuses to restore into a non-empty directory anyway. Afterwards you change the hostPath of the etcd-data volume in the etcd static Pod manifest to the new directory, so the kubelet restarts etcd with the restored state.