Skip to content
Kubernetes day 2 · Chapter 11 of 20 Day 9: v1.37 on your own – and the emergency

Day 9: v1.37 on your own – and the emergency

Goal: Upgrade the cluster to v1.37.2 using the order from Day 6, then repair an accident with the etcd snapshot, including the consequences nobody thinks of · Duration: approx. 120 min · Prerequisite: Day 8

Briefing

  • Part one: the second upgrade, this time with everything you have learned. Goal: no gap in the API, no error in the workload.
  • Part two: someone accidentally deletes the demo namespace. Your way back is the etcd snapshot you took before the upgrade.
  • A restore resets the entire cluster, not just what you are missing. And the components that watch the API know nothing about it.

Warning: Part 2 restores etcd on all three nodes. Take a snapshot of the VMs first if your virtualization supports it.

Drill 1: Approval and a fresh backup

The approval from Day 5, extended by the check from Day 8. For v1.37: Cilium 1.20 is only tested up to v1.36 (Day 8). You deliberately accept this risk in the lab. Then the backup at the v1.36 state, copied to both other nodes, because for a restore every node needs the snapshot locally:

mkdir -p ~/day2/tag09 && cd ~/day2/tag09
sudo 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 /var/backups/k8s/etcd-v1.36.6.db
sudo etcdutl snapshot status /var/backups/k8s/etcd-v1.36.6.db -w table
sudo tar -czf /var/backups/k8s/etc-kubernetes-v1.36.6.tgz -C / etc/kubernetes
sudo cp /var/backups/k8s/etcd-v1.36.6.db /var/backups/k8s/etc-kubernetes-v1.36.6.tgz /tmp/
sudo chown $USER /tmp/etcd-v1.36.6.db /tmp/etc-kubernetes-v1.36.6.tgz
for n in 172.16.0.228 172.16.0.83; do
  scp /tmp/etcd-v1.36.6.db /tmp/etc-kubernetes-v1.36.6.tgz ubuntu@$n:/tmp/
  ssh ubuntu@$n "sudo mkdir -p /var/backups/k8s && \
    sudo mv /tmp/etcd-v1.36.6.db /tmp/etc-kubernetes-v1.36.6.tgz /var/backups/k8s/"
done
rm -f /tmp/etcd-v1.36.6.db /tmp/etc-kubernetes-v1.36.6.tgz

Expected (snapshot table):

+----------+----------+------------+------------+---------+
|   HASH   | REVISION | TOTAL KEYS | TOTAL SIZE | VERSION |
+----------+----------+------------+------------+---------+
| c1c66043 |    32402 |       1659 |      16 MB |   3.6.0 |
+----------+----------+------------+------------+---------+

The measurements as on Day 6: a new last against shop, and the API probe on cp3 (commands from Day 6, Drill 1, with ~/day2/tag09/api.log). Who holds the address?

kubectl -n kube-system get lease plndr-cp-lock -o jsonpath='{.spec.holderIdentity}{"\n"}'

Expected:

cp1

Save your progress? Sign up for free

Drill 2: Choose the first node wisely

On Day 6 you started with the leader and paid for it with 20 seconds of API. kubeadm upgrade apply does not have to run on cp1; any control plane node will do. This time cp2 starts, and the leader cp1 comes last.

# on cp2
sudo sed -i "s#/v1.36/#/v1.37/#g" /etc/apt/sources.list.d/kubernetes.list
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.37/deb/Release.key \
  | sudo gpg --batch --yes --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
sudo apt-get update
sudo apt-mark unhold kubeadm
sudo apt-get install -y kubeadm=1.37.2-1.1
sudo apt-mark hold kubeadm
sudo kubeadm upgrade plan | sed -n '/Upgrade to the latest stable/,/kubeadm upgrade apply/p'

Expected (abridged):

COMPONENT                 NODE      CURRENT   TARGET
kube-apiserver            cp1       v1.36.6   v1.37.2
...
kube-proxy                          1.36.6    v1.37.2
CoreDNS                             v1.14.2   v1.14.6
etcd                      cp1       3.6.8-0   3.7.0-0
etcd                      cp2       3.6.8-0   3.7.0-0
etcd                      cp3       3.6.8-0   3.7.0-0

