Illustrative WIUNIX Academy IT engineering workspace with server/network equipment and an instructional board: HAProxy + Keepalived: build an active/standby VRRP load balancer

WIUNIX ACADEMY

HAProxy + Keepalived: build an active/standby VRRP load balancer

Build two HAProxy nodes with a shared virtual IP, route traffic to healthy HTTP backends and verify real failover with Keepalived VRRP—on your selected Linux version.

Intermediate55 min readUpdated
Make this guide yours

YOUR SYSTEM. YOUR STEPS.

Where are you working?

Choose your OS and version. The prerequisites, commands and troubleshooting follow your environment.

RHEL 9.8Reviewed against official documentation ·

USE YOUR OWN VALUES

Configure this tutorial

Set the example values for your environment; related explanations and copy panels update together. Never enter passwords, private keys or tokens. Values stay in this tab and are not sent to the server or share links. Check actual DNS, certificates and network policy before running commands.

Different IP addresses, domains or ports? Open the settings below, change the values and select “Apply to tutorial”, then copy the updated commands.

Set values for my environment8 editable values

THE OUTCOME

What will you have at the end?

Route real HTTP requests through one floating VIP, confirm exactly one owner, then stop the active load balancer and verify a third client still reaches a healthy backend after VIP transfer.

01External clientHTTP request to one virtual IP · 10.20.30.100:80
02Keepalived VRRPLB-01 10.20.30.11 ⇄ LB-02 10.20.30.12 · one VIP owner; IP protocol 112
03HAProxy poolActive owner forwards round-robin with strict /healthz status-200 checks
04Applicationsapp1 10.20.30.21:8080 · app2 10.20.30.22:8080
Supported operating systems and versions

Documentation-reviewed on 2026-11-05: Ubuntu 26.10, 26.04/24.04/22.04 LTS; Debian 13/12; standard current RHEL, Rocky and AlmaLinux 10.2/9.8; Arch/Manjaro rolling. This is a fresh disposable two-VM private HTTP lab, not an integration into an existing proxy. RPM uses native repositories, firewalld and SELinux; Arch uses a full rolling update and /usr/bin launchers. No failover or distribution VM was executed for this revision; example output is illustrative. Debian 12 LTS package coverage and Ubuntu 26.10’s short support window need review for a real deployment.

Before you begin

  • Two fresh disposable systemd VMs of the same selected distribution, with sudo, snapshots and independent console access; neither HAProxy nor Keepalived nor the tutorial checker identity is already installed. Do not run this installation during an incident.
  • One approved unused IPv4 virtual address in the same private broadcast domain as both node interfaces; the network must permit VIP ARP ownership movement and peer-to-peer VRRP IP protocol 112.
  • Two functioning, independently supervised HTTP backend services with a readiness endpoint that returns HTTP 200. Application response and readiness checks must be reachable from both load balancers.
  • Administrative control of host and provider firewalls. A third client to verify the VIP, and permission to run a short disruptive failover test.
  • Lab-only plaintext HTTP on a private subnet. For Internet publication, design a separately reviewed TLS and application-security layer.
01

LET’S BUILD

Identify the two load-balancer hosts

We will give a private HTTP service one address that can move between two independent load balancers. Use two fresh disposable VMs and a third client; VRRP needs an approved shared layer-2 network and a provider that permits secondary-IP ownership and gratuitous ARP movement. Unicast peer advertisements do not remove those address-delivery requirements.

Set the form to the real node, backend, VIP and interface values before copying commands. The VIP must be reserved and unused, and must not be statically assigned to either VM. The form enforces network membership and uniqueness; it does not discover whether another machine already owns that address. The network prefix comes from your CIDR, not a hard-coded /24.

Confirm systemd is PID 1 and port 80 has no unrelated listener. Keep separate console access. VRRP is an election protocol without fencing or quorum here: a network partition can make both nodes master, so prevent client access whenever ownership is ambiguous.

Bash

LB-01 and LB-02 · each server terminal · read-only inspection with sudo for socket ownership

cat /etc/os-release
ps -p 1 -o comm=
ip -br -4 address
ip -4 route
sudo ss -ltnp | grep -E ":80[[:space:]]" || true
How do I know it worked?

Both machines match the selected, reviewed Linux version; PID 1 is systemd. Their real interface addresses share the approved network and port 80 has no unexpected owner.

02

