WIUNIX ACADEMY
HAProxy + Keepalived: build an active/standby VRRP load balancer
Page 2 of 6. Review the earlier steps and prerequisites before continuing.
YOUR SYSTEM. YOUR STEPS.
Where are you working?
Choose your OS and version. The prerequisites, commands and troubleshooting follow your environment.
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
Open the HAProxy editor on each load balancer
Both load balancers use the same HTTP routing file. Open it with a privileged editor; the next block is file content, not a shell script. Preserve any unrelated settings only if you have planned an integration.
Use the exact same backend pool and readiness contract on LB-01 and LB-02, or a failover may appear successful while routing to different applications.
LB-01 and LB-02 · terminal with sudoedit
sudoedit /etc/haproxy/haproxy.cfgLET’S BUILD
Define HTTP balancing and true backend health
Save the complete file below on both load balancers. HAProxy binds to the local IPv4 wildcard, so the standby node can be ready without owning the VIP. The frontend uses HTTP port 80 only, and the backend distributes requests round-robin between two existing applications.
The HTTP health rule makes a GET request and accepts only status 200. Replace both server addresses, their port and the Host header to suit your service; keep the tested health URI identical across nodes. A TCP listener, a daemon status or a redirect cannot substitute for this check.
This is deliberately a private HTTP demonstration. For a public service, arrange approved TLS termination, authentication, rate controls and trusted proxy handling in a separate reviewed change. Never publish the sample cleartext listener to arbitrary Internet clients.
File contents · paste into the editor on LB-01 and LB-02; do not execute as shell
global
log /dev/log local0
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5s
timeout client 30s
timeout server 30s
frontend public_http
bind 0.0.0.0:80
default_backend web_apps
backend web_apps
balance roundrobin
option httpchk
http-check send meth GET uri /healthz ver HTTP/1.1 hdr Host lab.internal
http-check expect status 200
server app1 10.20.30.21:8080 check inter 2s fall 3 rise 2
server app2 10.20.30.22:8080 check inter 2s fall 3 rise 2Both files contain the same frontend, backend pool and strict 200 readiness check; all example addresses and Host values have been adapted.
Check policy for backend connections
Keep SELinux enforcing. Record the previous haproxy_connect_any value in the fresh root-only backup and then enable it for this lab. This broadens HAProxy TCP connection permission; a production adaptation should review a narrower backend-port policy.
If the boolean is unavailable or the saved-state file already exists, stop rather than overwriting history. After starting HAProxy, inspect AVCs and the real response: syntax validation is not a service-domain network-permission check.
Both lab VMs · policy inspection
getenforce
sudo bash <<'ROOT'
set -euo pipefail
state=/root/wiunix-academy-ha-backup/haproxy_connect_any.before
test ! -e "$state"
getsebool haproxy_connect_any > "$state"
setsebool -P haproxy_connect_any on
restorecon -v /etc/haproxy/haproxy.cfg
ROOTLET’S BUILD
Validate and start HAProxy on both hosts
Validate the complete saved file before allowing this fresh lab unit to start. A successful check is followed by unmasking and enabling this lab’s HAProxy service; it is not a restart procedure for an existing serving proxy.
From each VM, request the actual local proxied readiness endpoint and require exactly HTTP 200. Stop on a 503, redirect or security-policy denial instead of enabling the VIP.
Both fresh lab VMs · sudo for validation and first start · local curl check
sudo /usr/sbin/haproxy -c -f /etc/haproxy/haproxy.cfg && sudo systemctl unmask haproxy && sudo systemctl enable --now haproxy
sudo systemctl enable haproxy
systemctl is-active haproxy
status=$(curl --noproxy '*' --silent --show-error --max-time 4 \
-o /dev/null -w '%{http_code}' -H 'Host: lab.internal' http://127.0.0.1/healthz) && {
printf 'HTTP %s\n' "$status"
[ "$status" = 200 ]
}EXAMPLE OUTPUT ONLY:
Configuration file is valid
active
HTTP 200Configuration check succeeds; the service is active; the local proxied readiness request exits successfully with HTTP 200.
Create a small unprivileged failover check
Keepalived needs a real readiness result, not just a running HAProxy process. Create a root-owned script that queries the proxied loopback endpoint without credentials and runs under the dedicated academy-vrrp identity.
Open the fresh file below on both VMs. The complete contents follow; keep every parent directory root-owned and non-writable by the checker.
LB-01 and LB-02 · sudoedit · new root-owned script file
sudoedit /usr/local/sbin/wiunix-check-haproxyLET’S BUILD
Connect VRRP health to an actual HTTP response
A balancer is useful only while it can proxy a real readiness response. This script asks the local HTTP listener for the same Host and health path used by HAProxy, and exits successfully only for status 200. One working backend is enough; it does not certify that both applications or the remote client path work.
Curl has a two-second total deadline. Keepalived allows three seconds for the script process, with fall/rise thresholds to avoid reacting to a single transient failure. Those thresholds and neighbor convergence mean recovery takes time; established TCP connections can still break.
File contents · LB-01 and LB-02 · not for direct shell pasting
#!/bin/sh
status=$(/usr/bin/curl --noproxy '*' --silent --show-error \
--connect-timeout 1 --max-time 2 -o /dev/null -w '%{http_code}' \
-H 'Host: lab.internal' http://127.0.0.1/healthz) || exit 1
[ "$status" = 200 ]The script exits 0 for a healthy proxied application and nonzero for a failed HTTP request.