New compared to Day 6: etcd also makes a minor jump, from 3.6 to 3.7. The same rule applies to etcd as to Kubernetes: only one minor version per step. kubeadm follows it, because each Kubernetes version ships exactly one etcd version.

# on cp2
sudo kubeadm upgrade apply v1.37.2 --yes

Expected (abridged, 212 seconds in the lab):

[upgrade/staticpods] Component "etcd" upgraded successfully!
[upgrade/staticpods] Component "kube-apiserver" upgraded successfully!
...
[upgrade/addon] Skipping upgrade of addons because control plane instances [cp1 cp3] have
not been upgraded
[upgrade] SUCCESS! A control plane node of your cluster was upgraded to "v1.37.2".
# on cp3
awk '$2 != 200' ~/day2/tag09/api.log | wc -l

Expected:

0

Not a single failed probe, and shop stays at 0 errors. cp3 follows the same way with kubeadm upgrade node (package repository and kubeadm as above), also without an API gap.

Save your progress? Sign up for free

Drill 3: The leader last

sudo mv /etc/kubernetes/manifests/kube-vip.yaml /etc/kubernetes/kube-vip.yaml.pausiert
sleep 30
kubectl -n kube-system get lease plndr-cp-lock -o jsonpath='{.spec.holderIdentity}{"\n"}'

Expected:

cp2

Then on cp1: package repository and kubeadm to v1.37, sudo kubeadm upgrade node, then put kube-vip back:

sudo kubeadm upgrade node | grep addons
sudo mv /etc/kubernetes/kube-vip.yaml.pausiert /etc/kubernetes/manifests/kube-vip.yaml

Expected:

[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
# on cp3
awk '$2 != 200' ~/day2/tag09/api.log
wc -l < ~/day2/tag09/api.log

Expected:

21:41:55 000
781

One failed probe out of 781, exactly in the second when cp1 gave up the address. On Day 6 there were twelve. And shop:

kubectl -n demo logs last | awk '$2 != 200' | wc -l

Expected:

0

Save your progress? Sign up for free

Drill 4: The kubelets to v1.37

As on Day 7, in the order cp2, cp3, cp1: drain, packages kubelet=1.37.2-1.1 kubectl=1.37.2-1.1, restart, uncordon. Then:

kubectl get nodes
kubectl -n demo logs last | wc -l
kubectl -n demo logs last | awk '$2 != 200'

Expected:

NAME   STATUS   ROLES           AGE    VERSION
cp1    Ready    control-plane   166m   v1.37.2
cp2    Ready    control-plane   163m   v1.37.2
cp3    Ready    control-plane   162m   v1.37.2
4510
21:46:21 000
21:49:27 000

Two errors in 4,510 requests, one each during the drain of cp2 and of cp1. On Day 7 there were zero, with the same application. Both are single requests that ran into the two-second curl timeout. Most likely each hit exactly the moment when nginx shut down after the preStop hook while a connection was still open. The lesson: probe, preStop and PDB make errors very unlikely, but they do not make them impossible. 2 out of 4,510 is 0.04 percent, and that is exactly why clients have retries.

Save your progress? Sign up for free

Drill 5: The accident

For the second part you need etcdutl on all three nodes and a measurement that does not live in the demo namespace, because last is about to disappear with it. So cp1 itself measures against the service IP of shop:

for n in 172.16.0.228 172.16.0.83; do
  scp /usr/local/bin/etcdutl ubuntu@$n:/tmp/
  ssh ubuntu@$n "sudo install /tmp/etcdutl /usr/local/bin/"
done
IP=$(kubectl -n demo get svc shop -o jsonpath='{.spec.clusterIP}')
nohup sh -c "while true; do echo \"\$(date +%T) \$(curl -s -o /dev/null -w %{http_code} \
  -m 2 http://$IP)\"; sleep 0.5; done" > ~/day2/tag09/shop-host.log 2>&1 &

The state before the accident, for comparison:

kubectl -n kube-system get cm kubeadm-config -o jsonpath='{.data.ClusterConfiguration}' \
  | grep kubernetesVersion
kubectl -n kube-system get deploy coredns \
  -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'

Expected:

kubernetesVersion: v1.37.2
registry.k8s.io/coredns/coredns:v1.14.6

And now the slip:

kubectl delete namespace demo
kubectl get ns demo

Expected:

namespace "demo" deleted
Error from server (NotFound): namespaces "demo" not found

shop no longer responds. The only way back is the snapshot.

Save your progress? Sign up for free

Drill 6: Restore etcd

Warning: From here on, the API is unreachable for minutes. Everything that has happened in the cluster since the snapshot is lost.

Step 1: On all three nodes, stop the API server and etcd by moving their manifests out of the manifests folder:

# on cp1, cp2 and cp3
sudo mkdir -p /etc/kubernetes/angehalten
sudo mv /etc/kubernetes/manifests/kube-apiserver.yaml \
  /etc/kubernetes/manifests/etcd.yaml /etc/kubernetes/angehalten/

After half a minute, none of these containers is running anymore:

# on cp1, cp2 and cp3
sudo crictl ps --name "etcd|kube-apiserver" -q | wc -l

Expected: 0 on every node.

Step 2: On each node, move the old data directory aside and restore the snapshot with its own name and its own address. The list of all members is the same on all nodes:

# on cp1 (on cp2 and cp3: adjust name and address)
IC="cp1=https://172.16.0.126:2380,cp2=https://172.16.0.228:2380,cp3=https://172.16.0.83:2380"
sudo mv /var/lib/etcd /var/lib/etcd.vor-restore
sudo etcdutl snapshot restore /var/backups/k8s/etcd-v1.36.6.db --name cp1 \
  --initial-cluster $IC --initial-cluster-token day2-restore \
  --initial-advertise-peer-urls https://172.16.0.126:2380 --data-dir /var/lib/etcd
sudo ls /var/lib/etcd/member

Expected (abridged):

restored snapshot   {"path": "/var/backups/k8s/etcd-v1.36.6.db", "wal-dir": "/var/…
snap
wal

Step 3: Start etcd on all three nodes, wait, check:

# on cp1, cp2 and cp3
sudo mv /etc/kubernetes/angehalten/etcd.yaml /etc/kubernetes/manifests/
sudo 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 member list -w table | cut -c1-80

Expected:

+------------------+---------+------+---------------------------+---------------------
|        ID        | STATUS  | NAME |        PEER ADDRS         |       CLIENT ADDRS
+------------------+---------+------+---------------------------+---------------------
| 33764b6962314e3f | started |  cp1 | https://172.16.0.126:2380 | https://172.16.0.126
| 6292e5989e4c860b | started |  cp2 | https://172.16.0.228:2380 | https://172.16.0.228
| cf664b54c8c1c3e0 | started |  cp3 |  https://172.16.0.83:2380 |  https://172.16.0.83
+------------------+---------+------+---------------------------+---------------------

The IDs are new: a restore creates a new etcd cluster from old data. The etcd image is 3.7.0 and the data comes from 3.6. That works because etcd takes over older data on startup.

Step 4: the API servers.

# on cp1, cp2 and cp3
sudo mv /etc/kubernetes/angehalten/kube-apiserver.yaml /etc/kubernetes/manifests/
kubectl get ns demo
kubectl get nodes
kubectl -n demo get pods -o wide | cut -c1-80

Expected:

NAME   STATUS   AGE
demo   Active   6h13m
NAME   STATUS   ROLES           AGE     VERSION
cp1    Ready    control-plane   6h21m   v1.36.6
cp2    Ready    control-plane   6h19m   v1.36.6
cp3    Ready    control-plane   6h18m   v1.36.6
NAME                   READY   STATUS    RESTARTS   AGE     IP             NODE
last                   1/1     Running   0          4h4m    10.244.0.243   cp1
shop-fb966798b-pztbh   1/1     Running   0          4h38m   10.244.0.1     cp1
shop-fb966798b-tql5d   1/1     Running   0          4h37m   10.244.1.50    cp2
...

demo is back. But look closely: the nodes report v1.36.6, even though kubelets v1.37.2 are running there. The Pods show Running, four hours old, even though their containers were deleted three minutes ago. And shop still does not respond.

Save your progress? Sign up for free

Drill 7: Restart everyone who is watching

Cause: kubelets, controller manager, scheduler and Cilium watch the API with a version number (resourceVersion). After the restore, etcd jumped back to revision 32402, but the watchers are waiting for changes after a much higher number. They notice nothing and stick to their picture of the world. The Kubernetes documentation says: after a restore, restart all components that cache state.

First the kubelets:

# on cp1, cp2 and cp3
sudo systemctl restart kubelet
kubectl get nodes
kubectl -n demo get pods

Expected:

NAME   STATUS   ROLES           AGE     VERSION
cp1    Ready    control-plane   6h23m   v1.37.2
cp2    Ready    control-plane   6h21m   v1.37.2
cp3    Ready    control-plane   6h20m   v1.37.2
NAME                   READY   STATUS    RESTARTS   AGE
last                   1/1     Running   1          4h5m
shop-fb966798b-pztbh   0/1     Running   1          4h39m
shop-fb966798b-tql5d   0/1     Running   1          4h39m
...

The nodes report their real version again, and the Pods start up again (RESTARTS 1, new IPs). Then the controller manager and the scheduler, whose containers the kubelet restarts immediately:

# on cp1, cp2 and cp3
sudo crictl stop $(sudo crictl ps --name "kube-controller-manager|kube-scheduler" -q)

shop still stays at 0/1, and the network in the cluster is dead:

kubectl -n demo exec last -- curl -s -m 2 -o /dev/null -w "%{http_code}\n" http://web.demo
kubectl -n kube-system exec ds/cilium -c cilium-agent -- cilium-dbg status --brief

Expected:

000
command terminated with exit code 28
error in controller resolve-labels-demo/web-75d549d885-nwljh: pod.core
"web-75d549d885-nwljh" not found

Cilium is the fourth watcher: its endpoints and identities come from the time after the snapshot. Restart it as well:

kubectl -n kube-system rollout restart ds/cilium deploy/cilium-operator
kubectl -n kube-system rollout status ds/cilium
kubectl -n kube-system exec ds/cilium -c cilium-agent -- cilium-dbg status --brief
kubectl -n demo get pods

Expected:

daemon set "cilium" successfully rolled out
OK
NAME                   READY   STATUS    RESTARTS   AGE
last                   1/1     Running   1          4h7m
shop-fb966798b-pztbh   1/1     Running   1          4h41m
shop-fb966798b-tql5d   1/1     Running   1          4h41m
...

The results of the emergency:

awk '$2 != 200 {if (!a) a=$1; l=$1} END {print "erster Fehler", a, "letzter Fehler", l}' \
  ~/day2/tag09/shop-host.log

Expected:

erster Fehler 01:22:33 letzter Fehler 01:29:08

Six and a half minutes (“erster Fehler” is the first error, “letzter Fehler” the last). The restore itself took about two. The rest was the cost of the watchers’ stale state. In the order kubelet, controller manager and scheduler, network plugin, right after the API servers started, it would have been much faster.

Save your progress? Sign up for free

Drill 8: What else the snapshot reset

kubectl -n kube-system get cm kubeadm-config -o jsonpath='{.data.ClusterConfiguration}' \
  | grep kubernetesVersion
kubectl -n kube-system get deploy coredns \
  -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
kubectl -n kube-system get ds kube-proxy \
  -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'

Expected:

kubernetesVersion: v1.36.6
registry.k8s.io/coredns/coredns:v1.14.2
registry.k8s.io/kube-proxy:v1.36.6

Everything stored in etcd is at the state from before the upgrade: the kubeadm configuration, CoreDNS, kube-proxy. The kube-proxy Pods are even running with the old image again. The static Pods on the nodes, on the other hand, are v1.37.2, because their manifests live on disk, not in etcd. A mixed state that kubeadm straightens out:

# on cp2
sudo kubeadm upgrade apply v1.37.2 --yes | grep -i 'addon\|SUCCESS'

Expected (12 seconds in the lab):

[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
[upgrade] SUCCESS! A control plane node of your cluster was upgraded to "v1.37.2".
kubectl -n kube-system get cm kubeadm-config -o jsonpath='{.data.ClusterConfiguration}' \
  | grep kubernetesVersion
kubectl -n kube-system get ds kube-proxy \
  -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'

Expected:

kubernetesVersion: v1.37.2
registry.k8s.io/kube-proxy:v1.37.2

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Show the etcd version and the leader of all three members. Target time: 2 min
  2. Delete the backup of the old etcd data directory on all three nodes, but only after you have checked that etcd is healthy. Target time: 3 min
  3. Find out which revision etcd has now. Target time: 2 min

Solution

sudo etcdctl --endpoints=https://172.16.0.126:2379,https://172.16.0.228:2379,\
https://172.16.0.83:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt \
  --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key \
  endpoint status -w table | cut -d'|' -f2,4,10 | grep -v '^+'
         ENDPOINT          | VERSION | IS LEADER
 https://172.16.0.126:2379 |   3.7.0 |      true
 https://172.16.0.228:2379 |   3.7.0 |     false
  https://172.16.0.83:2379 |   3.7.0 |     false
# on cp1, cp2 and cp3, after endpoint health = true
sudo rm -rf /var/lib/etcd.vor-restore

The revision is in the JSON output of endpoint status under header.revision.

Save your progress? Sign up for free

Checkpoint

kubectl version
kubectl get nodes

Expected: the server and all three kubelets on v1.37.2.

kubectl -n demo get pods
kubectl -n kube-system exec ds/cilium -c cilium-agent -- cilium-dbg status --brief

Expected: all Pods 1/1 Running, Cilium OK.

Challenge

Write down the order of an etcd restore as a list of commands, short enough that you can work through it at three in the morning, including the restarts from Drill 7 and the correction from Drill 8.

Hint: Five blocks: stop, restore, start etcd, start the API servers, restart the watchers. Then clean up.

Solution

  1. On all nodes: take kube-apiserver.yaml and etcd.yaml out of manifests, and wait until crictl ps no longer shows such containers.
  2. On each node: move /var/lib/etcd aside, run etcdutl snapshot restore with its own --name and its own --initial-advertise-peer-urls, and the same --initial-cluster.
  3. On all nodes: put etcd.yaml back, wait for etcdctl endpoint health.
  4. On all nodes: put kube-apiserver.yaml back, wait for kubectl get --raw /readyz.
  5. On all nodes systemctl restart kubelet, then controller manager and scheduler via crictl stop, then kubectl rollout restart for the network plugin and all operators with their own cache.
  6. kubeadm upgrade apply <current version> aligns configuration and add-ons.
  7. Check what was lost since the snapshot (Deployments, Secrets, Helm releases), and reapply it from Git.

Save your progress? Sign up for free

Cleanup

pkill -f shop-host
# on cp3
pkill -f readyz

Interview Note

  • What does an etcd restore reset? Everything stored in etcd: objects, the kubeadm configuration, add-ons. Static Pod manifests, certificates on disk and the running containers are not reset.
  • Why do you have to restart components after a restore? Because watches work with a resourceVersion. If etcd jumps back, the watchers wait for changes that never come.
  • Why is a restore the very last way back? Because it discards all changes since the snapshot, including the correct ones. A deleted namespace can often be restored faster from Git.

Enterprise Note

  • For individual deleted objects, Velero or GitOps (Argo CD, Flux) is the better way back. An etcd restore is meant for the total loss of the control plane.
  • Restore drills belong in the operations calendar, at least once a year, with measured duration. The six and a half minutes of this lab are a guideline, not a promise.
  • Operators with their own state (database operators, service mesh, cert-manager) belong on the list of restarts after a restore.

Contents

More