Day 15: Patching the operating system by runbook
Goal: Bring all nodes to a new kernel, node by node with a reboot, following a written runbook and without downtime for workloads and the API · Duration: approx. 90 min · Prerequisite: Day 14
Briefing
- Kubernetes upgrades come three times a year, security updates for the operating system every week. A new kernel needs a reboot, and for a node a reboot is the same as a short outage.
- Today you put together what you have practiced over two weeks: drain
and PDB (Days 2 and 3), the orderly handover of the virtual address (Day
6),
move-leader(Day 11). This time as a runbook: a document that someone else can work through at three in the morning. - A runbook is not a script. It has checks with expected results and clear abort points. Whoever works through it decides at every point whether to continue.
Drill 1: What is pending?
mkdir -p ~/day2/tag15 && cd ~/day2/tag15
uname -r
sudo apt-get update -qq
apt list --upgradable 2>/dev/null | grep -E 'linux-image|kubernetes-cni|cri-tools' | cut -c1-90
apt list --upgradable 2>/dev/null | grep -c upgradable
apt-mark showhold
ls /var/run/reboot-requiredExpected:
6.8.0-149-generic
cri-tools/unknown 1.37.0-1.1 amd64 [upgradable from: 1.35.0-1.1]
kubernetes-cni/unknown 1.9.1-1.1 amd64 [upgradable from: 1.8.0-1.1]
linux-image-virtual/noble-updates 6.8.0-151.151 amd64 [upgradable from: 6.8.0-149.149]
22
kubeadm
kubectl
kubelet
ls: cannot access '/var/run/reboot-required': No such file or directory
A new kernel and a surprise: cri-tools and
kubernetes-cni come from the same package repository as
kubelet and kubeadm, but they are not on hold. An
apt upgrade upgrades them too. For cri-tools
(that is crictl) and the CNI plugins, this is harmless
here, because Cilium brings its own plugins. What matters is that you
know it, instead of finding it in the log afterwards. containerd is not
on the list. An update of the container runtime always belongs in a
maintenance window of its own.
Save your progress? Sign up for free
Drill 2: The runbook
The runbook lives as Markdown next to the cluster, in real life in the team’s Git repository:
cat > runbook-os-patch.md <<'EOF'
# Runbook: Operating system patches with reboot (kubeadm, 3 control plane nodes)
Order: nodes without a special role first, VIP holder and etcd leader last.
Only ONE node at a time. Abort at any point that is not as expected.
## 0. Before the window (once)
- [ ] Snapshot timer active, last snapshot < 6 h:
`sudo ls -lt /var/backups/k8s/etcd/ | head -2`
- [ ] All nodes Ready: `kubectl get nodes`
- [ ] etcd healthy (3 × true): `sudo -E etcdctl endpoint health -w table`
- [ ] No PDB with ALLOWED DISRUPTIONS 0: `kubectl get pdb -A`
- [ ] Load generator and API probe run on a node that is NOT up next
## 1. Per node
- [ ] Check the role: VIP holder?
`kubectl -n kube-system get lease plndr-cp-lock -o jsonpath='{.spec.holderIdentity}'`
etcd leader? `sudo -E etcdctl endpoint status -w table`
- [ ] If VIP holder: move the kube-vip manifest to /etc/kubernetes/kube-vip.yaml.pausiert,
wait 25 s, check the holder again
- [ ] If etcd leader: `etcdctl move-leader <ID of another member>`
- [ ] Drain: `kubectl drain <node> --ignore-daemonsets --delete-emptydir-data`
Expected: `node/<node> drained`
- [ ] Patch: `sudo apt-get full-upgrade` (not `upgrade`, or the kernel stays behind)
- [ ] Expected: /var/run/reboot-required exists
- [ ] Put the kube-vip manifest back (if paused)
- [ ] `sudo systemctl reboot`
- [ ] Wait until the node is Ready; new kernel: `kubectl get nodes -o wide`
- [ ] Wait briefly until all Pods on the node are Running
- [ ] `kubectl uncordon <node>`
- [ ] etcd healthy (3 × true)
- [ ] Errors in the load generator and in the API probe = 0
## 2. Afterwards
- [ ] All nodes on the new kernel, all Pods Running
- [ ] Record the result in the change log: duration, error counts, anything unusual
EOF
wc -l runbook-os-patch.mdExpected:
35 runbook-os-patch.md
Whoever works through the runbook needs no knowledge from this course, only the commands and the expectations. That is exactly what you measure it against today.
Save your progress? Sign up for free
Drill 3: Section 0
sudo ls -lt /var/backups/k8s/etcd/ | head -2
kubectl get nodes
sudo -E etcdctl endpoint health -w table
kubectl get pdb -A
kubectl -n kube-system get lease plndr-cp-lock -o jsonpath='{.spec.holderIdentity}{"\n"}'
sudo -E etcdctl endpoint status -w table | cut -d'|' -f2,10 | grep -v '^+'Expected (abridged):
NAME STATUS ROLES AGE VERSION
cp1 Ready control-plane 21h v1.37.2
cp2 Ready control-plane 21h v1.37.2
cp3 Ready control-plane 23m v1.37.2
...
cp2
ENDPOINT | IS LEADER
https://172.16.0.126:2379 | true
https://172.16.0.228:2379 | false
https://172.16.0.83:2379 | false
VIP holder cp2, etcd leader cp1. That gives the order: cp3,
cp2, cp1. The API probe runs on cp1 as a file, so that
kill hits it precisely:
cat > probe.sh <<'EOF'
while true; do
echo "$(date +%T) $(curl -sk -m 2 -o /dev/null -w %{http_code} https://172.16.0.100:6443/readyz)"
sleep 1
done
EOF
nohup sh probe.sh > api-cp3.log 2>&1 < /dev/null & echo $! > probe.pid
kubectl -n demo delete pod last
sed "s#http://web.demo#http://shop.demo#" ~/day2/tag00/last.yaml | kubectl apply -f -
kubectl -n demo wait --for=condition=Ready pod/lastSave your progress? Sign up for free
Drill 4: cp3 and the first trap
cp3 has no special role. Drain:
kubectl drain cp3 --ignore-daemonsets --delete-emptydir-data --timeout=180s | tail -1Expected:
node/cp3 drained
Now patch, first the way almost everyone types it:
# on cp3
sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::=--force-confold upgrade \
| grep -E '^The following|[0-9]+ upgraded'
ls /var/run/reboot-requiredExpected:
The following packages have been kept back:
The following packages will be upgraded:
18 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.
ls: cannot access '/var/run/reboot-required': No such file or directory
Four packages kept back, and they are exactly the important ones:
# on cp3
apt list --upgradable 2>/dev/null | tail -n +2 | cut -c1-90Expected:
linux-headers-generic/noble-updates 6.8.0-151.151 amd64 [upgradable from: 6.8.0-149.149]
linux-headers-virtual/noble-updates 6.8.0-151.151 amd64 [upgradable from: 6.8.0-149.149]
linux-image-virtual/noble-updates 6.8.0-151.151 amd64 [upgradable from: 6.8.0-149.149]
linux-virtual/noble-updates 6.8.0-151.151 amd64 [upgradable from: 6.8.0-149.149]
A new kernel is a new package
(linux-image-6.8.0-151-generic), and upgrade
does not install new packages. The metapackage stays behind, and so does
the kernel. Only full-upgrade pulls it in:
# on cp3
sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::=--force-confold full-upgrade \
| grep -E 'NEW packages|^ linux|[0-9]+ upgraded'
cat /var/run/reboot-required /var/run/reboot-required.pkgsExpected:
The following NEW packages will be installed:
linux-headers-6.8.0-151 linux-headers-6.8.0-151-generic
linux-image-6.8.0-151-generic linux-modules-6.8.0-151-generic
linux-tools-6.8.0-151 linux-tools-6.8.0-151-generic
linux-headers-generic linux-headers-virtual linux-image-virtual
linux-virtual
4 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
*** System restart required ***
linux-image-6.8.0-151-generic
linux-base
Both are in the runbook now: full-upgrade and the check
for reboot-required. Reboot:
# on cp3
sudo systemctl rebootBack on cp1, until cp3 reports Ready again:
kubectl get node cp3 -o wide | cut -c1-130Expected (after about 90 seconds):
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION
cp3 Ready,SchedulingDisabled control-plane 29m v1.37.2 172.16.0.83 <none> Ubuntu 24.04.5 LTS 6.8.0-151-generic
Ready does not mean that everything is running:
kubectl -n kube-system get pods --field-selector spec.nodeName=cp3 --no-headers \
| awk '{print $1, $2, $3}'Expected (right after Ready):
cilium-envoy-mdndh 0/1 CrashLoopBackOff
cilium-sczmv 0/1 Unknown
etcd-cp3 1/1 Running
kube-apiserver-cp3 1/1 Running
kube-controller-manager-cp3 1/1 Running
kube-proxy-46knh 0/1 CreateContainerConfigError
kube-scheduler-cp3 1/1 Running
kube-vip-cp3 1/1 Running
Alarming, but normal. kube-proxy reports in its events
services have not yet been read at least once, cannot construct envvars:
the kubelet is faster than its own view of the Services. One minute
later:
kubectl -n kube-system get pods --field-selector spec.nodeName=cp3 --no-headers \
| awk '{print $1, $2, $3}'
kubectl uncordon cp3
sudo -E etcdctl endpoint health -w tableExpected:
cilium-envoy-mdndh 1/1 Running
cilium-sczmv 1/1 Running
etcd-cp3 1/1 Running
kube-apiserver-cp3 1/1 Running
kube-controller-manager-cp3 1/1 Running
kube-proxy-46knh 1/1 Running
kube-scheduler-cp3 1/1 Running
kube-vip-cp3 1/1 Running
node/cp3 uncordoned
+---------------------------+--------+--------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+---------------------------+--------+--------------+-------+
| https://172.16.0.126:2379 | true | 73.509575ms | |
| https://172.16.0.228:2379 | true | 71.241906ms | |
| https://172.16.0.83:2379 | true | 118.090822ms | |
+---------------------------+--------+--------------+-------+
That is why the runbook says “wait until all Pods are Running” before the uncordon. The balance for cp3:
kubectl -n demo logs last | wc -l
kubectl -n demo logs last | awk '$2 != 200' | wc -l
wc -l < api-cp3.log
awk '$2 != 200' api-cp3.log | wc -lExpected:
1730
0
408
0
Save your progress? Sign up for free
Drill 5: cp2, the VIP holder
New measurement files, then hand over the virtual address as on Day 6:
kill $(cat probe.pid)
nohup sh probe.sh > api-cp2.log 2>&1 < /dev/null & echo $! > probe.pid
kubectl -n demo delete pod last
sed "s#http://web.demo#http://shop.demo#" ~/day2/tag00/last.yaml | kubectl apply -f -
kubectl -n demo wait --for=condition=Ready pod/last# on cp2
sudo mv /etc/kubernetes/manifests/kube-vip.yaml /etc/kubernetes/kube-vip.yaml.pausiertsleep 25
kubectl -n kube-system get lease plndr-cp-lock -o jsonpath='{.spec.holderIdentity}{"\n"}'
kubectl drain cp2 --ignore-daemonsets --delete-emptydir-data --timeout=180s | tail -1Expected:
cp1
node/cp2 drained
This time right from the start, and the manifest goes back before the reboot:
# on cp2
sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::=--force-confold full-upgrade \
| grep -E '[0-9]+ upgraded'
cat /var/run/reboot-required.pkgs
sudo mv /etc/kubernetes/kube-vip.yaml.pausiert /etc/kubernetes/manifests/kube-vip.yaml
sudo systemctl rebootExpected:
22 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
linux-image-6.8.0-151-generic
linux-base
After the reboot, Ready and one minute of patience:
kubectl uncordon cp2
kubectl get nodes -o custom-columns=NODE:.metadata.name,KERNEL:.status.nodeInfo.kernelVersion
kubectl -n demo logs last | wc -l
kubectl -n demo logs last | awk '$2 != 200' | wc -l
wc -l < api-cp2.log
awk '$2 != 200' api-cp2.log | wc -lExpected:
node/cp2 uncordoned
NODE KERNEL
cp1 6.8.0-149-generic
cp2 6.8.0-151-generic
cp3 6.8.0-151-generic
1434
0
345
0
Not a single API error, because the virtual address moved in an orderly way beforehand. kube-vip on cp2 starts again after the reboot, but does not take the address back. cp1 stays the holder.
Save your progress? Sign up for free
Drill 6: cp1, everything at once
cp1 is now VIP holder and etcd leader, and the load generator and the
probe run on cp1. All four have to go first. The load generator is bound
to nodeName: cp1, so it moves briefly to cp2:
kill $(cat probe.pid)
kubectl -n demo delete pod last
sed "s#http://web.demo#http://shop.demo#; s#nodeName: cp1#nodeName: cp2#" \
~/day2/tag00/last.yaml | kubectl apply -f -
kubectl -n demo wait --for=condition=Ready pod/last
sudo -E ETCDCTL_ENDPOINTS=https://172.16.0.126:2379 etcdctl move-leader 6292e5989e4c860bExpected:
pod "last" deleted from demo namespace
pod/last created
pod/last condition met
Leadership transferred from 33764b6962314e3f to 6292e5989e4c860b
From now on, the probe runs on cp2:
# on cp2
mkdir -p ~/day2/tag15 && cd ~/day2/tag15
cat > probe.sh <<'EOF'
while true; do
echo "$(date +%T) $(curl -sk -m 2 -o /dev/null -w %{http_code} https://172.16.0.100:6443/readyz)"
sleep 1
done
EOF
nohup sh probe.sh > api-cp1.log 2>&1 < /dev/null & echo $! > probe.pidThen hand over the VIP and drain. The selector leaves
last untouched; it runs on cp2 anyway:
sudo mv /etc/kubernetes/manifests/kube-vip.yaml /etc/kubernetes/kube-vip.yaml.pausiert
sleep 25
kubectl -n kube-system get lease plndr-cp-lock -o jsonpath='{.spec.holderIdentity}{"\n"}'
kubectl drain cp1 --ignore-daemonsets --delete-emptydir-data --pod-selector='app!=last' \
--timeout=180s | tail -1Expected:
cp2
node/cp1 drained
sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::=--force-confold full-upgrade \
| grep -E '[0-9]+ upgraded'
sudo mv /etc/kubernetes/kube-vip.yaml.pausiert /etc/kubernetes/manifests/kube-vip.yaml
sudo systemctl rebootWhile cp1 reboots, it is worth looking at the Pods from cp2:
# on cp2
sudo kubectl --kubeconfig /etc/kubernetes/admin.conf -n demo get pods -o wide --no-headers \
| awk '{print $1, $3, $7}'One web Pod stays Pending. Its events
say:
0/3 nodes are available: 1 node(s) had untolerated taint(s), 2 node(s) didn't match pod topology spread constraints.
The topology spread constraint from Day 7 (maxSkew: 1,
DoNotSchedule) still counts cp1 as a zone with zero Pods. A
third Pod on cp2 or cp3 would create a skew of 2. So it waits until cp1
is back. Thanks to the PDB (maxUnavailable: 1), two out of
three Pods are enough, and no errors occur. In a cluster with three
nodes, though, this is a reason to consider
ScheduleAnyway.
After the reboot, Ready and one minute:
kubectl uncordon cp1
kubectl get nodes -o custom-columns=NODE:.metadata.name,KERNEL:.status.nodeInfo.kernelVersion
kubectl -n demo logs last | wc -l
kubectl -n demo logs last | awk '$2 != 200' | wc -l# on cp2
kill $(cat ~/day2/tag15/probe.pid)
wc -l < ~/day2/tag15/api-cp1.log
awk '$2 != 200' ~/day2/tag15/api-cp1.log | wc -lExpected:
node/cp1 uncordoned
NODE KERNEL
cp1 6.8.0-151-generic
cp2 6.8.0-151-generic
cp3 6.8.0-151-generic
1324
0
301
0
And the load generator goes back to its place:
kubectl -n demo delete pod last
sed "s#http://web.demo#http://shop.demo#" ~/day2/tag00/last.yaml | kubectl apply -f -Save your progress? Sign up for free
Drill 7: The balance
| Node | Role | Requests shop |
Errors | API checks | Errors |
|---|---|---|---|---|---|
| cp3 | none | 1730 | 0 | 408 | 0 |
| cp2 | VIP holder | 1434 | 0 | 345 | 0 |
| cp1 | VIP holder, etcd leader | 1324 | 0 | 301 | 0 |
Three kernel reboots in 20 minutes, 0 errors in
4,488 requests and 1,054 API checks. Two items went into the runbook
during the run: full-upgrade and “Pods Running before
uncordon”. That is exactly what the first run of a runbook is for.
Save your progress? Sign up for free
Exercise: Speed Drills
- Show whether a node needs a reboot and because of which packages. Target time: 30 s
- Show the kernel of all nodes. Target time: 1 min
- Show all packages that are on hold. Target time: 30 s
Solution
cat /var/run/reboot-required.pkgs 2>/dev/null || echo "no reboot needed"
kubectl get nodes -o custom-columns=NODE:.metadata.name,KERNEL:.status.nodeInfo.kernelVersion
apt-mark showholdSave your progress? Sign up for free
Checkpoint
kubectl get nodes -o custom-columns=NODE:.metadata.name,KERNEL:.status.nodeInfo.kernelVersionExpected: all three nodes with
6.8.0-151-generic
sudo -E etcdctl endpoint health -w tableExpected: three members, all true
Challenge
The runbook still contains a mistake that you worked around yourself today. Find it.
Hint: Where did the API probe run when it was cp1’s turn, and what does section 0 say about it?
Solution
Section 0 requires the load generator and the probe to run on a node that is not up next. That is correct for one node, but not for the whole window: at some point, every node is up next. It belongs in section 1, right after the role check:
- [ ] Are the load generator or API probe running on this node? Then move them first.
sed -i 's/^- \[ \] If etcd leader:/- [ ] Are the load generator or API probe running on this node? Then move them first.\n&/' \
runbook-os-patch.md
grep -n 'move them first' runbook-os-patch.mdExpected:
20:- [ ] Are the load generator or API probe running on this node? Then move them first.
Save your progress? Sign up for free
Cleanup
Nothing to roll back. The runbook stays, and so does the kernel.
Interview Note
- How do you patch a cluster’s nodes without downtime? One after the other: hand over the roles (VIP, etcd leader), drain, patch, reboot, wait until everything runs, uncordon, check. PDBs make sure the drain breaks nothing.
- Why
full-upgradeinstead ofupgrade? A new kernel is a new package.upgradedoes not install new packages and holds it back. - What belongs in a runbook? Checks with expected results, order, abort points, a way back. And after every run, the corrections.
Enterprise Note
- kured (Kubernetes Reboot Daemon, CNCF) automates
this day: it sees
/var/run/reboot-required, takes a lock so that only one node at a time is up, drains, reboots and releases. It does not know the roles of kube-vip and etcd. For control plane nodes, today’s manual work often still makes sense. - Immutable operating systems such as Talos or Flatcar replace packages with whole images. Patching there means: new image, reboot, done, with a built-in way back.
- Runbooks live in Git next to the code, with review like any other change. Many teams link them to their incident tool so that on-call engineers can open them straight from the alert.