Cluster Architecture, Installation & Configuration is 25% of the CKA. Besides building a cluster with kubeadm, the most common maintenance tasks fall into three groups: upgrading the cluster to the next version, backing up and restoring etcd, and granting permissions to users or ServiceAccounts with RBAC. As before, this post uses the three-node cluster (k8s-cp1, k8s-w1, k8s-w2) built in Building a kubeadm Cluster (Part 2).

⚠️ Take snapshots first

Upgrades and etcd restores are both operations that can break the cluster. Following the TIP at the end of the previous post, take snapshots of all three VMs first, so a failed practice run can simply be rolled back.


Upgrading the Cluster with kubeadm

The version rules

Kubernetes versions are major.minor.patch, for example 1.37.2. Two rules apply when upgrading:

  • Minor versions go up one at a time: to go from 1.35 to 1.37, you must first upgrade to 1.36, then to 1.37. Patch versions can be skipped, for example 1.37.0 straight to 1.37.3.
  • Components have version skew limits (version skew policy): kube-apiserver is the reference, and no other component may be newer than it.
Component Allowed range relative to kube-apiserver
kube-controller-manager, kube-scheduler Same, or 1 minor version older
kubelet, kube-proxy Same, or up to 3 minor versions older
kubectl Within 1 minor version newer or older

So the order is always control plane first, then workers: upgrade kube-apiserver first; a kubelet lagging one version behind is still within the allowed range, and then the workers are upgraded one at a time.

See what you can upgrade to

Run kubeadm upgrade plan on the control plane. It lists the cluster's current version and the versions you can upgrade to:

sudo kubeadm upgrade plan

Both the cluster and kubeadm are v1.35.10. kubeadm does find that the latest stable release is v1.37.2, but it can only upgrade the cluster within its own minor version, so it falls back to stable-1.35 and offers v1.35.10 at most. That is why every round starts by upgrading kubeadm first: only then does upgrade plan show the new minor version.

Upgrade overview

Step control plane (k8s-cp1) workers (k8s-w1, k8s-w2)
1 Switch the apt repo to the new minor version Same
2 Upgrade kubeadm Same
3 kubeadm upgrade plan → kubeadm upgrade apply kubeadm upgrade node
4 kubectl drain to evict the Pods Same (run on the control plane)
5 Upgrade kubelet and kubectl, restart kubelet Same
6 kubectl uncordon to allow scheduling again Same

Versions in this post

The cluster in the previous post was installed with 1.37 from the start. While writing this post I reinstalled it with 1.35, so below we go from 1.35 to 1.37. Since minor versions go up one at a time, it takes two rounds: 1.35 → 1.36, then 1.36 → 1.37. The first round is explained step by step; the second round is the exact same process, so it only lists the commands. To follow along, change v1.37 to v1.35 in the previous post and build a separate cluster.

💡 Check how kubeadm was installed

The steps below follow "Method 1: apt" from the previous post, which is also what the CKA exam environment uses. If you used "Method 2: Download from dl.k8s.io", /etc/apt/sources.list.d/kubernetes.list doesn't exist at all, and apt-get install kubeadm fails with Unable to locate package kubeadm. Check with which kubeadm: /usr/bin means apt, /usr/local/bin means you downloaded it yourself. For the latter, see Upgrading a cluster installed from binaries at the end.

Round 1: 1.35 → 1.36

1. Switch the apt repo (on every node)

pkgs.k8s.io has one repo per minor version, as mentioned during installation in the previous post. This is the line to change when upgrading:

sudo sed -i 's/v1.35/v1.36/' /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
apt-cache madison kubeadm    # list the versions available in this repo

The versions listed by apt-cache madison look like 1.36.6-1.1. Pick the one at the top (the latest) and use that string when installing below.

2. Upgrade kubeadm on the control plane

The versions were pinned with apt-mark hold during installation, so unhold first and hold again after upgrading:

sudo apt-mark unhold kubeadm
sudo apt-get install -y kubeadm='1.36.6-1.1'
sudo apt-mark hold kubeadm

kubeadm version    # confirm it is now v1.36.6

3. kubeadm upgrade plan and apply

sudo kubeadm upgrade plan

Now that kubeadm is 1.36, upgrade plan lists the 1.36 versions the control plane components, CoreDNS and etcd can be upgraded to, plus any component configs (such as the kubelet config) that need manual attention. Once everything looks fine, run the command it suggests at the end:

sudo kubeadm upgrade apply v1.36.6

upgrade apply does the following:

  1. Runs the preflight checks again, confirming the cluster is healthy and the version jump is valid.
  2. Regenerates the static Pod manifests under /etc/kubernetes/manifests/. kubelet notices the file changes and restarts kube-apiserver, kube-controller-manager, kube-scheduler and etcd with the new images.
  3. Updates CoreDNS and kube-proxy.
  4. Renews the control plane certificates along the way (the default; turn it off with --certificate-renewal=false).

When you see SUCCESS! A control plane node of your cluster was upgraded to "v1.36.6", it's done. Note that the CNI (Calico) is not upgraded by kubeadm; follow the CNI's own documentation for that.

upgrade apply backs things up automatically

Before replacing the static Pods, upgrade apply backs up the old files to /etc/kubernetes/tmp/, which you can see in the output:

For each component it replaces, you'll see Moving new manifest to "/etc/kubernetes/manifests/etcd.yaml" and backing up old manifest to "/etc/kubernetes/tmp/kubeadm-backup-manifests-<time>/etcd.yaml", and the same for kube-apiserver, kube-controller-manager and kube-scheduler.

There are two kinds of folders under /etc/kubernetes/tmp/:

Folder Contents
kubeadm-backup-manifests-<time> The static Pod manifests from before the upgrade
kubeadm-backup-etcd-<time> A copy of this node's etcd data directory (/var/lib/etcd) from before the upgrade
sudo ls /etc/kubernetes/tmp/

If the upgrade fails halfway, kubeadm tries to roll back automatically using these backups; if that doesn't work, you can move the backed-up manifests and etcd data back into place by hand. These folders are not cleaned up automatically, so delete them yourself once you're sure the upgrade went fine.

The E1109 ... request canceled (Client.Timeout ...) line in the middle of the screenshot is kube-apiserver failing to reach etcd during the few seconds etcd restarts. kubeadm retries, and once you see Component "etcd" upgraded successfully! afterwards, all is well.

💡 This is no substitute for an etcd snapshot

kubeadm's backup is just a copy of the data directory on the same machine: if the VM dies, it goes with it, and it only captures the state at upgrade time. Before upgrading, it's still a good idea to follow etcd Backup and Restore below and take a snapshot with etcdctl snapshot save, stored somewhere else.

With multiple control plane nodes

kubeadm upgrade apply runs only on the first control plane node. On the other control plane nodes, upgrade kubeadm the same way, then run sudo kubeadm upgrade node; there's no need to run upgrade plan again.

4. Drain the node

Upgrading kubelet affects the Pods on the node, so first mark the node unschedulable and evict its Pods:

kubectl drain k8s-cp1 --ignore-daemonsets
  • drain = cordon (mark it SchedulingDisabled so no new Pods are scheduled) + evict the Pods on the node.
  • --ignore-daemonsets: DaemonSet Pods (such as calico-node and kube-proxy) would be recreated immediately after eviction anyway, so they're skipped. Without this flag, drain fails.
  • If any Pod uses emptyDir, add --delete-emptydir-data to agree to deleting the temporary data in it.
  • Bare Pods not managed by a controller such as a Deployment or ReplicaSet won't come back once evicted, so drain refuses to run; add --force to delete them.

💡 When drain gets stuck

drain respects PodDisruptionBudgets. If a PDB requires minAvailable equal to the replica count, every eviction is refused and drain keeps retrying forever. Use kubectl get pdb -A to find which PDB it is.

5. Upgrade kubelet and kubectl

sudo apt-mark unhold kubelet kubectl
sudo apt-get install -y kubelet='1.36.6-1.1' kubectl='1.36.6-1.1'
sudo apt-mark hold kubelet kubectl

sudo systemctl daemon-reload
sudo systemctl restart kubelet

6. Uncordon

kubectl uncordon k8s-cp1
kubectl get nodes

The VERSION column in kubectl get nodes shows the kubelet version. At this point k8s-cp1 is v1.36.6 while both workers are still v1.35.10, which is within the allowed skew.

7. Upgrade the workers

Upgrade the workers one at a time, so you don't evict too many Pods at once. Taking k8s-w1 as the example, SSH into it and run:

# 1. Switch the repo (same as above)
sudo sed -i 's/v1.35/v1.36/' /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update

