Lesson 61 · Section 6: Cluster Maintenance
Lesson 61: Cluster maintenance: looking after a cluster that is already running
The map for this section: the three maintenance jobs (taking a node out of service, upgrading Kubernetes, and backing up cluster state), why each one has a safe order, and the handful of commands behind them. · 16 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why cluster maintenance exists
Up to now every lesson has been about what you run on a cluster. This section is about the cluster itself, after the day it was installed. A cluster is never finished:
- The machines need work. Each node is a Linux server. It gets kernel and security updates, it needs a reboot now and then, and sooner or later its hardware fails or is replaced.
- Kubernetes moves on. The project publishes a new minor version about three times a year and supports only the three newest minor versions, each for roughly one year. A cluster that is never upgraded runs unsupported software within about a year.
- The cluster's memory is one database. Every Deployment, Secret and Service you created exists as a record in etcd. Lose etcd without a backup and the cluster forgets all of it, even while the containers keep running.
None of this is hard on one server that you can switch off for an evening. The difficulty is that the applications are expected to stay up while you do it.
Parts:
- Running cluster (must stay in service)
- Node maintenance (drain, work, uncordon)
- Version upgrade (one minor at a time)
- Backup and restore (etcd snapshot)
- Pods keep running (moved before the work)
- Parts stay compatible (control plane first)
- State can come back (after loss or mistake)
Connections:
- Running cluster to Node maintenance: a node needs work
- Running cluster to Version upgrade: release
- Running cluster to Backup and restore: before any change
- Node maintenance to Pods keep running: result
- Version upgrade to Parts stay compatible: result
- Backup and restore to State can come back: result
What the section covers
| Job | The question it answers | Main tools | Lessons that go deep |
|---|---|---|---|
| Node maintenance | How do I work on a node without dropping its Pods? | kubectl drain, cordon, uncordon | OS Upgrades |
| Version upgrade | Which version can I move to, and in what order? | kubeadm upgrade, the package manager | Kubernetes Releases, Cluster Upgrade Introduction, Demo Cluster Upgrade |
| Backup and restore | How do I get the cluster's state back? | etcdctl, etcdutl, your manifests | Backup and Restore Methods, Working with ETCDCTL and ETCDUTL |
This lesson gives you the whole picture once, so that each later lesson has a place to fit. In the exam these jobs belong to "Manage the lifecycle of Kubernetes clusters" and "Create and manage Kubernetes clusters using kubeadm", inside a domain worth 25% of the score. The tasks are procedural: nothing to design, only steps to carry out in the right order on the right machine.
How each job works
Job 1: taking a node out of service
A node can leave service in two ways: without warning, or on your terms.
Without warning. When a node stops reporting, the control plane marks it NotReady and puts a taint on it.
Pods tolerate that taint for 300 seconds by default. If the node is back within those five minutes, its Pods simply
carry on. If it is not, the Pods are evicted. Pods owned by a Deployment, ReplicaSet or StatefulSet are created
again on other nodes. A Pod that nobody owns is gone for good.
On your terms. Waiting five minutes and hoping is not a plan, so before planned work you empty the node yourself:
Parts:
- node01 (Ready, runs Pods)
- node01 (SchedulingDisabled)
- Pods on node02 (recreated elsewhere)
- Do the work (patch, reboot, upgrade)
- node01 (Ready, but empty)
- New Pods only (old ones stay away)
Connections:
- node01 to node01: cordon
- node01 to Pods on node02: evict
- Pods on node02 to Do the work: node is empty
- Do the work to node01: uncordon
- node01 to New Pods only: later
| Command | What it does | Pods already on the node |
|---|---|---|
kubectl cordon node01 | Marks the node unschedulable | Stay where they are |
kubectl drain node01 | Cordons the node, then evicts its Pods | Leave; their controllers recreate them elsewhere |
kubectl uncordon node01 | Makes the node schedulable again | Nothing comes back by itself |
Two details explain most surprises with drain:
- Drain evicts, it does not move. A Pod is never migrated. It is terminated on this node, and its controller
notices one replica is missing and creates a new Pod, which the scheduler places on another node. That is why a
Pod without a controller is a problem: nothing would recreate it, so
drainrefuses to continue unless you add--force, and then the Pod is deleted and lost. - Uncordon does not rebalance. After
uncordonthe node isReadyand empty. The Pods that left are happily running elsewhere, and Kubernetes does not shuffle running Pods around. The node fills up again only as new Pods are created.
Job 2: moving to a new version
A Kubernetes version looks like v1.35.2: major, minor, patch. Patch releases carry fixes only. Minor releases
add and remove features, and they are the ones an "upgrade" is about.
A cluster is many programs, and they cannot all be replaced in the same second. For a while the API server is on the new version and the kubelets are on the old one. Kubernetes allows this, within published limits called the version skew policy:
| Component | Allowed version, compared with kube-apiserver |
|---|---|
| kube-apiserver | The reference. In a highly available control plane, the API servers may differ by one minor version |
| kube-controller-manager, kube-scheduler | Same, or one minor version older. Never newer |
| kubelet, kube-proxy | Same, or up to three minor versions older. Never newer |
| kubectl | One minor version older or newer |
Two rules follow from the table, and together they dictate the whole procedure:
- Nothing may be newer than the API server. So the control plane is upgraded first and the nodes after it.
- Minor versions cannot be skipped. Going from 1.34 to 1.36 means upgrading to 1.35 first, then to 1.36.
On a cluster built with kubeadm, the same tool performs the upgrade:
Parts:
- kubeadm (new version first)
- Control plane (kubeadm upgrade apply)
- Its kubelet (package, then restart)
- Worker node (drain it first)
- kubeadm, kubelet (upgrade node, restart)
- Uncordon (then the next worker)
Connections:
- kubeadm to Control plane: plan
- Control plane to Its kubelet: then
- Its kubelet to Worker node: control plane done
- Worker node to kubeadm, kubelet: empty
- kubeadm, kubelet to Uncordon: Ready
- Upgrade the kubeadm package
- kubeadm upgrade plan
- kubeadm upgrade apply on the control plane
- Upgrade kubelet and kubectl, restart the kubelet
- Each worker: drain, kubeadm upgrade node, upgrade kubelet, uncordon
kubeadm upgrade apply replaces the static Pod manifests of the API server, controller manager, scheduler and etcd,
and renews the cluster's certificates along the way. It does not touch the kubelet, because the kubelet is a
system service installed as a package. You upgrade that yourself, on every node, and restart it.
Under the hood · Why the kubelet may lag three versions but never lead
The API server is written to understand requests from older clients: old API versions stay available for a long time after a newer one appears. So an old kubelet can keep talking to a new API server. The reverse is not promised. A newer kubelet may ask for an API or a field that an older API server has never heard of.
The three-version allowance (it was two before Kubernetes 1.28) is practical. Upgrading the control plane is quick; replacing or upgrading hundreds of worker nodes is slow. With three versions of room, and three releases a year, a team can upgrade the control plane every release and refresh the nodes about once a year without ever leaving the supported range.
Job 3: keeping a copy of the state
Everything the API server knows is stored in etcd. So there are two ways to keep a copy of a cluster:
| Method | What you save | Good for | Limits |
|---|---|---|---|
| The manifests | The YAML files you applied, in Git | Rebuilding applications on any cluster | Misses whatever was created by hand with kubectl create or kubectl run |
| An etcd snapshot | One file with every object in the cluster | Restoring this cluster exactly as it was | Needs access to etcd; not possible on most managed clusters |
Neither method saves the data inside volumes. A database's files live on a PersistentVolume, not in etcd, and need their own backup.
Parts:
- etcd (all cluster state)
- snapshot.db (one file)
- Safe place (off the node)
- etcdutl (snapshot restore)
- New data folder (/var/lib/etcd-restore)
- etcd static Pod (hostPath points to it)
Connections:
- etcd to snapshot.db: save
- snapshot.db to Safe place: copy
- Safe place to etcdutl: after a disaster
- etcdutl to New data folder: writes
- New data folder to etcd static Pod: mount
The etcd route uses two small programs that ship with etcd:
etcdctltalks to a running etcd server over the network. It takes the snapshot, and because etcd in Kubernetes requires TLS, it needs the server address and three certificate files.etcdutlworks on files on disk, with no server involved. It checks a snapshot file and restores one into a new data folder. Older guides restore withetcdctl snapshot restore; that form was deprecated in etcd 3.5 and is gone in etcd 3.6, so useetcdutl.
A restore does not load data into the running database. It writes a new data folder, and you then change the etcd static Pod manifest so that etcd starts from that folder. The cluster comes back as it was at the moment of the snapshot: anything created later is gone, and anything deleted later is back.
Using it
These three examples are a first look. Each job gets its own lesson, with every flag explained.
Example · Take stock before you change anything
Any maintenance task starts with the same questions: which nodes, which versions, and how was the cluster built?
kubectl get nodes -o wide # nodes, roles, kubelet version, OS image
kubectl version # kubectl (client) and API server versions
kubeadm version -o short # the kubeadm installed on this machine
kubectl -n kube-system get pods -o wide # etcd and the control plane as static Pods
NAME STATUS ROLES AGE VERSION OS-IMAGE
controlplane Ready control-plane 40d v1.35.2 Ubuntu 24.04.3 LTS
node01 Ready <none> 40d v1.35.2 Ubuntu 24.04.3 LTS
Pods named etcd-controlplane and kube-apiserver-controlplane in kube-system tell you this is a kubeadm
cluster with etcd running as a static Pod on the control plane node. That decides where every later command runs.
Example · Empty a node, work on it, bring it back
node01 needs a kernel update and a reboot.
kubectl get pods -A -o wide --field-selector spec.nodeName=node01 # what runs there now
kubectl drain node01 --ignore-daemonsets
node/node01 cordoned
Warning: ignoring DaemonSet-managed Pods: kube-system/kube-proxy-7xk2p
evicting pod default/web-7c5ddbdf54-x2x9k
pod/web-7c5ddbdf54-x2x9k evicted
node/node01 drained
kubectl get nodes # node01: Ready,SchedulingDisabled
ssh node01 # do the work: update packages, reboot
kubectl uncordon node01 # back on the control plane
kubectl get nodes # node01: Ready
The web Pod is now running on another node and stays there. node01 is back in service, but empty apart from
its DaemonSet Pods.
Example · Save one etcd snapshot and check it
On the control plane node of a kubeadm cluster, the certificates sit in /etc/kubernetes/pki/etcd:
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 /opt/backup/snapshot.db
sudo etcdutl --write-out=table snapshot status /opt/backup/snapshot.db
+----------+----------+------------+------------+
| HASH | REVISION | TOTAL KEYS | TOTAL SIZE |
+----------+----------+------------+------------+
| 6c1b7d2e | 48213 | 1184 | 5.1 MB |
+----------+----------+------------+------------+
A table with a key count means the file is a readable snapshot. Restoring it is a separate procedure, covered in
the backup lessons: etcdutl snapshot restore into a new folder, then one path changed in
/etc/kubernetes/manifests/etcd.yaml.
Tricks
Common mistakes
- Expecting Pods to return after
uncordon. They do not. Uncordon only allows new Pods to be scheduled there. - Using
cordonwhen the task needsdrain. Cordon stops new Pods but leaves the running ones on the node, where a reboot will kill them. - Adding
--forcewithout reading the error. It deletes Pods that have no controller, and nothing recreates them. - Upgrading a worker before the control plane, or skipping a minor version. Both break the version skew policy.
kubeadm upgrade plantells you which version you may move to. - Thinking
kubeadm upgrade applyupgrades everything. The kubelet and kubectl are packages, upgraded separately on each node, followed by a restart of the kubelet. - Treating an etcd snapshot as a complete backup. It holds the API objects, not the files inside your volumes.
- Never testing a restore. A backup you have not restored once is a hope. Practise the restore on a lab cluster.
Recap
- Cluster maintenance is three jobs: node maintenance, version upgrades, and backup and restore.
draincordons a node and evicts its Pods;uncordonreopens it, and the old Pods do not come back.- Upgrade one minor version at a time: kubeadm, then the control plane, then each worker, drained first.
- No component may be newer than the API server; the kubelet may be up to three minor versions older.
- All cluster state is in etcd: save it with
etcdctl snapshot save, restore it withetcdutl snapshot restore.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.