[{"content":"A cluster that survives losing a server A Kubernetes cluster needs a place to keep its state: which nodes exist, what\u0026rsquo;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.\nThis 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.\nk3s 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.\nThe k3s docs recommend a fixed registration address in front of the servers, but they don\u0026rsquo;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\u0026rsquo;d have pointed things at master-1 and accepted that it\u0026rsquo;s a single point of failure.\nMy 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, so master-1 shows up as k3s-master-1 in kubectl. I installed v1.37.2+k3s1.\nNode 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.\nopenssl rand -hex 32 It\u0026rsquo;s a real secret, so it doesn\u0026rsquo;t appear in this post. Wherever you see \u0026lt;your-cluster-token\u0026gt; below, the real value goes in on the machine itself.\nmaster-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\u0026rsquo;s setup lives in one readable file.\ntoken: \u0026#34;\u0026lt;your-cluster-token\u0026gt;\u0026#34; 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:\ncurl -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.\nThe 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.\nmaster-2 and master-3 join, they don\u0026rsquo;t start The other two servers got almost the same file. The only differences are a missing line and a new one:\ntoken: \u0026#34;\u0026lt;your-cluster-token\u0026gt;\u0026#34; server: https://192.168.100.241:6443 tls-san: - 192.168.100.254 # ...plus the same three node IPs as before There\u0026rsquo;s no cluster-init, and there\u0026rsquo;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.\nI 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\u0026rsquo;t up yet. Once all three were running, the VIP was safe to use.\nThe 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.\nAfter master-2, I wasn\u0026rsquo;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\u0026rsquo;s where the failure tolerance started.\nThe 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:\ncurl -k https://192.168.100.254:6443/ping I expected pong, and if it hadn\u0026rsquo;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\u0026rsquo;t run etcd or serve the API, so it needs no cluster-init and no tls-san:\ntoken: \u0026#34;\u0026lt;your-cluster-token\u0026gt;\u0026#34; 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):\nNAME 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 \u0026lt;none\u0026gt; 9m57s v1.37.2+k3s1 k3s-worker-2 Ready \u0026lt;none\u0026gt; 89s v1.37.2+k3s1 The \u0026lt;none\u0026gt; is normal. The roles column is built from node-role.kubernetes.io/... labels, and k3s doesn\u0026rsquo;t put one on agents. It\u0026rsquo;s cosmetic, but I like readable output, so I labeled both from master-1:\nsudo 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.\nWhere things stood By the end of this, here\u0026rsquo;s where things stood:\nThree servers with embedded etcd, so the cluster could lose one and keep running Two workers joined through the VIP and labeled worker Every node Ready on v1.37.2+k3s1 One shared token, kept in a password manager and on the nodes I\u0026rsquo;d only tested the VIP failover back when nothing was behind it. In the next post, I\u0026rsquo;ll break the running cluster on purpose and watch whether the load balancers and etcd do what I built them to do.\n","permalink":"https://blog.cristiamacuna.com/posts/02-k3s-ha-install/","summary":"\u003ch2 id=\"a-cluster-that-survives-losing-a-server\"\u003eA cluster that survives losing a server\u003c/h2\u003e\n\u003cp\u003eA Kubernetes cluster needs a place to keep its state: which nodes exist, what\u0026rsquo;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.\u003c/p\u003e\n\u003cp\u003eThis is the second post in my k3s homelab series. In \u003ca href=\"/posts/01-haproxy-keepalived-vip/\"\u003ethe first one\u003c/a\u003e I built two load balancers that share the virtual IP \u003ccode\u003e192.168.100.254\u003c/code\u003e. Everything below leans on that address.\u003c/p\u003e","title":"Building an HA k3s Homelab: Installing k3s with Embedded etcd"},{"content":"If one load balancer dies, the cluster goes with it I didn\u0026rsquo;t want a single load balancer in front of the cluster. If that box died, I\u0026rsquo;d lose kubectl, I couldn\u0026rsquo;t join new nodes, and ingress would disappear with it. So I started this homelab with two load balancers sharing one address.\nThis is the first post in a series about my k3s homelab. It starts with a highly available cluster and goes on to storage, GitOps, observability, and a lot of security hardening. I work in DevSecOps, and I wanted a cluster I could break, fix, and learn from at home.\nThe idea I went with is simple. Two Ubuntu machines both run HAProxy and share one virtual IP, 192.168.100.254 in my lab. One holds the address. The other sits on standby and takes it if the main one goes quiet. That\u0026rsquo;s VRRP, and Keepalived is what implements it.\nHAProxy on both boxes listens on that VIP for:\nTCP 6443, the k3s API, balanced to the three future masters .241, .242, and .243 TCP 443 and HTTP 80 for ingress, also pointed at those same three masters for now Clients, new nodes, and ingress always talk to .254, no matter which load balancer is up.\nThe architecture flowchart TB C[Clients / kubectl] --\u0026gt; VIP((VIP\u0026lt;br/\u0026gt;192.168.100.254)) VIP --\u0026gt; LB1[\u0026#34;lb-1\u0026lt;br/\u0026gt;HAProxy + Keepalived\u0026lt;br/\u0026gt;MASTER · .253\u0026#34;] VIP -.failover.-\u0026gt; LB2[\u0026#34;lb-2\u0026lt;br/\u0026gt;HAProxy + Keepalived\u0026lt;br/\u0026gt;BACKUP · .252\u0026#34;] LB1 --\u0026gt;|6443 API\u0026lt;br/\u0026gt;80/443 ingress| M[\u0026#34;master-1 · .241\u0026lt;br/\u0026gt;master-2 · .242\u0026lt;br/\u0026gt;master-3 · .243\u0026#34;] LB2 -.-\u0026gt; M My lab The LAN is 192.168.100.0/24, and the gateway in these configs is 192.168.100.1. If your gateway is different, that\u0026rsquo;s the line to change. I limited the DHCP pool to .2–.199 so it couldn\u0026rsquo;t hand out these addresses. Keepalived uses virtual_router_id 51 on both sides. The servers are a mini PC and two Raspberry Pi 4s, all with SSDs, the workers are two Raspberry Pi 5s, and the load balancers are two Ubuntu machines.\nRole IP VIP 192.168.100.254 lb-1, MASTER, priority 200 192.168.100.253 lb-2, BACKUP, priority 100 192.168.100.252 master-1 / master-2 / master-3 .241 / .242 / .243 worker-1 / worker-2 .231 / .232 First, fixed IPs for everything I started with static IPs, on every node, before I installed HAProxy or Keepalived. I wanted each machine to keep the same address after a reboot.\nI checked the interface with ip -br a and the gateway with ip route | grep default. This is the netplan I used for a node on eth0 at 192.168.100.241, and I adjusted the address and interface name on the others.\nsudo tee /etc/netplan/01-static.yaml \u0026gt; /dev/null \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; network: version: 2 ethernets: eth0: dhcp4: false addresses: [192.168.100.241/24] routes: - to: default via: 192.168.100.1 nameservers: addresses: [192.168.100.1, 1.1.1.1] EOF sudo chmod 600 /etc/netplan/01-static.yaml On Ubuntu, cloud-init and the installer leave their own files in /etc/netplan/, usually 50-cloud-init.yaml and 00-installer-config.yaml. If those stay around, the node ends up with a second address next to the static one. So before applying, I disabled cloud-init networking, removed the leftovers, and made sure my static file was the only one left in that directory.\necho \u0026#39;network: {config: disabled}\u0026#39; | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg sudo rm -f /etc/netplan/50-cloud-init.yaml sudo netplan apply If you\u0026rsquo;re connected over SSH, expect the session to drop here, since netplan apply moves the machine to its new address. I reconnected on the new IP and ran ip -br a to check that only the static address was left.\nSetting up HAProxy I installed the packages on both load balancers, including psmisc for killall, which the health check uses later. I also ran ip -br a so I\u0026rsquo;d have the interface name for Keepalived.\nsudo apt update sudo apt install -y haproxy keepalived psmisc ip -br a # I needed the interface name for Keepalived I wanted both load balancers to run the same config and send VIP traffic to the three masters. I pointed 80 and 443 at the masters, not the workers. Once k3s is in, Traefik\u0026rsquo;s ServiceLB pods run on every node, so this still works.\nI used option tcp-check so HAProxy would probe each backend with a TCP connect and stop sending traffic to a server that isn\u0026rsquo;t listening.\n/etc/haproxy/haproxy.cfg:\nglobal log /dev/log local0 user haproxy group haproxy daemon defaults log global timeout connect 5s timeout client 50s timeout server 50s frontend k3s-frontend bind *:6443 mode tcp option tcplog default_backend k3s-backend backend k3s-backend mode tcp option tcp-check balance roundrobin default-server inter 10s downinter 5s server master-1 192.168.100.241:6443 check server master-2 192.168.100.242:6443 check server master-3 192.168.100.243:6443 check frontend https_front bind *:443 mode tcp option tcplog default_backend ingress_https backend ingress_https mode tcp balance roundrobin option tcp-check server master-1 192.168.100.241:443 check server master-2 192.168.100.242:443 check server master-3 192.168.100.243:443 check frontend http_front bind *:80 mode http option httplog default_backend ingress_http backend ingress_http mode http balance roundrobin server master-1 192.168.100.241:80 check server master-2 192.168.100.242:80 check server master-3 192.168.100.243:80 check I copied that file to the other load balancer, then validated it and restarted the service:\nsudo haproxy -c -f /etc/haproxy/haproxy.cfg # Configuration file is valid sudo systemctl restart haproxy sudo ss -ltnp | grep -E \u0026#39;:6443|:443|:80\u0026#39; The restart matters. HAProxy starts at install time with the package\u0026rsquo;s default config, so a valid file on disk isn\u0026rsquo;t live until the service restarts or reloads. haproxy -c only checks the file. That\u0026rsquo;s why I always restart right after validating. After the restart, ss showed listeners on 6443, 443, and 80.\nNote: both load balancers need the exact same haproxy.cfg. If the copies drift, everything looks fine until a failover sends traffic to the one with the mistake. Only Keepalived\u0026rsquo;s state, priority, and interface should differ, so after any edit I diff the two files (or copy from one source), run haproxy -c, and restart.\nSharing the VIP with Keepalived Then I needed the two load balancers to share 192.168.100.254. virtual_router_id 51 had to match on both. The health check I used is killall -0 haproxy. It doesn\u0026rsquo;t kill the process; it only checks that HAProxy is running, and Keepalived holds the VIP while that succeeds. That\u0026rsquo;s why I installed psmisc, and why enable_script_security and script_user root are required. If your interface isn\u0026rsquo;t eth0, that\u0026rsquo;s the line to change.\nThis is the file I put on lb-1, the MASTER, with priority 200.\n/etc/keepalived/keepalived.conf:\nglobal_defs { enable_script_security script_user root } vrrp_script chk_haproxy { script \u0026#34;killall -0 haproxy\u0026#34; interval 2 } vrrp_instance haproxy-vip { interface eth0 state MASTER priority 200 virtual_router_id 51 virtual_ipaddress { 192.168.100.254/24 } track_script { chk_haproxy } } On lb-2 I used the same file and changed only two lines, state BACKUP and priority 100:\nstate BACKUP priority 100 Then I enabled and started both services on each load balancer:\nsudo systemctl enable --now haproxy keepalived systemctl is-active haproxy keepalived I was looking for both to report active, with 192.168.100.253 and the VIP on lb-1, and only .252 on lb-2 unless it had already taken over.\nBreaking it on purpose I wanted to be sure the standby actually took the address, so I stopped Keepalived on lb-1 and checked lb-2.\nAt this point k3s wasn\u0026rsquo;t installed yet, so nothing was listening behind HAProxy. I expected the VIP and the listeners to move, and the curl to fail. At this stage the test is about the address, not the API.\n# on lb-1 sudo systemctl stop keepalived # on lb-2 ip -br a | grep 192.168.100.254 curl -k https://192.168.100.254:6443/ping sudo ss -ltnp | grep -E \u0026#39;:6443|:443|:80\u0026#39; The VIP moved over. On lb-2 I saw .252/24 and .254/24. ss showed listeners on 0.0.0.0:80, :6443, and :443.\nAs expected, the curl failed:\ncurl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to 192.168.100.254:6443 HAProxy also logged backend k3s-backend has no server available!, and the same for ingress_https and ingress_http.\nWith no k3s behind it, HAProxy marks every backend down and closes the connection, which is exactly what that error means. What mattered was that the VIP moved and HAProxy was listening on all three ports. The \u0026ldquo;no server available\u0026rdquo; messages go away once the control plane is up, and then the same curl should return pong.\nI started Keepalived on lb-1 again and checked for the VIP:\n# on lb-1 sudo systemctl start keepalived ip -br a | grep 192.168.100.254 Where things stood before k3s By the end of this, here\u0026rsquo;s where things stood:\nThe VIP moved to lb-2 when I stopped Keepalived on lb-1 HAProxy was listening on 6443, 443, and 80 The two haproxy.cfg files were identical Each node had only its static IP Next I\u0026rsquo;ll install k3s in high availability with embedded etcd. The first server will include 192.168.100.254 as a TLS SAN, and every later node will join through that same address. That\u0026rsquo;s also when curl -k https://192.168.100.254:6443/ping should start returning pong.\n","permalink":"https://blog.cristiamacuna.com/posts/01-haproxy-keepalived-vip/","summary":"\u003ch2 id=\"if-one-load-balancer-dies-the-cluster-goes-with-it\"\u003eIf one load balancer dies, the cluster goes with it\u003c/h2\u003e\n\u003cp\u003eI didn\u0026rsquo;t want a single load balancer in front of the cluster. If that box died, I\u0026rsquo;d lose \u003ccode\u003ekubectl\u003c/code\u003e, I couldn\u0026rsquo;t join new nodes, and ingress would disappear with it. So I started this homelab with two load balancers sharing one address.\u003c/p\u003e\n\u003cp\u003eThis is the first post in a series about my \u003ca href=\"https://docs.k3s.io/\"\u003ek3s\u003c/a\u003e homelab. It starts with a highly available cluster and goes on to storage, GitOps, observability, and a lot of security hardening. I work in DevSecOps, and I wanted a cluster I could break, fix, and learn from at home.\u003c/p\u003e","title":"One IP to Rule Them All: HAProxy and Keepalived in Front of k3s"}]