## 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.