DEV Community

Cover image for Watch a VIP Fail Over: HAProxy + Keepalived in One Linux Box with Network Namespaces
Chikara Inohara
Chikara Inohara

Posted on

Watch a VIP Fail Over: HAProxy + Keepalived in One Linux Box with Network Namespaces

In an earlier post I dug into Corosync and how a Proxmox cluster decides whether its peers are alive. This time it's a more everyday kind of redundancy: a VIP (virtual IP) made highly available with HAProxy + Keepalived.

Spinning up five VMs just to test this is tedious, though. So I built "2 load balancers + 2 web servers + a client" inside a single Linux box using network namespaces, and watched the VIP move. No Docker, no VMs.

Every screenshot here is a real run on my Ubuntu machine.

Topology

Topology: client to VIP, lb1 MASTER and lb2 BACKUP, both forwarding to web1 and web2

Component Job
HAProxy round-robins to web1 / web2 and health-checks them
Keepalived uses VRRP to decide whether lb1 or lb2 owns the VIP; gives the VIP up when HAProxy dies

Environment

Item Version
OS Ubuntu 24.04
HAProxy 2.8.30
Keepalived 2.2.8
sudo apt install -y haproxy keepalived iproute2
Enter fullscreen mode Exit fullscreen mode

Step 1: Build the lab with network namespaces

One bridge, br-lab, with five namespaces hanging off it via veth pairs:

# up.sh
#!/bin/bash
# Build "2 LBs + 2 webs + client" inside one Linux box with network namespaces
set -e
ip link add br-lab type bridge && ip link set br-lab up
i=0
for ns in client lb1 lb2 web1 web2; do
  case $ns in client) ip=10.0.0.100;; lb1) ip=10.0.0.11;; lb2) ip=10.0.0.12;; web1) ip=10.0.0.21;; web2) ip=10.0.0.22;; esac
  ip netns add $ns
  ip link add v-$ns type veth peer name eth0 netns $ns
  ip link set v-$ns master br-lab up
  ip -n $ns addr add $ip/24 dev eth0
  ip -n $ns link set eth0 up
  ip -n $ns link set lo up
done
# Let the LBs bind the VIP even when they do not hold it
for ns in lb1 lb2; do ip netns exec $ns sysctl -qw net.ipv4.ip_nonlocal_bind=1; done
# Web: just return its own name
for ns in web1 web2; do
  mkdir -p /tmp/$ns && echo "hello from $ns" > /tmp/$ns/index.html
  ip netns exec $ns nohup python3 -m http.server 80 --directory /tmp/$ns >/tmp/$ns.log 2>&1 &
done
echo "lab is up"
Enter fullscreen mode Exit fullscreen mode

Tearing it down:

# down.sh
#!/bin/bash
for ns in client lb1 lb2 web1 web2; do ip netns pids $ns 2>/dev/null | xargs -r kill 2>/dev/null; ip netns del $ns 2>/dev/null; done
ip link del br-lab 2>/dev/null; echo "lab is down"
Enter fullscreen mode Exit fullscreen mode

Step 2: HAProxy

lb1 and lb2 are almost identical; only the X-LB header differs, so we can see which LB answered.

# haproxy-lb1.cfg
global
    log stdout format raw local0
    pidfile /run/haproxy-lb1.pid

defaults
    mode http
    log global
    option httplog
    timeout connect 2s
    timeout client  10s
    timeout server  10s

frontend fe_web
    bind 10.0.0.10:80
    http-response set-header X-LB lb1
    default_backend be_web

frontend fe_health
    bind 127.0.0.1:8404
    monitor-uri /health

backend be_web
    balance roundrobin
    option httpchk GET /
    server web1 10.0.0.21:80 check inter 1s fall 2 rise 2
    server web2 10.0.0.22:80 check inter 1s fall 2 rise 2
Enter fullscreen mode Exit fullscreen mode
  • bind 10.0.0.10:80 — listen on the VIP. ip_nonlocal_bind=1 lets the LB that doesn't hold the VIP bind it anyway
  • fe_health — an endpoint (monitor-uri) Keepalived uses to ask "is HAProxy alive?"
  • check inter 1s fall 2 rise 2 — web health checks; a server is pulled after 2 failures

Step 3: Keepalived

# keepalived-lb1.conf
global_defs {
    router_id lb1
    script_user root
    enable_script_security
}

vrrp_script chk_haproxy {
    script "/usr/bin/curl -sf -o /dev/null http://127.0.0.1:8404/health"
    interval 1
    fall 2
    rise 2
}

vrrp_instance VI_WEB {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 110
    advert_int 1
    unicast_src_ip 10.0.0.11
    unicast_peer {
        10.0.0.12
    }
    virtual_ipaddress {
        10.0.0.10/24 dev eth0
    }
    track_script {
        chk_haproxy
    }
}
Enter fullscreen mode Exit fullscreen mode

The only differences on lb2:

$ diff keepalived-lb1.conf keepalived-lb2.conf
2c2
<     router_id lb1
---
>     router_id lb2
15c15
<     state MASTER
---
>     state BACKUP
18c18
<     priority 110
---
>     priority 100
20c20
<     unicast_src_ip 10.0.0.11
---
>     unicast_src_ip 10.0.0.12
22c22
<         10.0.0.12
---
>         10.0.0.11
Enter fullscreen mode Exit fullscreen mode

The key part is vrrp_script: every second it hits HAProxy's health URL, and after 2 failures the instance goes to FAULT and releases the VIP.

I used unicast_peer with environments that don't allow multicast (like clouds) in mind.

Starting it:

# start-lb.sh
#!/bin/bash
# usage: start-lb.sh lb1|lb2   (logs go to /tmp/keepalived-lbX.log)
n=$1; d=$(cd $(dirname $0); pwd)
ip netns exec $n haproxy -D -f $d/haproxy-$n.cfg
ip netns exec $n nohup keepalived --dont-fork --log-console --log-detail \
  -f $d/keepalived-$n.conf -p /run/ka-$n.pid -r /run/ka-$n-vrrp.pid -c /run/ka-$n-chk.pid \
  > /tmp/keepalived-$n.log 2>&1 &
Enter fullscreen mode Exit fullscreen mode

Step 4: Normal state

lb1 holds the VIP; requests alternate between web1 and web2 via lb1

  • the VIP 10.0.0.10 is on lb1
  • client requests come back with x-lb: lb1, alternating between web1 and web2

Step 5: Kill HAProxy on lb1

A small script hits the VIP from the client every 0.2 seconds:

# watch.sh
#!/bin/bash
# Hit the VIP from the client every 0.2s and log which LB / web answered
end=$((SECONDS+${1:-12}))
while [ $SECONDS -lt $end ]; do
  t=$(date +%T.%3N)
  r=$(ip netns exec client curl -s -m 1 -i http://10.0.0.10/ | tr -d '\r' | awk '/^x-lb:/{lb=$2} /^hello/{w=$3} END{print lb, w}')
  [ -z "${r// /}" ] && r="xx  (no response)"
  echo "$t  $r"
  sleep 0.2
done
Enter fullscreen mode Exit fullscreen mode

With that running, I kill HAProxy on lb1:

About 3 seconds of no response, then lb2 answers and the VIP has moved to lb2

  • right after the kill there are about 3 seconds of no response (including curl's 1-second timeout)
  • then lb2 answers, and web1 / web2 balancing carries on
  • the final ip addr shows the VIP has moved to lb2

What happened, according to Keepalived

keepalived logs: lb1 enters FAULT and removes the VIP; lb2 becomes MASTER and sends gratuitous ARPs

Sequence: check fails twice, FAULT, priority 0 advert, lb2 becomes MASTER, gratuitous ARP, client reaches lb2

  1. lb1's check script fails (now returning 7 is curl's "couldn't connect")
  2. after 2 failures it enters FAULT and removes the VIP
  3. lb1 sends a VRRP advert with priority 0 ("I'm out")
  4. lb2 receives it, becomes MASTER and adds the VIP
  5. a gratuitous ARP tells the neighbours "10.0.0.10 now lives at this MAC"

Downtime is roughly check interval × fall + switchover. You can tighten interval / fall to fail over faster, but go too far and a brief hiccup will flip the VIP back and forth (flapping) — it's a trade-off.

Step 6: Bring lb1 back

HAProxy restarted on lb1; after about 5 seconds lb1 takes the VIP back with zero lost requests

  • after restarting HAProxy, once the check passes again (rise 2), the higher-priority lb1 takes the VIP back (preemption)
  • here lb1 was back about 5 seconds after the restart, with zero lost requests

If you don't want the VIP to move back every time, use nopreempt (both nodes must start as state BACKUP) so it stays where it is.

Bonus: when a web server dies

web1 stopped; all requests go to web2

Stop web1 and HAProxy's health check (fall 2) takes it out; everything goes to web2. The VIP doesn't move.
The split of responsibilities is clear: Keepalived makes the LB redundant, HAProxy makes the web tier redundant.

Doing this on AWS

This setup doesn't work as-is in an AWS VPC.

  • a VPC doesn't move IPs based on ARP, so gratuitous ARPs won't move the VIP
  • multicast generally isn't available in a VPC (that's another reason for unicast)

On AWS the usual options are a Keepalived notify script that reassigns a secondary private IP or Elastic IP through the API, or simply using an NLB / ALB.

Takeaways

  • with network namespaces you can test an HA setup on a single Linux box
  • Keepalived's vrrp_script watches HAProxy and releases the VIP when it dies
  • with these settings (1 s interval × 2), downtime was about 3 seconds
  • failback is automatic via preemption, with zero lost requests
  • clouds handle VIPs differently: reassign via API or use a managed LB

References

Top comments (0)