Skip to content
LF Cert Trainer

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

1/10Cluster maintenance
0:00 / 3:31

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.

The three jobs of cluster maintenancea node needs workreleasebefore any changeresultresultresultRunning clustermust stay in serviceNode maintenancedrain, work, uncordonVersion upgradeone minor at a timeBackup and restoreetcd snapshotPods keep runningmoved before the workParts stay compatiblecontrol plane firstState can come backafter loss or mistake
The three jobs of cluster maintenance. Each job protects something different: the running Pods, the compatibility between components, and the cluster's stored state.

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

JobThe question it answersMain toolsLessons that go deep
Node maintenanceHow do I work on a node without dropping its Pods?kubectl drain, cordon, uncordonOS Upgrades
Version upgradeWhich version can I move to, and in what order?kubeadm upgrade, the package managerKubernetes Releases, Cluster Upgrade Introduction, Demo Cluster Upgrade
Backup and restoreHow do I get the cluster's state back?etcdctl, etcdutl, your manifestsBackup 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:

Taking a node out of service and bringing it backcordonevictnode is emptyuncordonlaternode01Ready, runs Podsnode01SchedulingDisabledPods on node02recreated elsewhereDo the workpatch, reboot, upgradenode01Ready, but emptyNew Pods onlyold ones stay away
Taking a node out of service and bringing it back. Drain is cordon plus eviction. Uncordon only reopens the node for new Pods: the Pods that left do not return.

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
CommandWhat it doesPods already on the node
kubectl cordon node01Marks the node unschedulableStay where they are
kubectl drain node01Cordons the node, then evicts its PodsLeave; their controllers recreate them elsewhere
kubectl uncordon node01Makes the node schedulable againNothing 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 drain refuses to continue unless you add --force, and then the Pod is deleted and lost.
  • Uncordon does not rebalance. After uncordon the node is Ready and 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:

ComponentAllowed version, compared with kube-apiserver
kube-apiserverThe reference. In a highly available control plane, the API servers may differ by one minor version
kube-controller-manager, kube-schedulerSame, or one minor version older. Never newer
kubelet, kube-proxySame, or up to three minor versions older. Never newer
kubectlOne minor version older or newer

Two rules follow from the table, and together they dictate the whole procedure:

  1. Nothing may be newer than the API server. So the control plane is upgraded first and the nodes after it.
  2. 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:

The order of a kubeadm upgradeplanthencontrol plane doneemptyReadykubeadmnew version firstControl planekubeadm upgrade applyIts kubeletpackage, then restartWorker nodedrain it firstkubeadm, kubeletupgrade node, restartUncordonthen the next worker
The order of a kubeadm upgrade. The tool first, then the control plane, then the workers one by one. Nothing on a node may ever be newer than the API server it talks to.

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
  1. Upgrade the kubeadm package
  2. kubeadm upgrade plan
  3. kubeadm upgrade apply on the control plane
  4. Upgrade kubelet and kubectl, restart the kubelet
  5. Each worker: drain, kubeadm upgrade node, upgrade kubelet, uncordon
A kubeadm upgrade, end to end

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:

MethodWhat you saveGood forLimits
The manifestsThe YAML files you applied, in GitRebuilding applications on any clusterMisses whatever was created by hand with kubectl create or kubectl run
An etcd snapshotOne file with every object in the clusterRestoring this cluster exactly as it wasNeeds 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.

An etcd backup, and the way backsavecopyafter a disasterwritesmountetcdall cluster statesnapshot.dbone fileSafe placeoff the nodeetcdutlsnapshot restoreNew data folder/var/lib/etcd-restoreetcd static PodhostPath points to it
An etcd backup, and the way back. A snapshot is one file. Restoring it writes a new data folder, and etcd is then pointed at that folder.

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:

  • etcdctl talks 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.
  • etcdutl works on files on disk, with no server involved. It checks a snapshot file and restores one into a new data folder. Older guides restore with etcdctl snapshot restore; that form was deprecated in etcd 3.5 and is gone in etcd 3.6, so use etcdutl.

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 cordon when the task needs drain. Cordon stops new Pods but leaves the running ones on the node, where a reboot will kill them.
  • Adding --force without 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 plan tells you which version you may move to.
  • Thinking kubeadm upgrade apply upgrades 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.
  • drain cordons a node and evicts its Pods; uncordon reopens 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 with etcdutl 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.