Back Up and Restore etcd Data
Learn to back up and restore etcd data in Kubernetes with hands-on steps, troubleshooting, and best practices for cluster resilience.
Focus: back up and restore etcd data
Your Kubernetes cluster is a black box until etcd, the brain storing every pod, service, and config, goes silent. A single corrupt snapshot or misconfigured backup can mean total data loss — hours of re-creation, broken state, and lost debugging context. In production, etcd is the most critical yet often most neglected component; mastering backup and restore gives you the power to survive disk failure, accidental deletion, or a misapplied manifest.
The problem this lesson solves
etcd is the only source of truth for your cluster's state. Every Pod, ConfigMap, Secret, Deployment, and RBAC binding lives inside its key-value store. If etcd crashes and you lack a recent backup, you cannot simply "restart" — you must rebuild the entire cluster from scratch, losing all workload history and persistent metadata. The pain grows exponentially with multi-master setups: a split-brain scenario or a snapshot that silently decays overnight can leave operators scrambling to restore from outdated snapshots. This lesson eliminates that fear by showing you exactly how to create, verify, and restore etcd snapshots.
Core concept / mental model
Think of etcd as the brain of Kubernetes — it stores every object's current state. A backup is a frozen brain scan: a single snapshot file (.db extension) that captures all keys and values at one point in time. Restoring is like replaying that scan into a fresh brain, but only if the snapshot was taken when the brain was healthy.
Pro Tip: A snapshot is not just a copy of the etcd data directory. It is a point-in-time, consistent snapshot that etcd itself produces via
etcdctl snapshot save. Do not attempt to copy/var/lib/etcddirectly while etcd is running — you will get a corrupted backup.
Key definitions
- Snapshot: A single file containing all etcd data at a specific moment.
- etcdctl: The CLI client that interacts with etcd — the only tool you should use for backup and restore.
- Member: Each etcd node in the cluster. In production, you typically have 3 or 5 nodes (odd number).
- snapshot status: Command to verify a snapshot file is valid.
How it works step by step
Backup and restore follows a simple but strict sequence. Here is the mental flow:
- Locate etcd — On master nodes, etcd runs as a static pod or systemd service. Find its endpoint (usually
https://127.0.0.1:2379). - Take a snapshot — Using
etcdctl(v3 API) with the correct TLS certificates (--cacert,--cert,--key). - Verify the snapshot — Run
etcdctl snapshot statusto confirm the file is not corrupt. - Backup the snapshot — Store it off-cluster: Stash, S3, or even a second disk.
- Restore — Stop etcd (if running), clear the data directory, then run
etcdctl snapshot restoreto create a new member with the snapshot's data. - Start etcd — Restart the etcd service or static pod. The cluster re-joins and sees the restored state.
Pro Tip: Always test your restore procedure in a staging cluster first. A backup you have never restored is a prayer, not a plan.
Hands-on walkthrough
We will simulate a single-master cluster (or a multi-master with one node). These commands assume you have etcdctl installed and access to the etcd certificates on a control plane node. If you use a managed Kubernetes (EKS, GKE, AKS), you do not have direct etcd access — use the provider's backup plan instead.
1. Check etcd endpoint and version
# List etcd pods (if static)
kubectl get pods -n kube-system | grep etcd
# SSH into the control plane node and check etcdctl version
etcdctl version
2. Take a snapshot
# Use the certs from /etc/kubernetes/pki/etcd
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 /backup/etcd-snapshot-$(date +%Y%m%d%H%M).db
Expected output:
Snapshot saved at /backup/etcd-snapshot-202503151200.db
3. Verify the snapshot
ETCDCTL_API=3 etcdctl --write-out=table snapshot status /backup/etcd-snapshot-202503151200.db
Expected output:
+----------+----------+------------+------------+
| HASH | REVISION | TOTAL KEYS | TOTAL SIZE |
+----------+----------+------------+------------+
| abcdef12 | 123456 | 1024 | 12 MB |
+----------+----------+------------+------------+
A hash that is not 0000000000000000 means the snapshot is valid.
4. Simulate a disaster — restore from snapshot
First, stop etcd:
# If static pod
mv /etc/kubernetes/manifests/etcd.yaml /tmp/
# Wait for pod to stop
kubectl get pods -n kube-system | grep etcd
Then restore:
# Remove the old data directory
rm -rf /var/lib/etcd
# Restore to a new data directory
ETCDCTL_API=3 etcdctl snapshot restore /backup/etcd-snapshot-202503151200.db \
--data-dir=/var/lib/etcd-restore \
--name=master-node \
--initial-cluster=master-node=https://127.0.0.1:2380 \
--initial-cluster-token=etcd-cluster
Expected output: (no output means success)
Move the restored data into place:
mv /var/lib/etcd-restore /var/lib/etcd
Restart etcd:
mv /tmp/etcd.yaml /etc/kubernetes/manifests/
# Wait for etcd pod to come back
kubectl get pods -n kube-system -w
Verify cluster health:
kubectl cluster-info
kubectl get nodes
All your namespaces, deployments, and configs should reappear.
5. Automate the backup (optional but recommended)
Create a cron job on the control plane node:
# /etc/cron.d/etcd-backup
0 */6 * * * root /usr/local/bin/etcd-backup.sh
Where etcd-backup.sh contains the etcdctl snapshot save command and copies the file to S3 via aws s3 cp.
Pro Tip: For multi-master clusters, take the snapshot from one member only — you do not need to back up every node. But keep snapshots from all nodes in case one is corrupted.
Compare options / when to choose what
| Option | Use Case | Pros | Cons |
|---|---|---|---|
etcdctl snapshot save |
Production clusters you manage | Full data, deterministic, verifiable | Requires direct etcd API access; manual or cron-driven |
| Managed backup (EKS, GKE, AKS) | Managed Kubernetes | No infrastructure burden, automated | Limited restore granularity; vendor lock-in |
| etcd operator backup | Stateful workloads with Operator pattern | Integrated with custom resources | Adds complexity; not suitable for generic clusters |
When to choose what:
- Self-managed cluster (kubeadm, on-premises): Use
etcdctl snapshot savewith a cron job and off-cluster storage. - Managed Kubernetes: Use the provider's backup feature (EKS backup via AWS Backup, GKE backup via Snapshots).
- High-volume clusters (>10k keys): Consider periodic incremental snapshots (via etcd v3.5+ snapshot retention) and separate S3 lifecycle policies.
Troubleshooting & edge cases
Snapshot file is empty or hash is zeroes
Cause: The endpoint was unreachable or the snapshot was interrupted.
Fix: Run etcdctl endpoint health first, then retry with --debug flag.
ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 endpoint health
Restored cluster has wrong node names
Cause: The --name and --initial-cluster flags do not match the etcd configuration.
Fix: Extract the original member name from the snapshot or use --skip-force-new-cluster only if you understand the risks. For production, always specify --name correctly.
Cluster fails to start after restore
Symptoms: etcd pod crash-looping, kubectl get nodes returns nothing.
Checklist:
- Verify snapshot was taken from the same cluster (not a different one).
- Ensure the
--data-dirpath matches the--data-dirin the etcd manifest. - Confirm TLS certificates are still valid — restore does not change certificates.
- Check etcd logs:
journalctl -u etcd -forkubectl logs <etcd-pod>.
Restoring to a fresh node (scale-up scenario)
When adding a new etcd member, you do not restore a snapshot. Instead, join the new member to the existing cluster using etcdctl member add. Snapshot restore is only for disaster recovery or when replacing a dead member completely.
What you learned & what's next
You now know how to back up and restore etcd data — the brain of your Kubernetes cluster. You can take a snapshot, verify its integrity, and restore it to a clean state. You understand that etcdctl snapshot save and etcdctl snapshot restore are your go-to commands, and that testing the restore process is as critical as the backup itself.
Next lesson: Now that your cluster's state is safe, learn how to update and roll back a deployment — because even with backups, you want to recover quickly from faulty rollouts.
Practice recap
SSH into a test cluster control plane node. Run etcdctl snapshot save to create a snapshot, then inspect it with etcdctl snapshot status. Simulate a disaster by stopping etcd and restoring from that snapshot. Verify all pods and workloads reappear. Automate the backup with a cron job that saves to a local directory.
Common mistakes
- Copying the etcd data directory directly instead of using
etcdctl snapshot save— you'll get a corrupted, unusable backup. - Forgetting to specify
--cacert,--cert, and--keywhen runningetcdctl snapshot save— the command will fail or connect to a wrong endpoint. - Restoring a snapshot to a cluster with a different set of etcd member names — the restored member will not rejoin correctly.
- Not verifying the snapshot with
snapshot statusafter creation — you discover corruption only when you need to restore. - Assuming managed Kubernetes (EKS, GKE, AKS) allows direct etcd access — it does not; use the provider's backup mechanism instead.
Variations
- Use a cron job on every control plane node to rotate snapshots with retention policies (e.g., keep last 7 daily snapshots).
- Automate off-cluster storage using S3, Stash, or external backup tools like Velero.
- For multi-cluster environments, consider centralizing snapshots into a shared object store with versioning.
Real-world use cases
- Disaster recovery after accidental deletion of a critical namespace or deployment.
- Migrating a self-managed Kubernetes cluster to a new set of nodes (dump + restore snapshot).
- Recovering from etcd data corruption caused by a failed etcd upgrade or disk I/O errors.
Key takeaways
- etcd is the brain of Kubernetes; protect it with regular
etcdctl snapshot savecommands. - Always verify snapshot integrity with
etcdctl snapshot statusimmediately after creation. - Restore via
etcdctl snapshot restorerequires stopping etcd, wiping the data directory, and re-specifying member identity. - Managed Kubernetes clusters do not give direct etcd access — use the cloud provider's backup tooling.
- Automate backups with a cron job and off-site storage; test your restore process in a staging cluster.
- Snapshot restore is for disaster recovery, not for adding new members — use
etcdctl member addfor scaling.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.