How To Install And Configure HAProxy For High Availability
HAProxy is a fast, reliable load balancer and reverse proxy for distributing client requests across multiple application servers. When it runs on two proxy nodes with a shared virtual IP address, it can continue accepting connections if one node fails.
This guide uses RHEL-compatible commands for RHEL, CentOS Stream and Fedora. The same design suits websites, APIs and internal services hosted in Australian regions such as Sydney, Melbourne, Brisbane or Perth, where a short outage can affect users across very different network paths.
Plan The High Availability Design
A resilient HAProxy deployment usually contains two load balancers, two or more backend servers, and Keepalived to move a virtual IP address between the load balancers. Clients connect to the virtual IP rather than to an individual proxy node.
| Component | Example | Purpose |
|---|---|---|
| Proxy node one | 10.0.10.11 |
Active HAProxy server |
| Proxy node two | 10.0.10.12 |
Standby HAProxy server |
| Virtual IP | 10.0.10.10 |
Stable client-facing address |
| Backend servers | 10.0.20.21, 10.0.20.22 |
Application or web services |
| Public service | www.example.com |
DNS points to the virtual IP |
Assign fixed addresses and matching HAProxy configurations to both proxy nodes. Permit TCP ports 80 and 443 from approved client networks, and allow the backend service port, such as 8080, between the proxies and application servers.
For administrators supporting Australian customers, placing nodes in separate availability zones within a Sydney or Melbourne cloud region can reduce the effect of a host failure. A second region may be appropriate for disaster recovery, while normal load balancing remains local to minimise latency.
Install HAProxy Packages
Update package metadata and install HAProxy:
sudo dnf update -y
sudo dnf install -y haproxy
Confirm the installed version and enable the service at boot:
haproxy -v
sudo systemctl enable haproxy
Before editing the configuration, make a backup and inspect the service account:
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.orig
getent passwd haproxy
A useful Linux reference such as this practical Linux resource can help learners review permissions, systemd and network troubleshooting before changing a production proxy.
Build The Frontend Configuration
Edit /etc/haproxy/haproxy.cfg and define global settings, default behaviour, a frontend listener and a backend pool:
global
log 127.0.0.1 local2
user haproxy
group haproxy
daemon
stats socket /run/haproxy/admin.sock mode 660 level admin
defaults
mode http
log global
option httplog
option dontlognull
timeout connect 5s
timeout client 30s
timeout server 30s
frontend public_http
bind *:80
default_backend application_nodes
backend application_nodes
balance roundrobin
option httpchk GET /health
http-check expect status 200
server app01 10.0.20.21:8080 check
server app02 10.0.20.22:8080 check
The health endpoint should return HTTP 200 only when the application is ready to serve traffic. roundrobin distributes requests evenly, while leastconn can be better for long-lived sessions. If an application stores sessions locally, configure shared sessions or use a carefully designed cookie-based persistence method.
Validate the file before starting HAProxy:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo systemctl restart haproxy
sudo systemctl status haproxy --no-pager
Protect And Tune The Proxy
Open the required firewall ports with firewalld:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
SELinux is normally compatible with HAProxy when it binds to standard ports. If a custom port is required, label it correctly rather than disabling enforcement:
sudo semanage port -a -t http_port_t -p tcp 8080
For HTTPS, terminate TLS at HAProxy with a certificate and private key stored in a restricted directory. Keep private keys readable only by root or the HAProxy service account, and schedule renewal before expiry. Australian organisations should also account for Privacy Act obligations when logging client addresses and request data.
Add High Availability With Keepalived
Install Keepalived on both proxy nodes:
sudo dnf install -y keepalived
sudo systemctl enable keepalived
On the active node, create /etc/keepalived/keepalived.conf:
vrrp_script check_haproxy {
script "/usr/bin/systemctl is-active --quiet haproxy"
interval 2
weight 20
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 51
priority 110
advert_int 1
authentication {
auth_type PASS
auth_pass change-this
}
virtual_ipaddress {
10.0.10.10/24
}
track_script {
check_haproxy
}
}
On the standby node, use the same virtual router ID and virtual IP, but set state BACKUP, change the priority to 100, and use the correct network interface. Allow VRRP protocol traffic in the network security policy, then start the service:
sudo systemctl start keepalived
ip addr show
Keepalived must be tested on the actual network. Some cloud providers require a floating-IP feature instead of multicast or VRRP, so check the provider’s networking model before deploying this pattern.
Validate Traffic And Failover
From a client machine, send repeated requests and observe the backend responses:
for i in {1..10}; do curl -s http://10.0.10.10/; sleep 1; done
Check service logs when a backend is marked down:
sudo journalctl -u haproxy -f
You can also query the HAProxy statistics socket:
echo "show stat" | sudo socat stdio /run/haproxy/admin.sock
Test node failure by stopping HAProxy on the active proxy:
sudo systemctl stop haproxy
The virtual IP should move to the standby node, and new requests should continue through the remaining proxy. Restore the service afterwards and verify that failback behaviour matches your operational policy. Test during a maintenance window rather than during peak periods such as Boxing Day sales or end-of-financial-year processing.
Operate And Maintain The Cluster
Monitor HAProxy process health, backend response time, connection counts, certificate expiry and Keepalived state. Centralise logs where possible, but avoid recording sensitive request details unnecessarily. Alert when all servers in a backend are unavailable, since a healthy proxy process does not guarantee a healthy application.
Configuration management is valuable for keeping both nodes identical. If applications are containerised, review this rootless Podman guide when deciding how services should run behind the proxy. Keep configuration files in version control, validate changes with haproxy -c, and reload rather than restart when uninterrupted connections matter.
Document the virtual IP, firewall rules, DNS records, certificate locations and recovery steps. The practical operating standard is simple: configure both proxy nodes consistently, verify health checks, simulate failure, and confirm that clients still reach a working backend through the shared virtual IP.