Day 14: Automating upgrades with k3s
Goal: Raise a k3s cluster declaratively across two minor versions with the system-upgrade-controller, measure outages and tie upgrades to release channels · Duration: approx. 60 min · Prerequisite: Day 13
Briefing
- On Days 6, 7 and 9 you did every step by hand: package repository, kubeadm, kubelet, node by node. That is instructive, but you cannot keep it up with 50 clusters.
- Rancher’s system-upgrade-controller (SUC) turns
this around: in a
Planobject you describe which nodes should move to which version. For each node, the controller starts a Job that cordons the node, swaps the binary and restarts the service. - It works best with k3s and RKE2, because there Kubernetes is a
single binary. Today you use a dedicated k3s server on the VM
k3s(172.16.0.182), starting at v1.35.10.
Plan (version: v1.36.6+k3s1)
│ controller compares with the node version
▼
Job per node ─▶ cordon ─▶ swap k3s binary ─▶ restart k3s ─▶ uncordon
Drill 1: Setting up k3s
All commands today run on the VM k3s. Port 6444 instead
of 6443, and without Traefik and ServiceLB, so that nothing collides
with other services on the VM:
# on k3s
mkdir -p ~/day2/tag14 && cd ~/day2/tag14
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.35.10+k3s1 sh -s - server \
--https-listen-port 6444 --disable traefik --disable servicelb --write-kubeconfig-mode 600Expected (end):
[INFO] systemd: Creating service file /etc/systemd/system/k3s.service
[INFO] systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO] systemd: Starting k3s
Warning: If another cluster already runs on the VM (in our case a kind cluster), k3s restarts in a loop. The journal then says:
"Failed to start cAdvisor" err="inotify_init: too many open files"The limit for inotify instances (default 128) is used up. The fix:
printf 'fs.inotify.max_user_instances = 1024\nfs.inotify.max_user_watches = 524288\n' \ | sudo tee /etc/sysctl.d/60-inotify.conf sudo sysctl --system >/dev/null sudo systemctl restart k3s
The kubeconfig is under /etc/rancher/k3s/k3s.yaml,
readable only by root. A copy for you:
mkdir -p ~/.kube
sudo cat /etc/rancher/k3s/k3s.yaml > ~/.kube/k3s.yaml && chmod 600 ~/.kube/k3s.yaml
export KUBECONFIG=~/.kube/k3s.yaml
kubectl get nodes -o wide | cut -c1-80
kubectl get pods -AExpected:
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IM
argo Ready control-plane 92s v1.35.10+k3s1 172.16.0.182 <none> Ubuntu
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-b644fd95b-nq846 1/1 Running 0 32s
kube-system local-path-provisioner-68868f474b-pfkz5 1/1 Running 0 31s
kube-system metrics-server-7f4b6d9bd7-xkvjn 1/1 Running 0 31s
A whole cluster in one binary, including containerd, CoreDNS and metrics-server.
Save your progress? Sign up for free
Drill 2: Measuring points
The familiar test application and the load generator, this time
against web:
kubectl create namespace demo
kubectl -n demo create deployment web --image=traefik/whoami:v1.11 --replicas=2
kubectl -n demo expose deployment web --port=80
cat > last.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: last
namespace: demo
spec:
containers:
- name: curl
image: curlimages/curl:8.17.0
command: ["sh","-c","while true; do echo \"$(date +%T) $(curl -s -m 2 -o /dev/null -w %{http_code} http://web.demo)\"; sleep 0.2; done"]
EOF
kubectl apply -f last.yaml
kubectl -n demo wait --for=condition=Ready pod/last
kubectl -n demo logs last | tail -1Expected:
16:45:40 200
Plus the API probe. This time 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://127.0.0.1:6444/livez)"
sleep 1
done
EOF
nohup sh probe.sh > api.log 2>&1 < /dev/null & echo $! > probe.pid
tail -1 api.logExpected:
16:45:51 401
401 instead of 200: k3s does not let
anonymous requests through, not even to /livez. That is
enough for the measurement, because 401 means “the API
server answers”. Outages show up as 000 (no connection) or
503 (not ready yet).
Save your progress? Sign up for free
Drill 3: Installing the controller
kubectl apply \
-f https://github.com/rancher/system-upgrade-controller/releases/download/v0.21.1/crd.yaml \
-f https://github.com/rancher/system-upgrade-controller/releases/download/v0.21.1/system-upgrade-controller.yaml
kubectl -n system-upgrade rollout status deploy/system-upgrade-controller --timeout=180sExpected:
customresourcedefinition.apiextensions.k8s.io/plans.upgrade.cattle.io created
namespace/system-upgrade created
serviceaccount/system-upgrade created
role.rbac.authorization.k8s.io/system-upgrade-controller created
clusterrole.rbac.authorization.k8s.io/system-upgrade-controller created
clusterrole.rbac.authorization.k8s.io/system-upgrade-controller-drainer created
rolebinding.rbac.authorization.k8s.io/system-upgrade created
clusterrolebinding.rbac.authorization.k8s.io/system-upgrade created
clusterrolebinding.rbac.authorization.k8s.io/system-upgrade-drainer created
configmap/default-controller-env created
deployment.apps/system-upgrade-controller created
deployment "system-upgrade-controller" successfully rolled out
A fixed version instead of latest in the URL: the
controller itself belongs under version control too.
Save your progress? Sign up for free
Drill 4: The first plan
cat > plan-server.yaml <<'EOF'
apiVersion: upgrade.cattle.io/v1
kind: Plan
metadata:
name: server
namespace: system-upgrade
spec:
concurrency: 1
cordon: true
nodeSelector:
matchExpressions:
- key: node-role.kubernetes.io/control-plane
operator: In
values: ["true"]
serviceAccountName: system-upgrade
upgrade:
image: rancher/k3s-upgrade
version: v1.36.6+k3s1
EOF| Field | Meaning |
|---|---|
concurrency: 1 |
at most one node at a time |
cordon: true |
cordon the node, but do not evict Pods |
nodeSelector |
which nodes this plan applies to |
upgrade.image |
container that swaps the binary; tag = version |
version |
target version |
For workers, there would be a second plan with its own
nodeSelector and prepare, so that workers only
get their turn after the servers. With one node, one plan is enough.
Go:
date +%T
kubectl apply -f plan-server.yaml
for i in $(seq 1 30); do
sleep 6
echo "$(date +%T) $(kubectl get nodes --no-headers 2>/dev/null | awk '{print $2, $5}')"
doneExpected (abridged; stop with Ctrl+C as soon as the
node is Ready again):
16:46:14
plan.upgrade.cattle.io/server created
16:46:20 Ready v1.35.10+k3s1
16:46:27 Ready,SchedulingDisabled v1.35.10+k3s1
16:46:48 Ready,SchedulingDisabled v1.35.10+k3s1
16:46:55
16:47:02
16:47:08
16:47:15 Ready,SchedulingDisabled v1.35.10+k3s1
16:47:22 Ready,SchedulingDisabled v1.36.6+k3s1
16:47:43 Ready v1.36.6+k3s1
Six seconds after the plan, the node is cordoned. At 16:46:55
kubectl goes silent: k3s is starting with the new binary.
90 seconds later the node is on v1.36.6 and schedulable again. What the
Job did:
kubectl -n system-upgrade logs job/$(kubectl -n system-upgrade get jobs -o name | head -1 | cut -d/ -f2) \
| tail -6Expected:
+ info 'K3s binary has been replaced successfully'
+ echo '[INFO] ' 'K3s binary has been replaced successfully'
+ return
+ kill_k3s_process
+ kill -SIGTERM 1064831
+ info 'Successfully killed old k3s pid 1064831'
Replace the binary, stop the old process, systemd starts the new one. That is all there is to it.
Save your progress? Sign up for free
Drill 5: The balance
kubectl -n demo logs last | wc -l
kubectl -n demo logs last | awk '$2 != 200' | wc -l
awk '{print $2}' api.log | sort | uniq -c
awk '$2=="000" || $2=="503"' api.log | sed -n '1p;$p'
kubectl -n demo get pods -o custom-columns=NAME:.metadata.name,RESTARTS:.status.containerStatuses[0].restartCountExpected:
582
0
14 000
106 401
6 503
16:46:54 000
16:47:14 503
NAME RESTARTS
last 0
web-66d4895877-8l5mj 0
web-66d4895877-f9wqp 0
0 errors in the application, even though the cluster’s only node swapped out its Kubernetes. The API server was gone for 20 seconds (16:46:54 to 16:47:14). When k3s restarts, it does not stop the running containers. They belong to containerd and simply keep running.
Save your progress? Sign up for free
Drill 6: On to 1.37
An upgrade is a change to one line:
kubectl -n demo delete pod last --now
kubectl apply -f last.yaml && kubectl -n demo wait --for=condition=Ready pod/last
nohup sh probe.sh > api2.log 2>&1 < /dev/null & echo $! > probe.pid
date +%T
kubectl -n system-upgrade patch plan server --type=merge -p '{"spec":{"version":"v1.37.2+k3s1"}}'After a good minute (AGE counts from the installation):
kubectl get nodes
kubectl -n system-upgrade get jobs
kubectl -n demo logs last | wc -l
kubectl -n demo logs last | awk '$2 != 200' | wc -l
kill $(cat probe.pid)
awk '$2=="000" || $2=="503"' api2.log | sed -n '1p;$p'Expected:
NAME STATUS ROLES AGE VERSION
argo Ready control-plane 8m40s v1.37.2+k3s1
NAME STATUS COMPLETIONS DURATION AGE
apply-server-on-argo-with-113f28e8e04db5f7c93cd28024ad23e-784b0 Complete 1/1 66s 79s
360
0
16:49:59 000
16:50:20 503
Again 0 errors and 21 seconds without the API. Two minor versions in five minutes, without a single command on the node itself. The Job name contains a hash of the plan: every change to the plan creates a new Job.
Save your progress? Sign up for free
Exercise: Speed Drills
- Show all plans with their target version. Target time: 30 s
- Show which version the controller resolved for the plan. Target time: 1 min
- Show which nodes are currently cordoned. Target time: 30 s
Solution
kubectl -n system-upgrade get plans
kubectl -n system-upgrade get plan server -o jsonpath='{.status.latestVersion}{"\n"}'
kubectl get nodes | grep SchedulingDisabledlatestVersion writes the + as
- (v1.37.2-k3s1), because it has to work as a
label value.
Save your progress? Sign up for free
Checkpoint
kubectl get nodes -o jsonpath='{.items[0].status.nodeInfo.kubeletVersion}{"\n"}'Expected: v1.37.2+k3s1
kubectl -n system-upgrade get plan server -o jsonpath='{.status.conditions[?(@.type=="Complete")].status}{"\n"}'Expected: True
Challenge
Fixed versions have to be bumped by you. k3s publishes release
channels, such as “latest 1.37”. Switch the plan to the channel
v1.37 and check whether the controller starts a new
upgrade.
Hint: version and channel
are mutually exclusive.
Solution
kubectl -n system-upgrade patch plan server --type=json -p='[
{"op":"remove","path":"/spec/version"},
{"op":"add","path":"/spec/channel","value":"https://update.k3s.io/v1-release/channels/v1.37"}]'
sleep 15
kubectl -n system-upgrade get plan server -o jsonpath='{.status.latestVersion}{"\n"}'
kubectl -n system-upgrade get jobs --no-headers | wc -lExpected:
plan.upgrade.cattle.io/server patched
v1.37.2-k3s1
1
The channel points to v1.37.2, which is already running, so no new
Job. When v1.37.3 comes out, the controller starts the upgrade on its
own. Convenient, but it also means that patches arrive without you
looking at the changelog. For production, a channel per minor version
(v1.37) is acceptable, stable or
latest is not, because then the jump to the next minor
version also arrives unannounced.
Save your progress? Sign up for free
Cleanup
k3s is no longer needed. The bundled script removes it completely:
sudo /usr/local/bin/k3s-uninstall.sh >/dev/null
rm -f ~/.kube/k3s.yamlInterview Note
- How do you automate Kubernetes upgrades on k3s or
RKE2? With the system-upgrade-controller: a
Plandefines the target version or channel, the affected nodes and the concurrency, and the controller starts a Job per node. - Why do the Pods survive a k3s restart? The containers belong to containerd. When k3s stops, they keep running, and the new kubelet takes them over.
- What is the risk of channels? Upgrades arrive without review. Only use channels per minor version, and read the changelogs anyway.
Enterprise Note
- Rancher, Fleet or Argo CD distribute plans to many clusters. Waves are common: first test, then one production region, then the rest, with waiting time in between.
- For kubeadm clusters, there is no equivalent tool in the project
itself. Cluster API and Talos (
talosctl upgrade-k8s) solve this with their own means. - A plan with
draininstead ofcordonevicts Pods before the swap. With several nodes and PDBs (Day 3), that is the cleaner choice. With a single node like today,drainwould have stopped everything.