LET’S BUILD

Guard the fresh lab before package installation

Use two disposable fresh VMs. This guard refuses existing application directories, units, masks, backup paths and checker identities; it does not migrate an existing load balancer. It masks the new units before installation so package scripts cannot expose an unconfigured listener or advertise a sample VIP.

If it stops, inspect that VM and choose a fresh one. Do not remove the conflicting paths or unmask someone else’s service to bypass the guard. A failed installation may leave these two lab masks; retain them until this lab is configured or retire the disposable VM.

Bash

Both fresh lab VMs · sudo · run once before installing packages

sudo bash <<'ROOT'
set -euo pipefail
for path in /etc/haproxy /etc/keepalived /root/wiunix-academy-ha-backup /usr/local/sbin/wiunix-check-haproxy /etc/systemd/system/haproxy.service /etc/systemd/system/keepalived.service; do
  if [ -e "$path" ] || [ -L "$path" ]; then
    printf 'STOP: existing path: %s\n' "$path" >&2; exit 1
  fi
done
if getent passwd academy-vrrp >/dev/null || getent group academy-vrrp >/dev/null; then
  echo 'STOP: checker identity already exists' >&2; exit 1
fi
for unit in haproxy keepalived; do
  if [ "$(systemctl show "$unit.service" -p LoadState --value)" != not-found ]; then
    echo "STOP: existing unit $unit" >&2; exit 1
  fi
done
install -d -m 0700 /root/wiunix-academy-ha-backup
systemctl mask haproxy.service keepalived.service
ROOT
How do I know it worked?

No existing deployment was found; a fresh root-only backup directory and two lab-created masks now exist.

03

LET’S BUILD

Install packages from your distribution

Install from the selected distribution’s official repositories after the fresh-lab guard has masked both services. Review the complete transaction; a package operation is a planned change on these disposable VMs, not a troubleshooting command for a serving node. Curl and packet-capture tools are installed now, before backend checks.

Keep both services masked until their reviewed configuration is ready. Ubuntu/Debian use /usr/sbin launchers; Arch/Manjaro package binaries live in /usr/bin. Inspect the actual systemd ExecStart as well: a custom drop-in, extra configuration directory or different executable changes what the service loads. Stop and reconcile unexpected unit overrides rather than assuming a manual config check covers them.

Bash

LB-01 and LB-02 · server terminal with sudo

sudo dnf repolist --enabled
sudo dnf install haproxy keepalived curl iproute tcpdump shadow-utils policycoreutils-python-utils audit
rpm -q haproxy keepalived selinux-policy-targeted
sudo /usr/sbin/haproxy -v
sudo /usr/sbin/keepalived --version
sudo cat /usr/lib/systemd/system/haproxy.service /usr/lib/systemd/system/keepalived.service
How do I know it worked?

Both binaries print versions and the package installation succeeds. No claim about frontend, backend or VIP health follows from this alone.

04

LET’S BUILD

Prove each application backend is ready

Before configuring failover, have two independently supervised HTTP applications listening on app1 = 10.20.30.21:8080 and app2 = 10.20.30.22:8080. Both must return HTTP 200 to GET /healthz under the Host value lab.internal, and serve an actual response at /. Adapt the addresses, port, Host and readiness path to your application; this guide does not create or expose an unmaintained demo web server.

Run these requests from both load balancers. HTTP 200 from the readiness endpoint matters more than a listening TCP port; if an application returns redirects, login HTML or errors, fix its health endpoint before continuing. This test is only backend readiness, not VIP failover.

Bash

LB-01 and LB-02 · ordinary shell user · replace backend example values on each node

curl --noproxy '*' -i --max-time 4 -H 'Host: lab.internal' http://10.20.30.21:8080/healthz
curl --noproxy '*' -i --max-time 4 -H 'Host: lab.internal' http://10.20.30.22:8080/healthz
curl --noproxy '*' --fail --silent --show-error --max-time 4 -H 'Host: lab.internal' http://10.20.30.21:8080/
EXAMPLE OUTPUT
EXAMPLE ONLY — supplied by an application, not measured on WIUNIX production:
HTTP/1.1 200 OK
Content-Type: text/plain

OK
How do I know it worked?

Both health requests return status 200, and the application root responds. If only one backend passes, do not call this a healthy two-backend design.

05

LET’S BUILD

Review the network and firewall path