# 2. Upgrade kubeadm
sudo apt-mark unhold kubeadm
sudo apt-get install -y kubeadm='1.36.6-1.1'
sudo apt-mark hold kubeadm

# 3. Update this node's kubelet config
sudo kubeadm upgrade node

Then go back to the control plane (workers usually have no kubeconfig) and drain this worker:

kubectl drain k8s-w1 --ignore-daemonsets

Back on k8s-w1, upgrade kubelet:

sudo apt-mark unhold kubelet kubectl
sudo apt-get install -y kubelet='1.36.6-1.1' kubectl='1.36.6-1.1'
sudo apt-mark hold kubelet kubectl
sudo systemctl daemon-reload
sudo systemctl restart kubelet

Finally run kubectl uncordon k8s-w1 on the control plane, confirm it's Ready, and repeat for k8s-w2. Round 1 is only finished once all three nodes are v1.36.6. Then start round 2: handling one version at a time makes it easier to tell which step caused a problem.

round1-nodes

While you're at it, check etcd: kubeadm upgrade apply also switches etcd to the version paired with this Kubernetes release, and 1.36.6 uses registry.k8s.io/etcd:3.6.8-0. When backing up etcd later, match your etcdctl version to this.

Round 2: 1.36 → 1.37

The process is exactly the same as round 1, just with 1.37 as the version. Start on the control plane (k8s-cp1):

# 1. Switch the repo
sudo sed -i 's/v1.36/v1.37/' /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
apt-cache madison kubeadm

# 2. Upgrade kubeadm
sudo apt-mark unhold kubeadm
sudo apt-get install -y kubeadm='1.37.2-1.1'
sudo apt-mark hold kubeadm

# 3. plan and apply
sudo kubeadm upgrade plan
sudo kubeadm upgrade apply v1.37.2

# 4. drain
kubectl drain k8s-cp1 --ignore-daemonsets

# 5. Upgrade kubelet and kubectl
sudo apt-mark unhold kubelet kubectl
sudo apt-get install -y kubelet='1.37.2-1.1' kubectl='1.37.2-1.1'
sudo apt-mark hold kubelet kubectl
sudo systemctl daemon-reload
sudo systemctl restart kubelet

# 6. uncordon
kubectl uncordon k8s-cp1

Then upgrade the workers one at a time, with k8s-w1 as the example (drain / uncordon run on the control plane):

# On k8s-w1
sudo sed -i 's/v1.36/v1.37/' /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-mark unhold kubeadm
sudo apt-get install -y kubeadm='1.37.2-1.1'
sudo apt-mark hold kubeadm
sudo kubeadm upgrade node

# On k8s-cp1
kubectl drain k8s-w1 --ignore-daemonsets

# Back on k8s-w1
sudo apt-mark unhold kubelet kubectl
sudo apt-get install -y kubelet='1.37.2-1.1' kubectl='1.37.2-1.1'
sudo apt-mark hold kubelet kubectl
sudo systemctl daemon-reload
sudo systemctl restart kubelet

# On k8s-cp1
kubectl uncordon k8s-w1

Do the same for k8s-w2, then confirm all three nodes are upgraded:

kubectl get nodes
sudo kubeadm upgrade plan    # already the latest, so no upgrade targets are listed

When the VERSION column shows v1.37.2 for all three nodes, the upgrade is complete.

Patch upgrades

If the cluster is already on 1.37 and you only need to go from 1.37.0 to 1.37.2, the process is the same; just skip step 1, switching the apt repo.

Upgrading a cluster installed from binaries

If you installed from binaries downloaded from dl.k8s.io (method 2 in the previous post), there's no apt repo to switch and no apt-mark when upgrading. Instead, you "download the new binaries and replace the old ones in /usr/local/bin". The upgrade order and the kubeadm upgrade steps are exactly the same; only the installation method differs. The systemd units (kubelet.service, 10-kubeadm.conf) don't depend on the version, so there's no need to download them again.

Taking the control plane in round 1 (1.35 → 1.36) as the example:

ARCH="amd64"
K8S_MINOR="1.36"     # change to 1.37 for round 2
RELEASE=$(curl -sSL https://dl.k8s.io/release/stable-${K8S_MINOR}.txt)
echo ${RELEASE}      # e.g. v1.36.6

# 1. Download and replace kubeadm
cd /tmp
curl -L --remote-name-all https://dl.k8s.io/release/${RELEASE}/bin/linux/${ARCH}/kubeadm
sudo install -m 755 kubeadm /usr/local/bin/
kubeadm version -o short

# 2. plan and apply
sudo kubeadm upgrade plan
sudo kubeadm upgrade apply ${RELEASE}

# 3. drain
kubectl drain k8s-cp1 --ignore-daemonsets

# 4. Download and replace kubelet and kubectl
curl -L --remote-name-all https://dl.k8s.io/release/${RELEASE}/bin/linux/${ARCH}/{kubelet,kubectl}
sudo systemctl stop kubelet
sudo install -m 755 kubelet kubectl /usr/local/bin/
sudo systemctl daemon-reload
sudo systemctl start kubelet

# 5. uncordon
kubectl uncordon k8s-cp1

Workers work the same way: on the worker, download and replace kubeadm, then run sudo kubeadm upgrade node; drain it from the control plane; back on the worker, replace kubelet and kubectl and restart; finally uncordon it from the control plane. RELEASE is a shell variable, so set it again after SSHing into another machine.

crictl doesn't affect the upgrade. If you want it to match the minor version too, rerun the commands from the previous post with CRICTL_VERSION set to v1.36.0.

💡 In the exam, keep track of which machine you're on

Upgrade tasks in the exam have you SSHing back and forth between nodes. kubectl drain / uncordon must run on a machine with a kubeconfig, while apt-get, kubeadm upgrade node and systemctl must run on the node being upgraded. Remember to exit back to the original machine when done, or kubectl in the next task may not reach the cluster.


etcd Backup and Restore

Every object in the cluster (Deployments, Secrets, ConfigMaps, RBAC settings...) is stored in etcd. With an etcd snapshot, you can restore the cluster's state to the moment the snapshot was taken.

A kubeadm cluster uses stacked etcd: etcd runs on the same machine as the control plane, as a static Pod, with its data in /var/lib/etcd on the host.

Getting etcdctl and etcdutl

The exam environment has them installed, but kubeadm doesn't install them, so for your own lab cluster you need to download them. etcd has two tools:

Tool Purpose
etcdctl Connects to a running etcd over the network, e.g. snapshot save, querying members
etcdutl Works on the data files directly, no running etcd needed, e.g. snapshot restore, snapshot status

etcdctl snapshot restore was deprecated in etcd 3.5 and removed in 3.6, replaced by etcdutl. The version should ideally match the cluster's etcd, so first look it up from the static Pod's image:

kubectl -n kube-system get pod etcd-k8s-cp1 \
  -o jsonpath='{.spec.containers[0].image}{"\n"}'

The image tag is <etcd version>-<revision>, so registry.k8s.io/etcd:3.6.5-0 means etcd 3.6.5. Download that version from GitHub Releases:

ETCD_VER=v$(kubectl -n kube-system get pod etcd-k8s-cp1 \
  -o jsonpath='{.spec.containers[0].image}' | sed 's/.*://; s/-.*//')

cd /tmp
curl -L "https://github.com/etcd-io/etcd/releases/download/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz" | tar xz
sudo install etcd-${ETCD_VER}-linux-amd64/etcdctl etcd-${ETCD_VER}-linux-amd64/etcdutl /usr/local/bin/
etcdctl version

Backup: etcdctl snapshot save

kubeadm's etcd has TLS enabled, so connecting requires the CA and a client certificate. No need to memorize the paths; they're in the etcd manifest:

sudo grep -E 'listen-client-urls|cert-file|key-file|trusted-ca-file' \
  /etc/kubernetes/manifests/etcd.yaml

You'll see --listen-client-urls (including https://127.0.0.1:2379), --cert-file, --key-file and --trusted-ca-file, which map to etcdctl flags:

Flag in etcd.yaml etcdctl flag kubeadm default path
--listen-client-urls --endpoints https://127.0.0.1:2379
--trusted-ca-file --cacert /etc/kubernetes/pki/etcd/ca.crt
--cert-file --cert /etc/kubernetes/pki/etcd/server.crt
--key-file --key /etc/kubernetes/pki/etcd/server.key
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/etcd-backup.db

The certificate files are readable only by root, hence sudo. Afterwards, check the snapshot:

sudo etcdutl snapshot status /opt/etcd-backup.db -w table

It lists the snapshot's hash, revision, number of keys and size. If it displays fine, the snapshot file is intact.

💡 ETCDCTL_API=3 in older material

Lots of material prefixes commands with ETCDCTL_API=3. Since etcd 3.4 the v3 API is the default, and etcd 3.6 removed the variable altogether: setting it only adds an unrecognized environment variable warning (the snapshot is still saved), so just leave it out.

Restore: etcdutl snapshot restore

First make a change in the cluster, so you can tell afterwards whether the restore worked:

kubectl create deployment after-backup --image=nginx

The restore process is: stop kube-apiserver and etcd → restore the snapshot to a new data directory → point etcd at the new directory → start everything again.

1. Stop kube-apiserver and etcd

kubelet starts static Pods by watching /etc/kubernetes/manifests/. Move a manifest out, and its Pod is stopped:

sudo mkdir -p /root/manifests-backup
sudo mv /etc/kubernetes/manifests/kube-apiserver.yaml \
        /etc/kubernetes/manifests/etcd.yaml /root/manifests-backup/

Wait a few seconds and use sudo crictl ps | grep -E 'etcd|apiserver' to confirm both containers have stopped. kubectl can't connect at this point, which is expected.

2. Restore the snapshot to a new directory

sudo etcdutl snapshot restore /opt/etcd-backup.db --data-dir /var/lib/etcd-restore

--data-dir must be a directory that doesn't exist yet; etcdutl creates it. Overwriting /var/lib/etcd directly isn't recommended: if the snapshot turns out to be bad, the original data is still there.

3. Point etcd at the new data directory

Edit the etcd manifest you just moved out and find the hostPath named etcd-data under volumes:

sudo vim /root/manifests-backup/etcd.yaml
  volumes:
  - hostPath:
      path: /var/lib/etcd-restore   # was /var/lib/etcd
      type: DirectoryOrCreate
    name: etcd-data

Only the path on the host needs to change. The container's mountPath and --data-dir=/var/lib/etcd stay as they are: those are paths inside the container, which now map to the new directory on the host.

4. Put the manifests back

sudo mv /root/manifests-backup/etcd.yaml /root/manifests-backup/kube-apiserver.yaml \
        /etc/kubernetes/manifests/

kubelet starts etcd and then kube-apiserver. Wait a minute or two:

kubectl get pods -n kube-system
kubectl get deployment after-backup

after-backup was created after the backup, so after the restore it should return NotFound.

⚠️ Don't leave backup files in the manifests directory

kubelet starts every file in /etc/kubernetes/manifests/ as a static Pod, even ones without a .yaml extension. Leave an etcd.yaml.bak in there and you get two etcds fighting over the same port. Always keep manifest backups in a different directory.

In the exam you can just edit etcd.yaml

The snapshot is restored to a new directory and never touches the data etcd is using, so when time is short you can skip moving manifests: after the restore, edit /etc/kubernetes/manifests/etcd.yaml and point the etcd-data hostPath at the new directory (leave etcd-certs alone). kubelet recreates etcd on its own; meanwhile kube-apiserver can't reach etcd, restarts several times, and kubectl is refused for a while. After repeated failures kubelet waits longer before each retry (up to about 5 minutes), so if etcd is Running but kube-apiserver is still Exited, run systemctl restart kubelet to retry right away. On a real cluster, still stop the apiserver first as above so nothing is written during the switch.

The official docs also recommend restarting kube-controller-manager, kube-scheduler and every node's kubelet after a restore, so none of them hold on to pre-restore state. Restart kubelet with systemctl restart kubelet; restart controller-manager and scheduler the same "move the manifest out and back in" way.

With multiple etcd members

There's only one etcd member here, so the defaults are fine for the restore. With multiple members, every member must be restored from the same snapshot, each with its own --name, --initial-cluster and --initial-advertise-peer-urls, so they form a new etcd cluster.

Common exam variations

  • The task gives an endpoint and certificate paths: use the paths from the task, not the defaults. The task may also ask you to use another certificate such as /etc/kubernetes/pki/etcd/healthcheck-client.crt.
  • The task specifies the restored data directory: for example, restore to /var/lib/etcd-from-backup; then both --data-dir and the hostPath use that path.
  • etcd isn't on the local machine: SSH to the node running etcd first, then run etcdctl.
  • etcd runs as a systemd service (external etcd): check with systemctl status etcd first. If it does, stop it with systemctl stop etcd, point --data-dir in the unit file (e.g. /etc/systemd/system/etcd.service) at the restored directory, then run systemctl daemon-reload and systemctl start etcd. A default kubeadm cluster runs etcd and kube-apiserver as static Pods with no such service, so systemctl can't stop them.

Highly Available Control Plane

The cluster from the previous post has only one control plane node. If k8s-cp1 goes down, the cluster can no longer accept any changes (Pods already running are unaffected, but nothing can schedule or repair them). Production environments use multiple control plane nodes for high availability. The CKA is unlikely to ask you to build an HA cluster, but you should know the architecture.

etcd quorum

etcd uses the Raft algorithm to stay consistent: a write needs agreement from more than half of the members (quorum). With n members:

quorum=⌊n2⌋+1tolerated failures=n−quorum=⌊n−12⌋\text{quorum} = \left\lfloor \frac{n}{2} \right\rfloor + 1 \qquad \text{tolerated failures} = n - \text{quorum} = \left\lfloor \frac{n-1}{2} \right\rfloor

⌊ ⌋\lfloor\ \rfloor means rounding down. For example, with n=3n = 3, quorum=⌊1.5⌋+1=2\text{quorum} = \lfloor 1.5 \rfloor + 1 = 2, so at most 1 member can fail. Plugging in different member counts:

Members Quorum Failures tolerated
1 1 0
2 2 0
3 2 1
4 3 1
5 3 2

2 members tolerate no failures, just like 1, and 4 tolerate only 1, just like 3. An even count adds no fault tolerance, only one more machine that can fail. So etcd always uses an odd number of members, typically 3 or 5.

Stacked etcd vs. external etcd

Topology Description Pros Cons
stacked etcd etcd runs on every control plane node (kubeadm default) Fewer machines, simple setup Losing a control plane node also loses an etcd member
external etcd etcd runs on its own set of machines, separate from the control plane Control plane and etcd failures don't affect each other At least 3 extra machines, and more complex to operate

A load balancer in front of the API server

With multiple kube-apiservers, which one do kubelet and kubectl connect to? The answer is a load balancer in front, and everyone connects to its address. Common options are HAProxy + keepalived (providing a floating VIP), or kube-vip running as a static Pod on the control plane nodes.

When building an HA cluster with kubeadm, the difference is specifying the load balancer address at init:

# First control plane node
sudo kubeadm init --control-plane-endpoint "k8s-api.lab:6443" \
  --upload-certs --pod-network-cidr=10.244.0.0/16
  • --control-plane-endpoint: the address written into every kubeconfig and the API server certificate. The previous post didn't set it, so the kubeconfig points directly at k8s-cp1's IP, and moving to HA later means regenerating certificates. In production, even if you start with a single node, it's best to use a DNS name from the beginning.
  • --upload-certs: encrypts the control plane certificates and stores them temporarily in a Secret in the cluster. Other control plane nodes download them with --certificate-key when joining, so there's no need to copy /etc/kubernetes/pki by hand.

Other control plane nodes join with two extra flags:

sudo kubeadm join k8s-api.lab:6443 --token <token> \
  --discovery-token-ca-cert-hash sha256:<hash> \
  --control-plane --certificate-key <key>

Certificate Expiry and Renewal

Certificates generated by kubeadm are valid for 1 year, and the CA for 10 years. Once certificates expire, kubectl and the components can no longer talk to each other. Check the expiry dates:

sudo kubeadm certs check-expiration

As mentioned earlier, kubeadm upgrade apply renews certificates by default, so a cluster upgraded at least once a year usually never hits expiry. To renew manually:

sudo kubeadm certs renew all

After renewal, the control plane components don't load the new certificates automatically. Restart kube-apiserver, kube-controller-manager, kube-scheduler and etcd (again by moving the manifests out and back in). The certificate in /etc/kubernetes/admin.conf is renewed too, so remember to copy it to ~/.kube/config again.


RBAC: Who Can Do What to Which Resources

Authentication and authorization

When kube-apiserver receives a request, it first performs authentication (who are you) and then authorization (what are you allowed to do). The default authorization mode of a kubeadm cluster is Node,RBAC, visible in --authorization-mode in the kube-apiserver manifest.

Kubernetes has two kinds of identities:

Identity Is it a Kubernetes object? How it's created Used by
User No, Kubernetes has no User object Decided externally, e.g. the CN field of a client certificate People
ServiceAccount Yes, it belongs to a namespace kubectl create serviceaccount Programs running in Pods

A client certificate's CN is used as the user name, and O as the group.

The four RBAC objects

Object Scope Purpose
Role Namespace Defines which operations are allowed in this namespace
ClusterRole Cluster Defines operations; can cover cluster-scoped resources (Node, PV), or serve as a template shared by multiple namespaces
RoleBinding Namespace Binds a Role or ClusterRole to users / groups / ServiceAccounts, effective only in this namespace
ClusterRoleBinding Cluster Binds a ClusterRole to subjects, effective in all namespaces

Combined, there are three patterns:

  • Role + RoleBinding: grant permissions in a single namespace.
  • ClusterRole + ClusterRoleBinding: grant permissions across the whole cluster.
  • ClusterRole + RoleBinding: define permissions once in a ClusterRole and bind it separately in each namespace; the permissions apply only in the RoleBinding's namespace. The built-in view, edit and admin are usually used this way.

RBAC only has "allow", no "deny"; your permissions are the union of all your bindings.

Creating a Role and RoleBinding

Let user jane read Pods in the dev namespace:

kubectl create namespace dev
kubectl create role pod-reader -n dev \
  --verb=get,list,watch --resource=pods
kubectl create rolebinding jane-pod-reader -n dev \
  --role=pod-reader --user=jane

The resulting Role looks like this:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pod-reader
  namespace: dev
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]
  • apiGroups: the core API (Pod, Service, ConfigMap, Secret) is the empty string ""; Deployment and ReplicaSet belong to apps. kubectl api-resources shows which API group each resource belongs to.
  • resources: lowercase plural, such as pods or deployments. Subresources use a slash: pods/log is needed for kubectl logs, and pods/exec for kubectl exec.
  • verbs: get, list, watch, create, update, patch, delete; * means all of them.

