All exams← All Certified Kubernetes Administrator (CKA) notes

Cluster Maintenance & Upgrades: Certified Kubernetes Administrator (CKA) revision notes

Revision notes

## Cluster Maintenance & Upgrades

Maintaining a healthy Kubernetes cluster involves regular upgrades and robust backup/restore strategies. The CKA exam focuses on manual upgrades using `kubeadm` and `etcd` snapshot management.

## Upgrading Kubernetes Clusters

Manual upgrades typically involve updating the `kubeadm`, `kubelet`, and `kubectl` binaries. It's crucial to upgrade these components to the same minor version across the cluster to ensure compatibility.

Upgrade Process for Control Plane Nodes:

1. Backup etcd: This is the most critical step before any upgrade. See the backup section below.

2. Drain the node: Prepare the control plane node for maintenance by evicting all pods: `kubectl drain <node-name> --ignore-daemonsets`.

3. Upgrade kubeadm: Update the `kubeadm` package to the target version (e.g., `apt-get install -y kubeadm=1.28.x-00` or `yum install -y kubeadm-1.28.x`).

4. Plan the upgrade: Check for potential issues: `kubeadm upgrade plan`.

5. Apply the upgrade: Execute the upgrade on the control plane: `kubeadm upgrade apply v1.28.x`.

6. Upgrade kubelet and kubectl: Update the `kubelet` and `kubectl` packages to the same target version (e.g., `apt-get install -y kubelet=1.28.x-00 kubectl=1.28.x-00`).

7. Restart kubelet: `systemctl restart kubelet`.

8. Uncordon the node: Allow pods to be scheduled again: `kubectl uncordon <node-name>`.

Upgrade Process for Worker Nodes:

1. Drain the node: `kubectl drain <node-name> --ignore-daemonsets`.

2. Upgrade kubeadm: Update the `kubeadm` package.

3. Run kubeadm upgrade node: This command updates the worker node's configuration: `kubeadm upgrade node`.

4. Upgrade kubelet and kubectl: Update the `kubelet` and `kubectl` packages.

5. Restart kubelet: `systemctl restart kubelet`.

6. Uncordon the node: `kubectl uncordon <node-name>`.

Repeat for all worker nodes.

## Backup and Restore Operations

The etcd data store holds the entire state of your Kubernetes cluster. Backing it up is paramount for disaster recovery.

Backup etcd:

Use the `etcdctl snapshot save` command. You'll need to specify the API version, etcd endpoints, and TLS certificates if etcd is secured (which it typically is in a production cluster).

```bash

# Ensure ETCDCTL_API is set to 3

export ETCDCTL_API=3

# Example command (adjust paths for your setup)

etcdctl snapshot save /tmp/etcd-snapshot.db \

--endpoints=https://127.0.0.1:2379 \

--cacert=/etc/kubernetes/pki/etcd/ca.crt \

--cert=/etc/kubernetes/pki/etcd/peer.crt \

--key=/etc/kubernetes/pki/etcd/peer.key

```

Restore etcd:

Restoring etcd involves stopping the Kubernetes control plane components, restoring the snapshot, and then restarting the components.

1. Stop Kubernetes control plane components: On the control plane node, stop the `kube-apiserver`, `kube-scheduler`, and `kube-controller-manager` static pods. This often involves moving their manifest files out of `/etc/kubernetes/manifests/` or stopping the `kubelet` service.

2. Restore the snapshot: Create a *new* data directory for etcd and restore the snapshot into it.

```bash

export ETCDCTL_API=3

etcdctl snapshot restore /tmp/etcd-snapshot.db \

--data-dir=/var/lib/etcd-restored-data

```

3. Update etcd static pod manifest: Modify the `etcd.yaml` manifest file (usually `/etc/kubernetes/manifests/etcd.yaml`) to point to the new `--data-dir` (`/var/lib/etcd-restored-data` in the example).

4. Restart Kubernetes control plane components: Move the manifest files back or restart the `kubelet` service. Verify cluster health.

Always ensure you have backups of your Kubernetes configuration files (`/etc/kubernetes/`) and certificates (`/etc/kubernetes/pki/`) as well.

Practice questions

Cluster Maintenance & Upgrades: 5 practice questions

Q1. You need to perform maintenance on a worker node named 'node-01'. Which command should you use to safely evict all user pods from the node before shutting it down?
  • A.  kubectl cordon node-01
  • B.  kubectl stop node node-01
  • C.  kubectl drain node-01 --ignore-daemonsets
  • D.  kubectl delete node node-01
Show answer & explanation
✓ Answer: C. kubectl drain node-01 --ignore-daemonsets
`kubectl drain` first cordons the node to prevent new pods from being scheduled, then it evicts existing pods. `cordon` only prevents new pods, and `delete node` is a destructive action.
Q2. When upgrading a Kubernetes cluster created with `kubeadm`, what is the very first command you should run on the primary control-plane node to upgrade it from version v1.28.0 to v1.28.1?
  • A.  kubeadm upgrade plan
  • B.  kubeadm upgrade apply v1.28.1
  • C.  apt-get install -y kubeadm=1.28.1-00
  • D.  kubeadm upgrade node
Show answer & explanation
✓ Answer: C. apt-get install -y kubeadm=1.28.1-00
The `kubeadm` tool itself must be upgraded first via the system's package manager (like apt or yum) before it can be used to upgrade the cluster's components.
Q3. You are tasked with backing up the etcd database for a cluster that uses a stacked etcd topology. Assuming `etcdctl` is configured with the correct certificates, which command correctly creates a snapshot?
  • A.  etcdctl backup create /tmp/etcd-snapshot.db
  • B.  etcdctl backup --data-dir /var/lib/etcd --backup-dir /tmp/backup
  • C.  etcdctl snapshot save /tmp/etcd-snapshot.db
  • D.  etcdctl snapshot export /tmp/etcd-snapshot.db
Show answer & explanation
✓ Answer: C. etcdctl snapshot save /tmp/etcd-snapshot.db
The `etcdctl snapshot save` command is the standard and correct way to create a portable point-in-time backup of the etcd key-value store. The other commands use incorrect syntax or are for different purposes.
Q4. After successfully running `kubeadm upgrade apply` on a control-plane node, what are the next two components you must manually upgrade on that same node?
  • A.  kube-proxy and CoreDNS
  • B.  kubelet and kubectl
  • C.  etcd and containerd
  • D.  kube-apiserver and kube-scheduler
Show answer & explanation
✓ Answer: B. kubelet and kubectl
The `kubeadm upgrade apply` command does not upgrade the kubelet or kubectl. These must be upgraded separately using the system's package manager (e.g., apt-get, yum) on every node in the cluster.
Q5. What is the primary effect of running the command `kubectl cordon my-worker-node`?
  • A.  It marks the node as unschedulable, preventing new pods from being assigned to it.
  • B.  It removes the node object from the API server.
  • C.  It safely evicts all pods and marks the node as unschedulable.
  • D.  It immediately terminates all pods running on `my-worker-node`.
Show answer & explanation
✓ Answer: A. It marks the node as unschedulable, preventing new pods from being assigned to it.
`kubectl cordon` marks a node as unschedulable by adding the `NoSchedule` taint. It does not affect any pods that are already running on the node. To evict pods, you must use `kubectl drain`.
Test yourself
See the Certified Kubernetes Administrator (CKA) page

More Certified Kubernetes Administrator (CKA) topics