WIUNIX ACADEMY
HAProxy + Keepalived: build an active/standby VRRP load balancer
Page 5 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
LET’S BUILD
Prove failover from a third client
Now repeat the same application request from a client outside both load balancers. Success requires an HTTP 2xx from the unchanged VIP and the intended backend application after ownership transfer.
VIP movement or Keepalived state by itself cannot prove user-visible service recovery. If the client times out, inspect neighbor ARP and provider secondary-IP forwarding before calling this a success.
The panel prints the returned application body after checking status and peer address. Compare it with your intended application; a generic 200 page from the wrong service is not a pass. The temporary body file is removed when the panel exits.
Third independent client only; replace example Host and VIP.
(
set -euo pipefail
body=$(mktemp)
trap 'rm -f -- "$body"' EXIT
result=$(curl --noproxy '*' --silent --show-error --connect-timeout 3 --max-time 8 \
-H 'Host: lab.internal' -o "$body" -w '%{http_code} %{remote_ip}' \
http://10.20.30.100/)
printf 'HTTP status and peer: %s\n' "$result"
case "$result" in
2[0-9][0-9]\ *) : ;;
*) echo 'FAIL: expected HTTP 2xx without redirect' >&2; exit 1 ;;
esac
test "${result#* }" = '10.20.30.100'
cat -- "$body"
printf '\nCompare this response with the intended application.\n'
)EXAMPLE OUTPUT ONLY — not a measured deployment:
HTTP status and peer: 200 10.20.30.100The third client returns a real HTTP 2xx application response via the VIP after takeover.
LET’S BUILD
Recover the old node without automatic failback
Start HAProxy again on the node intentionally stopped for the test. Its local HTTP check must recover. With nopreempt on both peers, the currently healthy master ordinarily keeps the VIP instead of bouncing it back to the original higher-priority machine.
Check addresses again on both machines and verify the VIP from the client. For a deliberate failback later, schedule a separate transfer in a maintenance window by stopping Keepalived on the current master only after proving the recovered node healthy. Do not confuse restoration of a daemon with restored VIP ownership.
Previously stopped LB-01 (or actual failed host) · service recovery with sudo
sudo systemctl start haproxy
systemctl is-active haproxy
sudo -u academy-vrrp /usr/local/sbin/wiunix-check-haproxy
ip -br -4 address show dev ens18The recovered node has a healthy local proxied response, while the peer retains the VIP until a planned manual handback (unless another fault occurred).
Troubleshoot the failing layer, not the symptom
Check four distinct layers in order: backend readiness, local HAProxy HTTP, exclusive VIP ownership, then a real client request through the network. Diagnosis commands are grouped inside each disclosure below so they remain copyable. Do not assume a successful config test, a running daemon or an ARP record alone proves application continuity.
If a failure could cause duplicate VIP owners, protect the client path first. For one failed backend, repair that application; for two failed backends, the VIP may correctly be withdrawn by the zero-weight checker policy.
Both nodes claim the VIP
Likely split brain: VRRP IP protocol 112 blocked, peer addresses reversed, bad VRID or wrong interface. Compare the actual peers and observe advertisements on both links. Do not allow client traffic until ownership is unique.
sudo timeout --signal=TERM --kill-after=2s 15s tcpdump -n -i ens18 -c 8 'ip proto 112'
sudo journalctl -u keepalived -n 80 --no-pager
ip -br -4 address show dev ens18Neither node holds the VIP
Both script checks may have failed, leaving both instances in FAULT. Check the local HTTP response as the restricted script user and the real backend responses. Never force the VIP when all backends are down.
sudo -u academy-vrrp /usr/local/sbin/wiunix-check-haproxy; printf 'exit=%s\n' "$?"
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
sudo journalctl -u keepalived -n 80 --no-pagerVIP moves but clients time out
A virtual address appearing on a node does not prove external reachability. Inspect the client neighbor entry, the owner’s interface and routing, local packet capture and provider-side secondary-IP or anti-spoofing policy. Packet capture is bounded to 15 seconds; timeout status alone does not establish a network fault.
ip -4 neigh show 10.20.30.100
ip -4 route get 10.20.30.100
sudo timeout --signal=TERM --kill-after=2s 15s tcpdump -n -i ens18 -c 12 'arp or (ip and tcp port 80)'HAProxy answers 503 or marks backends down
A 503 usually means no eligible HTTP backend. The configured health request requires status 200, the correct Host and a reachable application port. Recheck that contract from both load balancers; do not switch off checks just to make the error disappear.
sudo /usr/sbin/haproxy -c -f /etc/haproxy/haproxy.cfg
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
sudo journalctl -u haproxy -n 60 --no-pagerScript error or Keepalived remains FAULT
Inspect the root-owned script path, the dedicated academy-vrrp account, curl and the loopback response. Compare a manual run with actual Keepalived execution and security denials; Unix UID permissions alone do not prove the service domain can run it.
sudo /usr/sbin/keepalived --config-test -f /etc/keepalived/keepalived.conf
ls -l /usr/local/sbin/wiunix-check-haproxy
sudo -u academy-vrrp /usr/local/sbin/wiunix-check-haproxy; printf 'exit=%s\n' "$?"
sudo journalctl -u keepalived -n 80 --no-pagerConfiguration is valid but service cannot bind port 80
An existing listener or service policy may own the address. Identify the actual process. Do not stop a production web server automatically to clear a port conflict.
sudo ss -ltnp | grep -E ':80[[:space:]]' || true
sudo journalctl -u haproxy -n 80 --no-pagerOperate and maintain the pair without blind restarts
Monitor the service from a third host, not only the two load balancers. Track VIP ownership, HAProxy responses, backend readiness, VRRP state, packet loss and user-visible errors. Plan application-state/session handling independently: this setup is not a shared-session database and does not preserve arbitrary established TCP connections.
After operating-system or package updates, validate the config and test one node at a time. Keep the network VRRP exception and failover IP capability under change control. Retain known-good configuration and plan controlled failover tests, rather than stopping both nodes together.
The commands here are nondisruptive status checks on the load balancers; the client-side verification from an outside network remains a separate requirement.
LB-01 and LB-02 · routine read-only health review
sudo /usr/sbin/haproxy -c -f /etc/haproxy/haproxy.cfg
sudo /usr/sbin/keepalived --config-test -f /etc/keepalived/keepalived.conf
systemctl is-active haproxy keepalived
ip -br -4 address show dev ens18
sudo journalctl -u keepalived -n 40 --no-pagerSafe rollback: withdraw the lab VIP first
This rollback is for the dedicated new installation in this article, not a shared pre-existing VRRP cluster. Schedule a maintenance window because removing the VIP ends client access to this lab entrance. Stop Keepalived on both nodes so neither can advertise the address while configuration is being restored.
Do not remove the physical IP addresses, alter SSH routing or flush the firewall. If either host participated in an existing VRRP service before this tutorial, do not use this generic shutdown procedure; restore that service according to your retained change plan.
LB-01 and LB-02 · coordinated maintenance · sudo
sudo systemctl disable --now keepalived
ip -br -4 address show dev ens18The lab VIP is absent from both physical nodes. This intentionally removes the test entry point, but does not delete application data.
Restore the previous HAProxy file and retire the lab check
After Keepalived is stopped on both VMs and the VIP is absent, restore the vendor files from this lab’s root-only snapshots, or remove only a file recorded as originally absent. The command leaves HAProxy stopped and disabled, because a restored factory file is not necessarily a usable application configuration.
Keep the backup and checker script until you have checked that no other service references them. Remove only this lab’s recorded firewall changes and SELinux customization; do not flush a firewall or delete shared user accounts. Retiring the disposable VMs after preserving evidence is the simplest full lab cleanup.
LB-01 and LB-02 · sudo · only after reviewing the saved backup and script ownership
sudo bash <<'ROOT'
set -euo pipefail
if systemctl is-active --quiet keepalived; then echo "STOP: Keepalived is active" >&2; exit 1; fi
if ip -o -4 address show dev ens18 | grep -Fq " 10.20.30.100/"; then echo "STOP: VIP still present" >&2; exit 1; fi
for name in haproxy keepalived; do
case "$name" in
haproxy) path=/etc/haproxy/haproxy.cfg ;;
keepalived) path=/etc/keepalived/keepalived.conf ;;
esac
if [ -f "/root/wiunix-academy-ha-backup/$name.cfg.before" ]; then
cp -a "/root/wiunix-academy-ha-backup/$name.cfg.before" "$path"
elif [ -f "/root/wiunix-academy-ha-backup/$name.absent" ]; then
rm -f -- "$path"
else
echo "STOP: missing backup state for $name" >&2; exit 1
fi
done
systemctl disable --now haproxy
ROOTThe retained factory-file state is restored, HAProxy remains stopped and disabled, the VIP stays withdrawn and backend data has not been erased.