Verifying with kubectl auth can-i

Admins can impersonate other identities with --as, without needing their certificates:

kubectl auth can-i list pods -n dev --as jane          # yes
kubectl auth can-i delete pods -n dev --as jane        # no
kubectl auth can-i list pods -n default --as jane      # no, the RoleBinding only applies in dev
kubectl auth can-i --list -n dev --as jane             # list everything jane can do in dev

After finishing an RBAC task in the exam, a quick auth can-i check is the fastest way to confirm it.

💡 A RoleBinding's roleRef can't be changed

Once a RoleBinding is created, its roleRef (which Role it binds) can't be modified, and kubectl edit will be rejected. To switch Roles, delete and recreate it; subjects, on the other hand, can be edited directly.

Issuing a certificate and kubeconfig for a user

The jane above is just a name. For her to actually log in, she needs a client certificate with CN jane. Kubernetes has a built-in CertificateSigningRequest (CSR) API that signs with the cluster's CA:

# 1. Generate a private key and CSR; CN is the user name, O is the group
openssl genrsa -out jane.key 2048
openssl req -new -key jane.key -out jane.csr -subj "/CN=jane/O=dev-team"

# 2. Submit the CSR to the cluster
cat <<EOF | kubectl apply -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
  name: jane
spec:
  request: $(base64 -w0 jane.csr)
  signerName: kubernetes.io/kube-apiserver-client
  expirationSeconds: 86400
  usages:
  - client auth
