Skip to content
Kubernetes day 2 · Chapter 14 of 20 Day 12: Replacing a control plane node

Day 12: Replacing a control plane node

Goal: Take a control plane node and its etcd member cleanly out of the cluster and add it back as a new node, without downtime for workloads and the API · Duration: approx. 75 min · Prerequisite: Day 11

Briefing

  • Hardware breaks, VMs get rebuilt, operating systems get switched. In those cases you do not repair a node, you replace it: out of the cluster, back in fresh.
  • A control plane node carries more than a worker: an etcd member, an API server, and in the lab also a share of the virtual address (kube-vip).
  • The most dangerous phase is the gap. With three etcd members, one may fail. While the replacement runs, there are only two, and the cluster tolerates no further failure.
  • Today you replace cp3 with “new hardware”. In the lab it is the same VM, which after kubeadm reset is as empty as a new one.
  drain cp3 ─▶ kubeadm reset (etcd member gone) ─▶ delete Node
      ─▶ upload certificates + token ─▶ kubeadm join --control-plane ─▶ follow-up work

Drill 1: Taking stock

The measurements run as they did yesterday: last against shop, API probe on cp2.

mkdir -p ~/day2/tag12 && cd ~/day2/tag12
kubectl -n demo delete pod last
sed "s#http://web.demo#http://shop.demo#" ~/day2/tag00/last.yaml | kubectl apply -f -
kubectl -n demo wait --for=condition=Ready pod/last
kubectl -n kube-system get lease plndr-cp-lock -o jsonpath='{.spec.holderIdentity}{"\n"}'
kubectl get pods -A -o wide --field-selector spec.nodeName=cp3 | cut -c1-60

Expected (abridged):

cp2
NAMESPACE     NAME                          READY   STATUS    RESTARTS
demo          web-75d549d885-bmcxf          1/1     Running   1
kube-system   cilium-envoy-78g7t            1/1     Running   0
kube-system   cilium-x9cpq                  1/1     Running   0
kube-system   coredns-559f6c778d-llbl7      1/1     Running   0
kube-system   etcd-cp3                      1/1     Running   1 (7h26m ago)
kube-system   kube-apiserver-cp3            1/1     Running   1 (7h26m ago)
...
kube-system   kube-vip-cp3                  1/1     Running   1 (15h ago)

The virtual address is on cp2, which is good. If it were on cp3, you would first hand it over in an orderly way, as on Day 6.

One file does not survive kubeadm reset and does not belong to kubeadm: the kube-vip manifest. Back it up:

# on cp3
mkdir -p ~/day2/tag12
sudo cp /etc/kubernetes/manifests/kube-vip.yaml ~/day2/tag12/kube-vip.yaml
sudo chown $USER ~/day2/tag12/kube-vip.yaml

And thanks to Day 11, a fresh etcd snapshot is there anyway:

sudo ls /var/backups/k8s/etcd/ | tail -1

Save your progress? Sign up for free

Drill 2: Out with the workloads

kubectl drain cp3 --ignore-daemonsets --delete-emptydir-data --timeout=180s | tail -3

Expected:

pod/web-75d549d885-bmcxf evicted
pod/coredns-559f6c778d-llbl7 evicted
node/cp3 drained

Eight seconds, and thanks to the PDBs and readiness probes from Day 3, without errors.

Save your progress? Sign up for free

Drill 3: Resetting the node

Warning: kubeadm reset deletes etcd data, certificates and all manifests on the node. From now on, etcd has only two members.

# on cp3
sudo kubeadm reset -f

Expected (abridged):

[reset] Reading configuration from the "kubeadm-config" ConfigMap in namespace "kube-system"...
[preflight] Running pre-flight checks
[reset] Deleted contents of the etcd data directory: /var/lib/etcd
[reset] Stopping the kubelet service
[reset] Unmounting mounted directories in "/var/lib/kubelet"
E1122 16:30:43.743974   86743 remote_runtime.go:355] "StopPodSandbox from runtime service failed" err="rpc error: code = DeadlineExceeded ...
...
W1122 16:30:51.754190   86743 cleanupnode.go:105] [reset] Failed to remove containers: ...
[reset] Deleting contents of directories: [/etc/kubernetes/manifests /var/lib/kubelet /etc/kubernetes/pki]
[reset] Deleting files: [/etc/kubernetes/admin.conf /etc/kubernetes/super-admin.conf ...]
The reset process does not perform cleanup of CNI plugin configuration,
network filtering rules and kubeconfig files.

The StopPodSandbox errors look alarming. A Pod sandbox did not respond in time, and reset carries on anyway. What matters is etcd:

sudo -E ETCDCTL_ENDPOINTS=https://172.16.0.126:2379 etcdctl member list -w table
sudo -E etcdctl endpoint health -w table 2>&1 | tail -5

Expected:

+------------------+---------+------+---------------------------+---------------------------+------------+
|        ID        | STATUS  | NAME |        PEER ADDRS         |       CLIENT ADDRS        | IS LEARNER |
+------------------+---------+------+---------------------------+---------------------------+------------+
| 33764b6962314e3f | started |  cp1 | https://172.16.0.126:2380 | https://172.16.0.126:2379 |      false |
| 6292e5989e4c860b | started |  cp2 | https://172.16.0.228:2380 | https://172.16.0.228:2379 |      false |
+------------------+---------+------+---------------------------+---------------------------+------------+
| https://172.16.0.126:2379 |   true |  42.376051ms |                           |
| https://172.16.0.228:2379 |   true |  58.660385ms |                           |
|  https://172.16.0.83:2379 |  false | 5.001356785s | context deadline exceeded |
+---------------------------+--------+--------------+---------------------------+
Error: unhealthy cluster

kubeadm reset removed cp3’s etcd member from the cluster first. Two members, both healthy. unhealthy cluster only shows up because the environment variable still contains cp3’s old address.

If reset did not run, or the old node is dead, the member stays behind as a corpse. Then you remove it by hand with etcdctl member remove <ID>. Otherwise etcd keeps counting on three members, and a second failure costs the quorum.

Clean up what reset leaves behind:

# on cp3
sudo rm -f /etc/cni/net.d/05-cilium.conflist

And the Node object:

kubectl delete node cp3

Expected:

node "cp3" deleted

Save your progress? Sign up for free

Drill 4: The admission ticket

A new control plane node needs two things: a token to join, and the shared certificates (CAs, service account keys). kubeadm uploads the certificates encrypted into a Secret, for two hours:

sudo kubeadm init phase upload-certs --upload-certs | tail -1 > ~/day2/tag12/certkey.txt
kubeadm token create --print-join-command > ~/day2/tag12/join.txt
wc -c ~/day2/tag12/certkey.txt ~/day2/tag12/join.txt | head -2

Expected:

 65 /home/ubuntu/day2/tag12/certkey.txt
167 /home/ubuntu/day2/tag12/join.txt

Both are secrets: whoever has them can bring a node with full privileges into your cluster. They go into files, not into the terminal output or the shell history, and you delete them after the join.

Save your progress? Sign up for free

Drill 5: Joining again

Copy the contents of the two files to cp3 (for example with scp), then:

# on cp3
J=$(cat ~/day2/tag12/join.txt); K=$(cat ~/day2/tag12/certkey.txt)
date +%T
sudo $J --control-plane --certificate-key $K --apiserver-advertise-address 172.16.0.83 \
  | grep -E '^\[etcd\]|^\[control-plane-check\]|This node|^\*'
date +%T

Expected:

16:31:33
[etcd] Announced new etcd member joining to the existing etcd cluster
[etcd] Creating static Pod manifest for "etcd"
[etcd] Waiting for the new etcd member to join the cluster. This can take up to 40s
[control-plane-check] Waiting for healthy control plane components. This can take up to 4m0s
[control-plane-check] Checking kube-apiserver at https://172.16.0.83:6443/livez
[control-plane-check] Checking kube-controller-manager at https://127.0.0.1:10257/healthz
[control-plane-check] Checking kube-scheduler at https://127.0.0.1:10259/livez
[control-plane-check] kube-controller-manager is healthy after 44.33794ms
[control-plane-check] kube-scheduler is healthy after 103.436608ms
[control-plane-check] kube-apiserver is healthy after 142.651558ms
This node has joined the cluster and a new control plane instance was created:
* Certificate signing request was sent to apiserver and approval was received.
* The Kubelet was informed of the new secure connection details.
* Control plane label and taint were applied to the new node.
* The Kubernetes control plane instances scaled up.
* A new etcd member was added to the local/stacked etcd cluster.
16:32:13

40 seconds. Right after that, put the kube-vip manifest back so that cp3 takes part in the virtual address again. Before the join it would have been in the way: kubeadm requires an empty manifest directory.

# on cp3
sudo cp ~/day2/tag12/kube-vip.yaml /etc/kubernetes/manifests/kube-vip.yaml
rm ~/day2/tag12/join.txt ~/day2/tag12/certkey.txt

Save your progress? Sign up for free

Drill 6: The follow-up work

The new node is there, but not yet like the others:

sudo -E ETCDCTL_ENDPOINTS=https://172.16.0.126:2379 etcdctl member list -w table
kubectl describe node cp3 | grep Taints
kubectl get csr | grep -E 'NAME|Pending'

Expected:

+------------------+---------+------+---------------------------+---------------------------+------------+
|        ID        | STATUS  | NAME |        PEER ADDRS         |       CLIENT ADDRS        | IS LEARNER |
+------------------+---------+------+---------------------------+---------------------------+------------+
| 33764b6962314e3f | started |  cp1 | https://172.16.0.126:2380 | https://172.16.0.126:2379 |      false |
| 6292e5989e4c860b | started |  cp2 | https://172.16.0.228:2380 | https://172.16.0.228:2379 |      false |
| c7611dce3d40328b | started |  cp3 |  https://172.16.0.83:2380 |  https://172.16.0.83:2379 |      false |
+------------------+---------+------+---------------------------+---------------------------+------------+
Taints:             node-role.kubernetes.io/control-plane:NoSchedule
NAME        AGE   SIGNERNAME                      REQUESTOR         REQUESTEDDURATION   CONDITION
csr-lmdfs   32s   kubernetes.io/kubelet-serving   system:node:cp3   <none>              Pending

Three members, cp3 with a new ID. Two things from earlier days catch up with you:

  1. The control plane taint is back. On Day 0 you removed it so that all three nodes carry workloads. kubeadm sets it again on every join.
  2. The kubelet wants a serving certificate, because of serverTLSBootstrap from Day 10. The request waits for your review.
kubectl taint node cp3 node-role.kubernetes.io/control-plane:NoSchedule-
kubectl get csr csr-lmdfs -o jsonpath='{.spec.request}' | base64 -d | openssl req -noout -text \
  | grep DNS:
kubectl certificate approve csr-lmdfs

Expected:

node/cp3 untainted
                    DNS:cp3, IP Address:172.16.0.83
certificatesigningrequest.certificates.k8s.io/csr-lmdfs approved

Finally, spread the workloads again. A new rollout is enough:

kubectl -n demo rollout restart deploy/web
kubectl -n demo rollout status deploy/web --timeout=120s | tail -1
kubectl -n demo get pods -l app=web -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName

Expected (abridged):

deployment "web" successfully rolled out
NAME                   NODE
web-7fd857fd9c-hsqtw   cp3
web-7fd857fd9c-sm5bv   cp1
web-7fd857fd9c-w45wg   cp2

The balance:

kubectl -n demo logs last | wc -l
kubectl -n demo logs last | awk '$2 != 200' | wc -l
# on cp2
pkill -f readyz
wc -l < ~/day2/tag11/api.log
awk '$2 != 200' ~/day2/tag11/api.log | wc -l

Expected:

859
0
220
0

0 errors in 859 requests to shop and 220 API checks. A control plane node and its etcd member replaced, and nobody noticed.

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Show the IDs and names of all etcd members. Target time: 1 min
  2. Generate a join command for a worker. Target time: 30 s
  3. Show which nodes carry a taint. Target time: 1 min

Solution

sudo -E ETCDCTL_ENDPOINTS=https://172.16.0.126:2379 etcdctl member list -w table
kubeadm token create --print-join-command > /tmp/worker-join.txt
kubectl get nodes -o custom-columns='NODE:.metadata.name,TAINTS:.spec.taints[*].key'

A worker only needs the token, no certificate key.

Save your progress? Sign up for free

Checkpoint

sudo -E ETCDCTL_ENDPOINTS=https://172.16.0.126:2379 etcdctl member list | wc -l

Expected: 3

kubectl get nodes -o custom-columns='NODE:.metadata.name,TAINTS:.spec.taints[*].key'

Expected: none of the three nodes has a taint.

Challenge

What if cp3 had been unreachable and kubeadm reset had never run? Write down the steps you would use to remove the dead member, and check them against today’s output.

Hint: What ID did cp3 have before the replacement?

Solution

cp3’s old ID was in the table on Day 11: cf664b54c8c1c3e0.

sudo -E ETCDCTL_ENDPOINTS=https://172.16.0.126:2379 etcdctl member remove cf664b54c8c1c3e0
kubectl delete node cp3

Then continue with Drill 4. Today, member remove reports an error, because reset has already removed the member:

Error: etcdserver: member not found

Remember the order: first remove the etcd member, then let a new node join. The other way round, etcd would briefly have four members and would need three for the quorum.

Save your progress? Sign up for free

Cleanup

Done already: token and certificate key deleted, taint removed, request approved. The uploaded certificate Secret expires on its own after two hours.

Interview Note

  • How do you replace a control plane node in a kubeadm cluster? Drain, kubeadm reset (or etcdctl member remove if the node is dead), delete the Node, upload the certificates with kubeadm init phase upload-certs, add it back with kubeadm join --control-plane.
  • Why is the phase during the replacement dangerous? Because etcd then has only two of three members and cannot tolerate another failure. Maintenance on a second node has to wait.
  • What does kubeadm join set again that you might not want? The control plane taint and, with serverTLSBootstrap, a pending certificate request.

Enterprise Note

  • With Cluster API or Talos, the replacement becomes a rollout: new machine in, old machine out, with fixed checks in between. The principle is the same as today.
  • Many teams replace control plane nodes regularly instead of patching them (“immutable infrastructure”). Then this day is routine instead of an emergency.
  • With five control plane nodes, etcd tolerates two failures, and still one during a replacement. For large clusters with strict availability targets, that is the reason for five instead of three.

Contents

More