Load Balancing Fundamentals Series

🔁 A Top 1% Hands-On for Making Two HAProxy Servers Redundant With keepalived and Experiencing Automatic VIP Failover

Introduction

This article is part of the Top 1% Series Complete Article Guide, and the ninth article in the Load Balancing Fundamentals Series. You can follow along with two of the Ubuntu Server environments set up in the Hands-On Prep Manual (set these up separately from the backend servers used in the HAProxy hands-on).

Prerequisite Knowledge

Getting the Big Picture

This hands-on covers four steps.

graph LR
    Step1["Step 1<br/>Install HAProxy<br/>on both servers"]
    Step2["Step 2<br/>Configure keepalived and<br/>assign a shared VIP"]
    Step3["Step 3<br/>Confirm which unit<br/>is currently Master"]
    Step4["Step4<br/>Stop the Master and<br/>confirm failover"]
    Step1 --> Step2 --> Step3 --> Step4

Hands-On Steps

Step 1: Install HAProxy on Both Servers

On both of your Ubuntu Server machines (say, 10.0.0.21 and 10.0.0.22), install HAProxy.

sudo apt update
sudo apt install -y haproxy

Append a simple configuration to /etc/haproxy/haproxy.cfg for verification (identical content on both servers).

frontend http_front
    bind *:80
    default_backend http_back

backend http_back
    server local 127.0.0.1:8080 check

Start a simple web server on both machines, for verification.

mkdir -p /tmp/web && echo "Response from $(hostname)" > /tmp/web/index.html
cd /tmp/web && nohup python3 -m http.server 8080 &
sudo systemctl restart haproxy

Step 2: Install and Configure keepalived on Both Servers

sudo apt install -y keepalived

On server 1 (Master, say 10.0.0.21), create the following in /etc/keepalived/keepalived.conf.

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass lb_lab_secret
    }
    virtual_ipaddress {
        10.0.0.100/24
    }
}

On server 2 (Backup, say 10.0.0.22), create the following, with only state and priority changed.

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass lb_lab_secret
    }
    virtual_ipaddress {
        10.0.0.100/24
    }
}

The server with the higher priority value becomes Master preferentially. As covered in the previous article, virtual_router_id must match exactly across every server in the same VRRP group. Start the service on both servers.

sudo systemctl enable --now keepalived
Why Is the `auth_pass` Setting Necessary?

VRRP’s liveness packet (the Advertisement) gets broadcast or multicast within the same network. Without the lightweight authentication auth_pass provides, you’d risk accepting an Advertisement from an unrelated VRRP group elsewhere on the same network, or a forged one sent by a malicious third party, triggering an unintended failover. This hands-on uses simple plaintext authentication (PASS), but in a genuine production environment, the more robust fix, as covered in the previous article, is isolating VRRP’s own liveness-check communication path onto a dedicated network.

Step 3: Confirm Which Server Is Currently Master

On server 1 (presumed Master), confirm the VIP is actually assigned to it.

ip addr show eth0

Output (relevant part):

inet 10.0.0.21/24 ...
inet 10.0.0.100/24 scope global secondary eth0

You can confirm server 1 has the VIP (10.0.0.100) attached as a secondary, alongside its ordinary IP address (10.0.0.21). Running ip addr show eth0 on server 2 shows no VIP at all.

From a different machine on the network, try accessing the VIP.

curl http://10.0.0.100/

Output:

Response from lb01

You confirmed server 1’s (Master’s) hostname comes back in the response.

Step 4: Stop the Master and Confirm Failover

On server 2 (Backup), monitor VRRP’s liveness packets while stopping server 1.

Run on server 2 (to monitor):

sudo tcpdump -i eth0 vrrp

From a different machine, stop keepalived on server 1:

ssh 10.0.0.21 "sudo systemctl stop keepalived"

Watching server 2’s tcpdump output, you can see that after server 1’s Advertisements stop arriving, server 2 itself starts sending Advertisements within a few seconds.

On server 2, check the VIP’s state.

ip addr show eth0

Output (relevant part):

inet 10.0.0.22/24 ...
inet 10.0.0.100/24 scope global secondary eth0

The VIP has been handed off to server 2. Try accessing the VIP again.

curl http://10.0.0.100/

Output:

Response from lb02

You confirmed the responding server switched from server 1 to server 2, with the client side never noticing a thing — its connection target stays exactly 10.0.0.100, unchanged. This is Gratuitous ARP’s instant switchover at the network layer, actually doing its job.

Try running sudo systemctl start keepalived on server 1 to bring it back, and confirm it reclaims the Master role again, thanks to its higher priority value.

What a Pro Sees Here (Top 1% Understanding)

Failover Speed Is Directly Tied to What’s Actually Switching Over

The failover you confirmed in Step 4 completed in just a few seconds. As covered in the previous article, this is because VRRP never rewrites a DNS answer — it directly rewrites the VIP’s actual location, at the network layer. In contrast to GSLB’s data-center-level failover carrying a minutes-long delay tied to DNS TTL, a VRRP switchover within the same network completes in seconds. The point of this hands-on is to let you feel firsthand how a difference in which layer the mechanism actually operates at shows up as a concrete difference in how fast the failover actually is.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. Neither server shows the VIP with ip addr show: Check the virtual_ipaddress setting in keepalived.conf, and check systemctl status keepalived to confirm the service actually started correctly.
  2. Both servers show the VIP at the same time (split-brain): Check whether virtual_router_id matches on both servers, and check whether VRRP’s liveness packets are actually arriving (confirm with tcpdump).
  3. Recovering the Master unit doesn’t restore its Master role: Check whether nopreempt is set in keepalived.conf. With this setting present, a higher priority value won’t automatically reclaim the role.

Summary

Takeaways to Apply Today

  1. When configuring keepalived, set a clear difference in the priority value, deliberately pinning the Master/Backup roles.
  2. When verifying failover speed, build the habit of actually observing VRRP packets with tcpdump and confirming the mechanism with your own eyes.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).