EOF

# 3. Approve it and extract the signed certificate
kubectl certificate approve jane
kubectl get csr jane -o jsonpath='{.status.certificate}' | base64 -d > jane.crt
  • signerName: kubernetes.io/kube-apiserver-client means the certificate is a client certificate for authenticating to the API server.
  • expirationSeconds is the validity period, here 1 day.
  • kubectl get csr shows the status going from Pending to Approved,Issued.

Then add the certificate to the kubeconfig:

kubectl config set-credentials jane \
  --client-certificate=jane.crt --client-key=jane.key --embed-certs=true
kubectl config set-context jane@kubernetes \
  --cluster=kubernetes --user=jane --namespace=dev

kubectl --context jane@kubernetes get pods            # works
kubectl --context jane@kubernetes get pods -n default # Forbidden

In a kubeadm-generated kubeconfig, the cluster is named kubernetes; confirm with kubectl config get-clusters. --embed-certs=true writes the certificate contents straight into the kubeconfig, so you don't need to carry the .crt and .key around when moving the file.

admin.conf no longer relies on system:masters

The system:masters group bypasses all authorization checks and can't be revoked through RBAC. Since 1.29, kubeadm puts admin.conf in the kubeadm:cluster-admins group instead, which gets cluster-admin through a ClusterRoleBinding that can be revoked if needed. Only super-admin.conf is still in system:masters; don't use it day to day.

