If one load balancer dies, the cluster goes with it

I didn’t want a single load balancer in front of the cluster. If that box died, I’d lose kubectl, I couldn’t join new nodes, and ingress would disappear with it. So I started this homelab with two load balancers sharing one address.

This 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.

The 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’s VRRP, and Keepalived is what implements it.

HAProxy on both boxes listens on that VIP for:

  • TCP 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.

The architecture

flowchart TB
    C[Clients / kubectl] --> VIP((VIP<br/>192.168.100.254))
    VIP --> LB1["lb-1<br/>HAProxy + Keepalived<br/>MASTER · .253"]
    VIP -.failover.-> LB2["lb-2<br/>HAProxy + Keepalived<br/>BACKUP · .252"]
    LB1 -->|6443 API<br/>80/443 ingress| M["master-1 · .241<br/>master-2 · .242<br/>master-3 · .243"]
    LB2 -.-> 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’s the line to change. I limited the DHCP pool to .2–.199 so it couldn’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.

RoleIP
VIP192.168.100.254
lb-1, MASTER, priority 200192.168.100.253
lb-2, BACKUP, priority 100192.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.

I 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.

sudo tee /etc/netplan/01-static.yaml > /dev/null <<'EOF'
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.

echo 'network: {config: disabled}' | 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’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.

Setting 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’d have the interface name for Keepalived.

sudo 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’s ServiceLB pods run on every node, so this still works.

I used option tcp-check so HAProxy would probe each backend with a TCP connect and stop sending traffic to a server that isn’t listening.

/etc/haproxy/haproxy.cfg:

global
    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:

sudo haproxy -c -f /etc/haproxy/haproxy.cfg
# Configuration file is valid
sudo systemctl restart haproxy
sudo ss -ltnp | grep -E ':6443|:443|:80'

The restart matters. HAProxy starts at install time with the package’s default config, so a valid file on disk isn’t live until the service restarts or reloads. haproxy -c only checks the file. That’s why I always restart right after validating. After the restart, ss showed listeners on 6443, 443, and 80.

Note: 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’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.

Sharing 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’t kill the process; it only checks that HAProxy is running, and Keepalived holds the VIP while that succeeds. That’s why I installed psmisc, and why enable_script_security and script_user root are required. If your interface isn’t eth0, that’s the line to change.

This is the file I put on lb-1, the MASTER, with priority 200.

/etc/keepalived/keepalived.conf:

global_defs {
    enable_script_security
    script_user root
}

vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    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:

    state BACKUP
    priority 100

Then I enabled and started both services on each load balancer:

sudo 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.

Breaking 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.

At this point k3s wasn’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.

# 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 ':6443|:443|:80'

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.

As expected, the curl failed:

curl: (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.

With 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 “no server available” messages go away once the control plane is up, and then the same curl should return pong.

I started Keepalived on lb-1 again and checked for the VIP:

# 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’s where things stood:

  • The 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’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’s also when curl -k https://192.168.100.254:6443/ping should start returning pong.