Sub topic 6
Scheduling snapshots and keeping them safe
This chapter assumes you can already take an etcd snapshot with etcdctl and check it with etcdutl. It also assumes you have SSH access to a kubeadm control plane node and an S3 bucket you can write to. Here you will turn the manual commands into a repeating job and protect the files it produces.
What an automated job must do
A snapshot taken by hand is a good start, but it goes stale. A useful job does four things on every run:
- Saves a snapshot with a timestamped name.
- Verifies that the file is readable.
- Copies it off the node.
- Deletes old local copies.
Copying off the node matters most. If the control plane disk fails, a snapshot stored on that disk fails with it.
A backup script
Save this as /usr/local/sbin/etcd-backup.sh and make it executable with chmod 700. It uses the default kubeadm certificate paths.
#!/usr/bin/env bash
set -euo pipefail
umask 077
BACKUP_DIR=/var/backups/etcd
BUCKET=s3://my-etcd-backups
STAMP=$(date -u +%Y%m%dT%H%M%SZ)
SNAP="$BACKUP_DIR/etcd-snapshot-$STAMP.db"
mkdir -p "$BACKUP_DIR"
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 "$SNAP"
etcdutl snapshot status "$SNAP" --write-out=table
aws s3 cp "$SNAP" "$BUCKET/" --sse AES256
find "$BACKUP_DIR" -name 'etcd-snapshot-*.db' -mtime +7 -deleteset -e stops the script at the first failure. A failed save therefore never reaches the upload or the pruning step. umask 077 makes the snapshot readable only by its owner.
Running it on a schedule
On a node with cron, add a file at /etc/cron.d/etcd-backup. This entry runs the script every six hours:
0 */6 * * * root /usr/local/sbin/etcd-backup.sh >> /var/log/etcd-backup.log 2>&1Six hours is a starting point. The gap between runs is the most data you can lose, so shorten it if your cluster state changes often. The last chapter of this tutorial covers this limit as the recovery point objective.
Keeping snapshots safe
A snapshot holds everything in the cluster, including Secrets. Anyone who can read the file can read those values.
Apply these controls:
- Restrict the backup directory and the bucket to the few people and roles that need them.
- Turn on server-side encryption for the bucket, as the
--sse AES256flag does above. - Give the node an IAM role that can only write to this bucket, not a broad account key.
- Enable bucket versioning or a lifecycle rule. Versioning stops a compromised node from silently overwriting old backups. A lifecycle rule expires old backups so the bucket does not grow without limit.
Common errors
- Silent cron failures. Cron does not report errors to you. Check the log file, and alert when no new object has appeared in the bucket for longer than your schedule interval.
- Running on every control plane node. One snapshot from one healthy member is enough. Running the job on all nodes produces duplicates.
- Never testing a restore. A file that passes
snapshot statusis readable, but only a restore proves it works.
Exercise
The script runs find ... -mtime +7 -delete after the upload. Why is it safer to put this line last, with set -e active, than to prune first?
Show answer
With set -e, a failed save, verify or upload stops the script before the prune step. Old local snapshots are then deleted only after a new snapshot has been saved, verified and copied off the node. If you pruned first, a failing job could delete your old backups without creating a new one.