ServiceAccount

When programs in Pods need to call the Kubernetes API (CI tools, Operators, monitoring), they use a ServiceAccount. Every namespace has an automatically created default ServiceAccount, used when a Pod doesn't specify one, and by default it has no RBAC permissions.

Create a ServiceAccount and give it read-only access to the dev namespace:

kubectl create serviceaccount app-sa -n dev
kubectl create rolebinding app-sa-view -n dev \
  --clusterrole=view --serviceaccount=dev:app-sa

The format of --serviceaccount is <namespace>:<name>. When verifying with auth can-i, a ServiceAccount's user name is system:serviceaccount:<namespace>:<name>:

kubectl auth can-i list pods -n dev --as system:serviceaccount:dev:app-sa   # yes
kubectl auth can-i create pods -n dev --as system:serviceaccount:dev:app-sa # no

Specify it in a Pod with serviceAccountName:

apiVersion: v1
kind: Pod
metadata:
  name: app
  namespace: dev
spec:
  serviceAccountName: app-sa
  containers:
    - name: app
      image: nginx

kubelet mounts the token into the container at /var/run/secrets/kubernetes.io/serviceaccount/, which holds three files: token, ca.crt and namespace. Kubernetes client libraries read them automatically.

  • The token is a time-limited projected token that kubelet renews automatically before it expires. Since 1.24, creating a ServiceAccount no longer generates a permanent token Secret.
  • To use a token outside the cluster (e.g. for CI), generate a short-lived one with kubectl create token app-sa -n dev --duration=1h.
  • Pods that don't need to call the API can set automountServiceAccountToken: false to skip mounting the token.

