Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 3.1CKA, Chapter 3

Back up etcd, break the cluster, restore it

Level
Hard
45 min
Machines
a kubeadm cluster (1 control plane node is enough), root on the control plane node
Status
Not started

Goal

This task is in almost every CKA exam. You take a snapshot of etcd with the right certificates, delete things on purpose, restore the snapshot into a new data directory and point the etcd static pod to it. At the end the deleted objects are back. You also check the certificate dates and drain a node safely.

Before you start

Tasks

  1. Find the etcd endpoint and the paths of the CA, server certificate and key in /etc/kubernetes/manifests/etcd.yaml.
  2. Save a snapshot to /opt/backup/etcd-$(date +%F).db and check its status (hash, revision, number of keys).
  3. Delete the namespace backup-test.
  4. Restore the snapshot into /var/lib/etcd-restore.
  5. Change the etcd static pod to use the new data directory. Wait until the API server works again.
  6. Check that backup-test and the deployment web are back.
  7. Show when the cluster certificates expire.
  8. Drain a worker node (if you have one) for maintenance and bring it back.

Check your work

ls -lh /opt/backup/
kubectl get ns backup-test
kubectl -n backup-test get deploy web
grep -A2 "name: etcd-data" /etc/kubernetes/manifests/etcd.yaml
kubeadm certs check-expiration
  • The snapshot file is a few MB, and snapshot status showed a hash and a key count.
  • backup-test is Active and web shows 2/2.
  • The etcd-data volume points to /var/lib/etcd-restore.

Hints

Show the hints
  • etcdctl needs ETCDCTL_API=3 (default in new versions), --endpoints=https://127.0.0.1:2379, --cacert, --cert, --key.
  • In etcd 3.5+, etcdutl snapshot restore replaces etcdctl snapshot restore (the old one still works with a warning).
  • Change only the hostPath of the volume etcd-data in etcd.yaml; kubelet sees the change and restarts the pod (1–2 minutes).
  • crictl ps | grep etcd shows the etcd container while kubectl does not work.

Solution

Show the solution
  1. Read the paths.
    grep -E "listen-client-urls|trusted-ca-file|cert-file|key-file" /etc/kubernetes/manifests/etcd.yaml

    Typical values: endpoint https://127.0.0.1:2379, CA /etc/kubernetes/pki/etcd/ca.crt, cert /etc/kubernetes/pki/etcd/server.crt, key /etc/kubernetes/pki/etcd/server.key.

  2. Snapshot and check it.
    mkdir -p /opt/backup
    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-$(date +%F).db
    etcdutl snapshot status /opt/backup/etcd-$(date +%F).db -w table
  3. Break something.
    kubectl delete ns backup-test
  4. Restore into a new directory.
    etcdutl snapshot restore /opt/backup/etcd-$(date +%F).db --data-dir /var/lib/etcd-restore
  5. Point etcd to it.
    sed -i 's#path: /var/lib/etcd$#path: /var/lib/etcd-restore#' /etc/kubernetes/manifests/etcd.yaml
    watch crictl ps
    kubectl get nodes
  6. Check the objects.
    kubectl -n backup-test get deploy web
    NAME   READY   UP-TO-DATE   AVAILABLE   AGE
    web    2/2     2            2           12m
  7. Certificates.
    kubeadm certs check-expiration

    kubeadm certs renew all renews them (then restart the control plane pods); a kubeadm upgrade also renews them.

  8. Maintenance on a worker.
    kubectl drain worker1 --ignore-daemonsets --delete-emptydir-data
    kubectl get nodes
    kubectl uncordon worker1

My notes

No notes yet.

Lessons for this lab

Commands in this lab