WIUNIX ACADEMY
HAProxy + Keepalived: build an active/standby VRRP load balancer
Page 3 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
Secure the script and test its exit status
The dedicated checker needs read/execute permission, never write permission. Root owns the script and every parent directory. Inspect the whole path; stop if a parent is a symlink to an unreviewed location or is writable by another account. Do not “repair” shared directory ownership blindly.
Run the check as academy-vrrp and retain its actual exit status. A manual success proves Unix permissions and the current HTTP response; Keepalived’s own SELinux/AppArmor context may still deny execution, so inspect its journal after starting the daemon.
LB-01 and LB-02 · sudo for ownership and unprivileged execution
sudo chown root:root /usr/local/sbin/wiunix-check-haproxy
sudo chmod 0755 /usr/local/sbin/wiunix-check-haproxy
namei -l /usr/local/sbin/wiunix-check-haproxy
sudo -u academy-vrrp /usr/local/sbin/wiunix-check-haproxy
check_status=$?
printf 'health-check exit=%s\n' "$check_status"
test "$check_status" -eq 0EXAMPLE OUTPUT ONLY:
health-check exit=0The unprivileged script exits 0; if it does not, Keepalived must not be started until the application readiness path is fixed.
Open Keepalived configuration on LB-01
Start with the preferred node. Both peers begin in BACKUP state; the higher priority only decides the preferred owner when neither node currently holds the VIP. No forced MASTER startup and no automatic preemption are configured.
If your approved network interface or addresses differ from the lab values, update them in both peer configurations. The VRID must match on both nodes and must not collide with another VRRP group on the same network.
LB-01 only · sudoedit
sudoedit /etc/keepalived/keepalived.confLET’S BUILD
Configure the preferred node (LB-01)
LB-01 advertises directly to LB-02 with priority 150. Both peers begin in BACKUP, use the same reserved VRID and keep the health check in an initial failed state until it passes. With explicit weight 0, three consecutive failed checks put this instance in FAULT and withdraw its VIP; the priority is not merely reduced.
The script runs as academy-vrrp. nopreempt lets a healthy current master retain the VIP when the preferred node returns. Initial startup order still affects ownership, and this two-node election has no fencing against a network partition.
File contents · LB-01 only
global_defs {
router_id WIUNIX_LB01
enable_script_security
script_user academy-vrrp academy-vrrp
}
vrrp_script chk_haproxy {
script "/usr/local/sbin/wiunix-check-haproxy"
interval 2
timeout 3
weight 0
init_fail
fall 3
rise 2
}
vrrp_instance VI_HTTP {
state BACKUP
interface ens18
virtual_router_id 51
priority 150
advert_int 1
nopreempt
unicast_src_ip 10.20.30.11
unicast_peer {
10.20.30.12
}
virtual_ipaddress {
10.20.30.100/24 dev ens18
}
track_script {
chk_haproxy
}
}LB-01 names itself, uses its real source address, targets LB-02, and advertises the one agreed VIP/VRID.
Open the second node’s independent configuration
Keepalived needs a distinct file on LB-02, not a mechanical copy of LB-01. Source and peer addresses, router identity and priority must differ, while the VIP, VRID and health policy match.
Check the physical interface name on LB-02: Linux interface names are not guaranteed to be identical on two machines.
LB-02 only · sudoedit
sudoedit /etc/keepalived/keepalived.confLET’S BUILD
Configure the standby candidate (LB-02)
LB-02 uses its own source address, the opposite peer and priority 100. Its VIP, VRID, interface intent and health contract must match LB-01. Check the interface on this VM independently; if its name differs, set the form before copying this node’s file.
BACKUP plus nopreempt avoids forced ownership and automatic failback. A blocked protocol-112 path can still produce two owners even when both files validate. Do not treat unicast or a configuration test as split-brain protection.
File contents · LB-02 only
global_defs {
router_id WIUNIX_LB02
enable_script_security
script_user academy-vrrp academy-vrrp
}
vrrp_script chk_haproxy {
script "/usr/local/sbin/wiunix-check-haproxy"
interval 2
timeout 3
weight 0
init_fail
fall 3
rise 2
}
vrrp_instance VI_HTTP {
state BACKUP
interface ens18
virtual_router_id 51
priority 100
advert_int 1
nopreempt
unicast_src_ip 10.20.30.12
unicast_peer {
10.20.30.11
}
virtual_ipaddress {
10.20.30.100/24 dev ens18
}
track_script {
chk_haproxy
}
}LB-02 points to LB-01, keeps priority 100 and has exactly the same VIP, VRID and script as LB-01.
LET’S BUILD
Check the host security policy before starting services
Add a persistent exact-path file-context rule for Keepalived’s script entry type. This transitions the script into an unconfined SELinux script domain, broader than a custom least-privilege policy; apply it only to this root-owned lab script, never a whole directory or user-writable code.
If the type or path mapping is unavailable, stop and review the installed vendor policy. Do not disable SELinux or generate an unchecked audit2allow policy. The Unix UID remains academy-vrrp. Inspect actual Keepalived execution and AVCs after startup; absence of an AVC alone does not prove success.
Both lab VMs · sudo · review the selected distribution’s active policy
sudo chown root:root /etc/haproxy/haproxy.cfg /etc/keepalived/keepalived.conf
sudo chmod 0644 /etc/haproxy/haproxy.cfg
sudo chmod 0600 /etc/keepalived/keepalived.conf
getenforce
sudo semanage fcontext -a -t keepalived_unconfined_script_exec_t '/usr/local/sbin/wiunix-check-haproxy'
sudo restorecon -v /usr/local/sbin/wiunix-check-haproxy /etc/keepalived/keepalived.conf
ls -lZ /usr/local/sbin/wiunix-check-haproxy
sudo ausearch -m AVC,USER_AVC -ts recent
systemctl cat haproxy keepalivedConfig ownership is correct and active security profiles are known; resolve policy denials before enabling VRRP.