💡 A Pod's serviceAccountName can't be changed in place

A Pod's serviceAccountName can't be modified after creation. When a task asks you to "make a Deployment use a certain ServiceAccount", change the Deployment's Pod template (kubectl set serviceaccount deployment <name> <sa>) and let the rolling update create new Pods.

Built-in ClusterRoles

ClusterRole Permissions
cluster-admin Every operation on every resource
admin Almost every resource in the namespace, including creating Roles and RoleBindings, but not the namespace itself or ResourceQuotas
edit Read/write on most resources in the namespace; can't view or modify Roles or RoleBindings
view Read-only on most resources in the namespace; can't see Secrets

When the required permissions match one of these, using a built-in ClusterRole such as --clusterrole=view with a RoleBinding is faster than writing your own Role, and less likely to miss something.


Summary

  • Minor versions go up one at a time when upgrading, control plane first and then workers; kubelet can be up to 3 minor versions older than kube-apiserver, but never newer.
  • Control plane: switch the repo → upgrade kubeadm → kubeadm upgrade plan / apply → drain → upgrade kubelet and kubectl → uncordon. Workers replace upgrade apply with kubeadm upgrade node.
  • Back up etcd with etcdctl snapshot save, taking the connection flags from /etc/kubernetes/manifests/etcd.yaml; restore with etcdutl snapshot restore into a new directory, then point the etcd manifest's hostPath at it.
  • etcd uses an odd number of members, and 3 tolerate 1 failure; an HA control plane needs the load balancer set with --control-plane-endpoint at kubeadm init.
  • kubeadm certificates are valid for 1 year; check with kubeadm certs check-expiration, and upgrades renew them automatically.
  • RBAC: Roles / ClusterRoles define permissions, RoleBindings / ClusterRoleBindings bind them to Users, Groups or ServiceAccounts; Users come from the certificate CN and can be issued through the CSR API; verify with kubectl auth can-i --as when done.

References

  1. Kubernetes - Upgrading kubeadm clusterskubernetes.io
  2. Kubernetes - Version Skew Policykubernetes.io
  3. Kubernetes - Safely Drain a Nodekubernetes.io
  4. Kubernetes - Operating etcd clusters for Kuberneteskubernetes.io
  5. etcd - Disaster recoveryetcd.io
  6. Kubernetes - Options for Highly Available Topologykubernetes.io
  7. Kubernetes - Creating Highly Available Clusters with kubeadmkubernetes.io
  8. Kubernetes - Certificate Management with kubeadmkubernetes.io
  9. Kubernetes - Using RBAC Authorizationkubernetes.io
  10. Kubernetes - Certificates and Certificate Signing Requestskubernetes.io
  11. Kubernetes - Configure Service Accounts for Podskubernetes.io