Lesson 131 · Section 10: Cluster Installation & High Availability
Lesson 131: Build an HA cluster with kubeadm: three control planes behind a load balancer
A walkthrough of a highly available kubeadm cluster with stacked etcd: the load balancer, init with --control-plane-endpoint and --upload-certs, control plane joins with --certificate-key, an expired key, failover, removing a node, and external etcd. · 22 min read · 3 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
What this demo builds
The previous lessons covered the theory of a highly available control plane and a single control plane install. This one puts them together: a stacked etcd HA cluster, the kubeadm default, built from five prepared machines and one load balancer.
| Machine | IP | Role |
|---|---|---|
lb | 192.168.56.10 (k8s-api.lab.internal) | HAProxy, TCP 6443 |
controlplane1 to controlplane3 | 192.168.56.11 to .13 | Control plane with stacked etcd |
node01, node02 | 192.168.56.21, .22 | Workers |
Parts:
- HAProxy (k8s-api:6443 (.10))
- controlplane1 (192.168.56.11)
- controlplane2 (192.168.56.12)
- controlplane3 (192.168.56.13)
- node01 (192.168.56.21)
- node02 (192.168.56.22)
Connections:
- node01 to HAProxy
- node02 to HAProxy
- HAProxy to controlplane1
- HAProxy to controlplane2
- HAProxy to controlplane3
Parts:
- 1. Prepare nodes (all 5 machines)
- 2. Load balancer (points at all 3)
- 3. kubeadm init (endpoint + certs)
- 4. CNI plugin (kubectl apply)
- 5. Join CPs (--control-plane)
- 6. Join workers (plain join)
Connections:
- 1. Prepare nodes to 2. Load balancer: then
- 2. Load balancer to 3. kubeadm init: then
- 3. kubeadm init to 4. CNI plugin: then
- 4. CNI plugin to 5. Join CPs: then
- 5. Join CPs to 6. Join workers: then
How it works
The endpoint is in everything
With --control-plane-endpoint k8s-api.lab.internal:6443, kubeadm:
- adds
k8s-api.lab.internalto the API server certificate's subject alternative names, so TLS is valid whichever control plane answers; - writes
server: https://k8s-api.lab.internal:6443intoadmin.confand into every kubelet's kubeconfig; - stores it as
controlPlaneEndpointin thekubeadm-configConfigMap, which every laterjoinreads.
A DNS name is better than the load balancer's IP: if the load balancer ever moves, you change one DNS record instead of every certificate.
Sharing the certificates
Parts:
- controlplane1 (kubeadm init)
- kubeadm-certs (Secret, 2 hours)
- controlplane2 (kubeadm join)
- Certificate key (32 bytes, hex)
- Same CAs on cp2 (/etc/kubernetes/pki)
Connections:
- controlplane1 to kubeadm-certs: encrypts
- kubeadm-certs to controlplane2: download
- Certificate key to controlplane2: decrypts
- controlplane2 to Same CAs on cp2: writes
Every control plane node must use the same cluster CA, etcd CA, front-proxy CA and service account key pair; otherwise tokens signed by one API server would be rejected by another. There are two ways to get them there:
| Way | How | When |
|---|---|---|
Automatic (--upload-certs) | init encrypts the shared files into the Secret kubeadm-certs in kube-system; join --control-plane --certificate-key <key> downloads and decrypts them | Almost always |
| Manual | Copy ca.crt/key, sa.key/pub, front-proxy-ca.crt/key and etcd/ca.crt/key from /etc/kubernetes/pki to each new control plane before join --control-plane | Locked-down setups that forbid secrets in the cluster |
The Secret and the key are deleted after two hours. That is a safety measure, not a problem: on any existing
control plane, sudo kubeadm init phase upload-certs --upload-certs uploads them again and prints a new key.
What join --control-plane adds
On a control plane join, kubeadm does everything a worker join does, and then:
- downloads the shared certificates and generates this node's own (API server, kubelet client, etcd peer);
- writes static Pod manifests for kube-apiserver, kube-controller-manager and kube-scheduler;
- adds a new etcd member to the existing cluster and writes this node's etcd manifest;
- labels and taints the node as a control plane.
Step 3 is why control planes should join one at a time: etcd grows from one member to two (when the majority is two of two), and then to three. Joining two at once while the second member is still syncing risks losing the majority.
Under the hood · The load balancer, in a few lines
The load balancer forwards TCP and checks that each API server answers. A minimal HAProxy configuration on lb:
frontend k8s-api
bind *:6443
mode tcp
default_backend k8s-api
backend k8s-api
mode tcp
option tcp-check
balance roundrobin
server controlplane1 192.168.56.11:6443 check fall 3 rise 2
server controlplane2 192.168.56.12:6443 check fall 3 rise 2
server controlplane3 192.168.56.13:6443 check fall 3 rise 2
Before kubeadm init, only controlplane1 exists and the other two backends show as down; that is fine, the health
checks bring them in as they join. If the load balancer runs on the control plane nodes themselves (kube-vip, or
HAProxy plus keepalived), it must listen on a different port or a virtual IP, because 6443 on each node is taken by
the API server. For production, run two load balancers with a floating virtual IP so that the load balancer is not a
single point of failure.
The walkthrough
Example · 1. Prepare, and check the load balancer
Prepare all five machines as in Prepare nodes for kubeadm: swap off, IP forwarding, containerd with
SystemdCgroup = true, and the same kubelet, kubeadm and kubectl versions. Set up HAProxy as shown above,
and make sure every node resolves the name (DNS, or a line in /etc/hosts):
echo '192.168.56.10 k8s-api.lab.internal' | sudo tee -a /etc/hosts # on every node
nc -zv k8s-api.lab.internal 6443
nc: connect to k8s-api.lab.internal port 6443 (tcp) failed: Connection refused
"Connection refused" is the right answer at this point: the load balancer is listening but has no healthy API server behind it yet. A timeout would mean the load balancer itself is not reachable.
Example · 2. Initialise the first control plane
On controlplane1:
sudo kubeadm init \
--control-plane-endpoint k8s-api.lab.internal:6443 \
--upload-certs \
--apiserver-advertise-address 192.168.56.11 \
--pod-network-cidr 10.244.0.0/16
...
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
f8902e114ef118304e561c3ecd4d0b543adc226b7a07f675f56564185ffe0c07
...
Your Kubernetes control-plane has initialized successfully!
...
You can now join any number of control-plane nodes running the following command on each as root:
kubeadm join k8s-api.lab.internal:6443 --token 9vr73a.a8uxyaju799qwdjv \
--discovery-token-ca-cert-hash sha256:7c2e69131a36ae2a042a339b33381c6d0d43887e2de83720eff5359e26aec866 \
--control-plane --certificate-key f8902e114ef118304e561c3ecd4d0b543adc226b7a07f675f56564185ffe0c07
Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join k8s-api.lab.internal:6443 --token 9vr73a.a8uxyaju799qwdjv \
--discovery-token-ca-cert-hash sha256:7c2e69131a36ae2a042a339b33381c6d0d43887e2de83720eff5359e26aec866
Save both join commands. Copy admin.conf to ~/.kube/config as before and install the CNI plugin:
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
kubectl get nodes
--apiserver-advertise-address is this node's own IP; the endpoint is the shared one. Both are needed when a VM
has more than one network interface.
Example · 3. Join controlplane2, then controlplane3
On controlplane2, the control plane join command, with this node's own advertise address added:
sudo kubeadm join k8s-api.lab.internal:6443 --token 9vr73a.a8uxyaju799qwdjv \
--discovery-token-ca-cert-hash sha256:7c2e69131a36ae2a042a339b33381c6d0d43887e2de83720eff5359e26aec866 \
--control-plane --certificate-key f8902e114ef118304e561c3ecd4d0b543adc226b7a07f675f56564185ffe0c07 \
--apiserver-advertise-address 192.168.56.12
[download-certs] Downloading the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
...
[etcd] Announced new etcd member joining to the existing etcd cluster
...
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.
Wait until kubectl get nodes shows controlplane2 as Ready, then repeat on controlplane3 with
--apiserver-advertise-address 192.168.56.13.
Example · 4. When the certificate key has expired
Two hours passed and the join fails with an error that the kubeadm-certs Secret is not found. On
controlplane1 (any existing control plane works):
sudo kubeadm init phase upload-certs --upload-certs
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
3c1b8a52d7e04f6a9b2c1d8e7f6a5b4c3d2e1f0a9b8c7d6e5f4a3b2c1d0e9f4d
Then print a full control plane join line with the new key (and a new token):
kubeadm token create --print-join-command \
--certificate-key 3c1b8a52d7e04f6a9b2c1d8e7f6a5b4c3d2e1f0a9b8c7d6e5f4a3b2c1d0e9f4d
Leave out --certificate-key and the same command prints a worker join line.
Example · 5. Join the workers and check the cluster
On node01 and node02, the worker join command, as in the previous lesson. Then, from any control plane:
kubectl get nodes
NAME STATUS ROLES AGE VERSION
controlplane1 Ready control-plane 20m v1.37.0
controlplane2 Ready control-plane 12m v1.37.0
controlplane3 Ready control-plane 9m v1.37.0
node01 Ready <none> 3m v1.37.0
node02 Ready <none> 2m v1.37.0
List the etcd members:
kubectl -n kube-system exec etcd-controlplane1 -- 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 \
member list -w table
+------------------+---------+---------------+----------------------------+----------------------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS |
+------------------+---------+---------------+----------------------------+----------------------------+
| 8e9e05c52164694d | started | controlplane1 | https://192.168.56.11:2380 | https://192.168.56.11:2379 |
| 2b3c8f1a9d0e7c45 | started | controlplane2 | https://192.168.56.12:2380 | https://192.168.56.12:2379 |
| 5f7a1c3e8b2d4096 | started | controlplane3 | https://192.168.56.13:2380 | https://192.168.56.13:2379 |
+------------------+---------+---------------+----------------------------+----------------------------+
And confirm that clients use the endpoint, not a node:
grep server: ~/.kube/config # https://k8s-api.lab.internal:6443
ssh node01 sudo grep server: /etc/kubernetes/kubelet.conf
Example · 6. Test a failover
Shut down controlplane1 and keep working:
ssh controlplane1 sudo poweroff
sleep 30
kubectl get nodes
kubectl create deployment failover-test --image=nginx --replicas=2
kubectl get pods -l app=failover-test -o wide
kubectl still answers (through controlplane2 or controlplane3), controlplane1 turns NotReady after about
40 seconds, and the new Deployment's Pods are scheduled, because another scheduler took the Lease. etcd runs on two
of three members, which is still a majority. Power controlplane1 back on and it rejoins by itself.
Example · 7. Remove a control plane node
To retire controlplane3 cleanly, first take it out of service, then let kubeadm reset remove its etcd member:
kubectl drain controlplane3 --ignore-daemonsets # from another node
ssh controlplane3 sudo kubeadm reset -f # removes its etcd member, then cleans up
kubectl delete node controlplane3
Remove it from the HAProxy backends too. If the node is already dead and cannot run reset, remove its etcd member
by hand with etcdctl member remove <ID> from a healthy member; a dead member left in the list counts against the
majority.
The external etcd variant
With external etcd, the etcd cluster is set up first on its own three hosts (the docs page "Set up a High Availability etcd cluster with kubeadm" does this with kubeadm itself). Then you copy the etcd CA and the API server's etcd client certificate to the first control plane node and initialise it from a config file instead of flags:
apiVersion: kubeadm.k8s.io/v1beta4
kind: ClusterConfiguration
kubernetesVersion: v1.37.0
controlPlaneEndpoint: "k8s-api.lab.internal:6443"
networking:
podSubnet: 10.244.0.0/16
etcd:
external:
endpoints:
- https://192.168.56.31:2379
- https://192.168.56.32:2379
- https://192.168.56.33:2379
caFile: /etc/kubernetes/pki/etcd/ca.crt
certFile: /etc/kubernetes/pki/apiserver-etcd-client.crt
keyFile: /etc/kubernetes/pki/apiserver-etcd-client.key
sudo kubeadm init --config kubeadm-config.yaml --upload-certs
Everything after that, CNI, control plane joins with --certificate-key, workers, is identical. The difference is
that control plane joins add no etcd member, and that backups and upgrades of etcd are now a separate job.
Tricks
Common mistakes
- Running
kubeadm initwithout--control-plane-endpointand trying to add control planes later. kubeadm does not support converting that cluster; rebuild it with the endpoint. - Forgetting
--upload-certsatinit, then joining with--control-planeand no certificates. Upload them withkubeadm init phase upload-certs --upload-certs, or copy them by hand. - Joining a control plane with the worker command. Without
--control-plane, the node becomes a worker. - Joining several control planes at the same time. Join one, wait for
Ready, then the next. - Using a node IP as the endpoint. Then that node is still a single point of failure.
- Leaving a dead control plane in the etcd member list. It still counts towards the majority; remove it.
Recap
- Load balancer first, on TCP 6443, pointing at every control plane; give it a DNS name.
- First node:
kubeadm init --control-plane-endpoint <lb>:6443 --upload-certs(plus the Pod range and its own advertise address). - Other control planes, one at a time: the join command with
--control-plane --certificate-key <key>. - The certificate key expires after two hours:
kubeadm init phase upload-certs --upload-certsmakes a new one. - Workers join as usual through the endpoint. Check with
kubectl get nodesandetcdctl member list. - External etcd: set
etcd.externalin aClusterConfigurationfile; the rest is the same.
Official docs
- Kubernetes docs: Creating Highly Available Clusters with kubeadm
- Kubernetes docs: Options for Highly Available Topology
- Kubernetes docs: Set up a High Availability etcd cluster with kubeadm
- Kubernetes docs: kubeadm init (uploading control plane certificates)
- Kubernetes docs: kubeadm certs
- kubeadm docs: High availability considerations (load balancer options)
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.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
Larkspur's cluster has one control plane node and a load balancer already in front of it. Join two prepared machines as control plane nodes, so that the cluster and its etcd keep working when one of them is down.
Build · Level 3 · challenge · target 25 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.