Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 40Kubernetes · etcd

How to back up and restore etcd

etcd is the database of a Kubernetes cluster: every Deployment, Service, Secret and ConfigMap lives there. Lose etcd and you lose the cluster's state. On a self-managed (kubeadm) cluster you must back it up yourself — with a snapshot — and practise the restore before you need it. This how-to does both, shows how to automate the backup, and what to do on managed clusters (EKS, AKS, GKE) where you can not reach etcd.

  • 5 Lessen
  • 8 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

40.1

The quick answer

On a control-plane node of a kubeadm cluster:

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-$(date +%F-%H%M).db
etcdutl snapshot status /backup/etcd-2026-11-19-0200.db -w table

40.2

Step by step

  1. Find the etcd settings (endpoint and certificate paths) in the static Pod manifest:
grep -E 'listen-client-urls|cert-file|key-file|trusted-ca-file|data-dir' /etc/kubernetes/manifests/etcd.yaml
  1. Take the snapshot (the quick answer) and check it:
etcdutl snapshot status /backup/etcd-2026-11-19-0200.db -w table
+----------+----------+------------+------------+
  HASH   | REVISION | TOTAL KEYS | TOTAL SIZE |
+----------+----------+------------+------------+
7d1e2c3a |   918273 |       1432 |     9.1 MB |
+----------+----------+------------+------------+
  1. Protect the file — it contains every Secret of the cluster:
chmod 600 /backup/etcd-*.db

Copy it off the node (another server, object storage with encryption). A backup on the same disk dies with the node.

  1. Restore (the cluster goes back to the time of the snapshot). Restore into a new data folder, then point etcd to it:
etcdutl snapshot restore /backup/etcd-2026-11-19-0200.db --data-dir /var/lib/etcd-restored
sed -i 's#path: /var/lib/etcd$#path: /var/lib/etcd-restored#' /etc/kubernetes/manifests/etcd.yaml
crictl ps | grep etcd

The kubelet sees the changed manifest and restarts etcd with the restored data (wait 1–2 minutes). On a cluster with several control-plane nodes, restore on all members with the same snapshot and the --name/--initial-cluster options (see the full etcd guide).

  1. Automate — a daily snapshot with a systemd timer (or cron), keep 14 days, copy off the node:
/usr/local/bin/etcd-backup.sh
#!/bin/bash
set -euo pipefail
f=/backup/etcd-$(date +%F-%H%M).db
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 "$f"
chmod 600 "$f"
find /backup -name 'etcd-*.db' -mtime +14 -delete
  1. Other clusters:
  • k3s: k3s etcd-snapshot save (with embedded etcd); snapshots in /var/lib/rancher/k3s/server/db/snapshots/.
  • EKS / AKS / GKE: no etcd access — the provider runs it. Back up your resources and volumes with Velero, and keep all manifests in Git (GitOps = rebuild from Git).

40.3

Check that it worked

Test the restore in a lab, not first in an emergency. After a restore:

kubectl get nodes
kubectl get pods -A | grep -v Running
kubectl get deploy -A

Objects created after the snapshot are gone (expected); everything else is back.

40.4

Common problems

ProblemCauseFix
context deadline exceededWrong endpoint or certificates.Take the values from etcd.yaml; use https://127.0.0.1:2379.
etcdutl: command not foundOnly etcdctl is installed.Get etcdutl from the same etcd release (older guides use etcdctl snapshot restore).
etcd does not start after restoreThe manifest still points to the old folder, or wrong owner/SELinux label.Check hostPath and --data-dir; crictl logs of the etcd container.
API server errors for some minutes after restoreComponents cache the old state.Wait; restart kubelet on the nodes if Pods look wrong.
Snapshot file is tiny or emptyIt was taken from a broken member or the disk was full.etcdutl snapshot status; take it from a healthy member.
Secrets readable by anyone with the backupSnapshots are not encrypted.600 rights, encrypted off-site storage, and encryption at rest in the cluster.

40.5

Go further

1Why restore into a new data folder instead of the old one?

The old data stays as a fallback, and etcd starts clean from the snapshot.

2How do you back up a managed cluster (EKS/AKS/GKE) where you can not reach etcd?

Back up resources and volumes with Velero, and keep all manifests in Git.

Alle commando's in dit hoofdstuk