A cluster that survives losing a server
A Kubernetes cluster needs a place to keep its state: which nodes exist, what’s running, what should be running. With one control-plane server, that place is a single machine. If it dies, the cluster is gone until I fix it. I wanted to pull the plug on one server and keep working.
This is the second post in my k3s homelab series. In the first one I built two load balancers that share the virtual IP 192.168.100.254. Everything below leans on that address.
k3s keeps the highly available setup simple with embedded etcd. etcd is the small database Kubernetes keeps its state in, and every server runs its own copy. The copies vote on changes, and a change only counts when a majority agrees. That majority is called quorum. With three servers, quorum is two, so I can lose one and the cluster keeps going. I ran three servers and two workers.
The k3s docs recommend a fixed registration address in front of the servers, but they don’t require one. Agents keep a list of every server and balance between them on their own. What the VIP gave me was one stable endpoint for kubectl, for new nodes, and for ingress. Without it, I’d have pointed things at master-1 and accepted that it’s a single point of failure.
My lab
Every node runs Ubuntu, on a mix of amd64 and arm64 hardware, which k3s handles without any extra work. The hostnames carry a
k3s-prefix, somaster-1shows up ask3s-master-1inkubectl. I installedv1.37.2+k3s1.
| Node | IP | Role |
|---|---|---|
| VIP (Keepalived on lb-1 and lb-2) | 192.168.100.254 | API and registration address |
| master-1 | 192.168.100.241 | first server |
| master-2 | 192.168.100.242 | joining server |
| master-3 | 192.168.100.243 | joining server |
| worker-1 / worker-2 | .231 / .232 | agents |
One token for the whole cluster
All the nodes prove they belong to the cluster with a shared secret, the join token. I generated mine once, on my laptop, and put it straight into a password manager.
openssl rand -hex 32
It’s a real secret, so it doesn’t appear in this post. Wherever you see <your-cluster-token> below, the real value goes in on the machine itself.
master-1 starts the cluster
I put the settings in /etc/rancher/k3s/config.yaml rather than a long list of install flags, so every node’s setup lives in one readable file.
token: "<your-cluster-token>"
cluster-init: true
tls-san:
- 192.168.100.254
- 192.168.100.241
- 192.168.100.242
- 192.168.100.243
tls-san lists every address the API certificate must be valid for. The VIP matters most, since anything connecting through .254 would otherwise hit a certificate error. Then I installed k3s as a server:
curl -sfL https://get.k3s.io | sh -s - server
I checked it with sudo k3s kubectl get nodes -o wide and sudo k3s kubectl get pods -A. master-1 came up Ready, and CoreDNS, Traefik, metrics-server, and local-path-provisioner followed. Traefik can take a minute or two.
The roles column read control-plane,etcd. Older guides and screenshots show control-plane,master, so I want to explain that one up front. Kubernetes moved from the name master to control-plane a while ago, and newer k3s versions only label nodes with the new name, plus etcd because this server runs a member of the database. Nothing is misconfigured when master is missing.
master-2 and master-3 join, they don’t start
The other two servers got almost the same file. The only differences are a missing line and a new one:
token: "<your-cluster-token>"
server: https://192.168.100.241:6443
tls-san:
- 192.168.100.254
# ...plus the same three node IPs as before
There’s no cluster-init, and there’s a server: line. cluster-init: true tells a server to bootstrap a brand-new etcd cluster, so it belongs on the first server only. If master-2 had it too, it would start a second, separate cluster instead of joining the first one. With server: and the shared token, it joins the existing cluster as one more etcd member.
I pointed server: at master-1 rather than the VIP on purpose. Right then, master-1 was the only healthy API server, and HAProxy could have sent the join request to .242 or .243, which weren’t up yet. Once all three were running, the VIP was safe to use.
The install command is the same one I ran on master-1, because these are still servers, not agents. A joining server can take a minute or two, so I gave it time. From master-1, kubectl get nodes showed both servers Ready with control-plane,etcd.
After master-2, I wasn’t highly available yet. Two members need both to be up for quorum, so losing either one breaks it. I repeated the same steps on master-3, and that’s where the failure tolerance started.
The workers come in through the VIP
By now all three servers sat behind HAProxy, so the workers could use the VIP as their registration address. First I checked that the VIP actually reached an API server:
curl -k https://192.168.100.254:6443/ping
I expected pong, and if it hadn’t answered, the HAProxy config from Post 1 would have been the thing to fix before going further. The agent config is shorter, because an agent doesn’t run etcd or serve the API, so it needs no cluster-init and no tls-san:
token: "<your-cluster-token>"
server: https://192.168.100.254:6443
curl -sfL https://get.k3s.io | sh -s - agent
On a worker the service is called k3s-agent, not k3s. Back on master-1, the cluster looked like this (trimmed to the first columns):
NAME STATUS ROLES AGE VERSION
k3s-master-1 Ready control-plane,etcd 43m v1.37.2+k3s1
k3s-master-2 Ready control-plane,etcd 35m v1.37.2+k3s1
k3s-master-3 Ready control-plane,etcd 28m v1.37.2+k3s1
k3s-worker-1 Ready <none> 9m57s v1.37.2+k3s1
k3s-worker-2 Ready <none> 89s v1.37.2+k3s1
The <none> is normal. The roles column is built from node-role.kubernetes.io/... labels, and k3s doesn’t put one on agents. It’s cosmetic, but I like readable output, so I labeled both from master-1:
sudo k3s kubectl label node k3s-worker-1 node-role.kubernetes.io/worker=worker
sudo k3s kubectl label node k3s-worker-2 node-role.kubernetes.io/worker=worker
Both workers then showed worker in the roles column.
Where things stood
By the end of this, here’s where things stood:
- Three servers with embedded etcd, so the cluster could lose one and keep running
- Two workers joined through the VIP and labeled
worker - Every node
Readyonv1.37.2+k3s1 - One shared token, kept in a password manager and on the nodes
I’d only tested the VIP failover back when nothing was behind it. In the next post, I’ll break the running cluster on purpose and watch whether the load balancers and etcd do what I built them to do.