Find the interface’s actual firewalld zone and read its existing policy. These source-restricted rules affect runtime only: TCP 80 from the lab subnet and VRRP from the two peer addresses. If firewalld is inactive or no interface zone is assigned, stop and review the actual firewall; do not silently change the default zone. Existing runtime rules are retained; only newly added rules and the actual zone are recorded in root-only receipts for retirement.

Before any firewall reload or node reboot, either stop Keepalived on both nodes or deliberately add these reviewed rules to permanent configuration too. Losing the VRRP allowance while both daemons run can create duplicate VIP owners. Avoid runtime-to-permanent, which also saves unrelated changes. Never remove a pre-existing rule during retirement.

Backends separately need TCP 8080 only from the two load balancers. Review upstream ACLs and secondary-IP/ARP capability; successful local firewall commands do not prove those paths.

Bash

LB-01 and LB-02 · sudo where UFW is the active firewall · replace peer subnet and interface before use

sudo bash <<'ROOT'
set -euo pipefail
firewall-cmd --state
zone=$(firewall-cmd --get-zone-of-interface=ens18)
test -n "$zone" && test "$zone" != 'no zone' || { echo 'STOP: review interface zone first' >&2; exit 1; }
firewall-cmd --zone="$zone" --list-all
receipt=/root/wiunix-academy-ha-backup/firewalld-added.rules
test ! -e "$receipt"
test ! -e /root/wiunix-academy-ha-backup/firewalld-zone.before
umask 077
printf '%s\n' "$zone" > /root/wiunix-academy-ha-backup/firewalld-zone.before
: > "$receipt"
while IFS= read -r rule; do
  if firewall-cmd --zone="$zone" --query-rich-rule="$rule"; then
    printf 'Already present: %s\n' "$rule"
  else
    result=$?
    test "$result" -eq 1 || exit "$result"
    printf '%s\n' "$rule" >> "$receipt"
    firewall-cmd --zone="$zone" --add-rich-rule="$rule"
  fi
done <<'RULES'
rule family="ipv4" source address="10.20.30.0/24" port port="80" protocol="tcp" accept
rule family="ipv4" source address="10.20.30.11/32" protocol value="vrrp" accept
rule family="ipv4" source address="10.20.30.12/32" protocol value="vrrp" accept
RULES
firewall-cmd --zone="$zone" --list-rich-rules
ROOT
How do I know it worked?

The real network and active security policy allow peer-to-peer IP protocol 112, permitted lab clients can reach TCP 80, and backends accept TCP 8080 from both balancers. UFW messages are not proof of upstream reachability.

06

LET’S BUILD

Save the current configuration before editing

Save the newly installed vendor files before replacing them. The fresh-lab guard already reserved a root-only backup directory; this step refuses to overwrite saved backups and records when a vendor file is absent. These are factory-file snapshots, not a backup of a pre-existing production service.

Create a dedicated non-login checker account so the group name does not depend on nobody/nogroup distribution conventions. Both services stay masked. If any command fails, resolve it before continuing; never rerun the whole install over partial state without inspecting it.

Bash

LB-01 and LB-02 · sudo · dedicated nodes only

sudo bash <<'ROOT'
set -euo pipefail
test -d /root/wiunix-academy-ha-backup
for name in haproxy keepalived; do
  case "$name" in
    haproxy) path=/etc/haproxy/haproxy.cfg ;;
    keepalived) path=/etc/keepalived/keepalived.conf ;;
  esac
  test ! -e "/root/wiunix-academy-ha-backup/$name.cfg.before"
  test ! -e "/root/wiunix-academy-ha-backup/$name.absent"
  if [ -L "$path" ]; then echo "STOP: symlink $path" >&2; exit 1; fi
  if [ -f "$path" ]; then
    cp -a "$path" "/root/wiunix-academy-ha-backup/$name.cfg.before"
  else
    touch "/root/wiunix-academy-ha-backup/$name.absent"
  fi
done
install -d -o root -g root -m 0755 /etc/haproxy /etc/keepalived /usr/local/sbin
groupadd --system academy-vrrp
useradd --system --gid academy-vrrp --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin academy-vrrp
getent passwd academy-vrrp
getent group haproxy
ROOT
How do I know it worked?

The backup directory is root-only and contains the original HAProxy file, plus the Keepalived file when one existed.

Explore all tutorials ↗