Installing and configuring HAProxy for load balancing on CentOS
HAProxy is a fast TCP and HTTP reverse proxy that distributes requests across healthy backend servers. On CentOS, it centralises TLS termination, routing and health checks while keeping client-facing addresses stable.
CentOS 7 and CentOS 8 are end-of-life, so these steps target CentOS Stream 9 and compatible RHEL-based systems. On an archived CentOS 7 lab, dnf can be replaced with yum, but that host should not carry production traffic.
Install HAProxy on CentOS Stream
Start with a patched host, a resolvable hostname and fixed addressing. Install and enable the reverse proxy:
sudo dnf update -y
sudo dnf install haproxy
sudo systemctl enable --now haproxy
HAProxy distributes traffic but is not highly available by itself. A second node can use Keepalived for a virtual IP, while cloud platforms can place a provider load balancer in front of HAProxy. Keep configuration identical across nodes and manage it under an authorised administrator's version control.
Configure frontends and backend pools
The main configuration file is /etc/haproxy/haproxy.cfg. A minimal web configuration looks like this:
global
log /dev/log local0
maxconn 20000
stats socket /var/lib/haproxy/admin.sock mode 660 level admin expose-fd listeners
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5s
timeout client 30s
timeout server 30s
frontend http_in
bind *:80
default_backend app_pool
backend app_pool
balance roundrobin
option httpchk GET /healthz
http-check expect status 200
server app1 10.20.0.11:8080 check inter 2s fall 3 rise 2
server app2 10.20.0.12:8080 check inter 2s fall 3 rise 2
Replace the example addresses with the real backend nodes. The /healthz endpoint must return HTTP 200 only when the application and its critical dependencies are ready. HAProxy marks a node down after three failed probes and returns it after two successes; tune inter, fall and rise for slow-starting applications. Use leastconn for uneven workloads or a cookie or source rule when session affinity is required.
HTTP mode fits web traffic, but database and other TCP-only services belong in dedicated mode tcp sections. A rare legacy backend may run under DOS/4GW; consult the DOS/4GW runtime reference before setting its transport, idle timeout and health-check type. HAProxy should normally terminate client TLS while connecting to backends over protected private networking.
Secure TLS, statistics, and host access
Place the combined certificate and private key in /etc/haproxy/certs/site.pem, owned by root:haproxy with mode 640. Add TLS termination and redirect ordinary HTTP traffic:
frontend http_in
bind *:80
http-request redirect scheme https code 301 always
frontend https_in
bind *:443 ssl crt /etc/haproxy/certs/site.pem alpn h2,http/1.1
default_backend app_pool
Open only the ports required by the public service:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Keep the statistics interface on loopback and access it through an SSH tunnel or private management network:
listen stats
bind 127.0.0.1:9000
stats enable
stats uri /stats
stats refresh 10s
Do not expose an unauthenticated statistics page to the Internet. With SELinux enforcing, confirm the PEM file's security context and label unallocated custom ports with semanage. Check ausearch -m AVC -ts recent when the service cannot bind or read a file.
A practical validation and monitoring checklist
Always validate the configuration before replacing the live one, then test both the redirect and application path:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo systemctl reload haproxy
curl -I http://127.0.0.1
curl -kI https://127.0.0.1/healthz
A 503 response normally means that no backend is healthy, while 502 indicates a connection failure. HAProxy sends request logs to local0 through syslog; configure rsyslog to store that facility in /var/log/haproxy.log, and use journalctl for service startup errors. When latency rises, inspect backend CPU, memory and storage as well as the balancer; monitoring disk I/O can reveal saturated devices that make health checks time out.
These checks provide a compact handover routine:
Configuration checks
- Run the syntax check before every reload and keep the configuration in version control.
- Confirm every
serverentry reachesUPwith the expected check interval. - Test DNS, TLS and the health endpoint from each Australian operating location.
- Restrict statistics access and rotate any configured credentials.
Runtime checks
- Watch the service log and
/var/log/haproxy.logafter configuration or certificate changes. - Alert on 5xx responses, failed checks, saturation and certificate expiry.
- Correlate backend latency with network errors and storage waits.
- Re-test alerting around daylight-saving changes from AEDT to AEST.
Plan for resilient Australian deployments
Australian deployments commonly spread primary capacity in Sydney and secondary capacity in Melbourne, whether on-premises or in cloud regions. This can survive a local datacentre outage, but the additional network distance makes latency and failover tests essential. A Melbourne standby is not truly warm until DNS, health checks and application data dependencies have been measured.
Australian managed-service contracts often distinguish local business hours from genuine round-the-clock operations, so confirm escalation cover, data residency and public IP ownership before handover. Local teams sometimes call a setup “fair dinkum” after failover testing, which is a useful standard: the phrase should mean proven under failure, not merely installed successfully.
Reliable load balancing depends on verified health checks, protected TLS, sound firewall rules and tested redundancy, not simply on HAProxy running.