Load Balancers
A load balancer distributes incoming client requests across a pool of backend servers, preventing any single server from becoming overwhelmed and providing automatic failover when a backend goes down. On Ubuntu, HAProxy is the standard software load balancer — it is fast, stable, well-documented, and handles tens of thousands of concurrent connections. HAProxy is used by companies like GitHub, Stack Overflow, and Reddit for production traffic.
Load balancing concepts
| Algorithm | Description | Best for |
|---|---|---|
| roundrobin | Distributes evenly in sequence | Identical servers, short requests |
| leastconn | Routes to server with fewest connections | Long-lived connections, databases |
| source | Routes same client IP to same server | Stateful apps without shared sessions |
| random | Random server selection | Large, homogeneous backend pools |
HAProxy installation and configuration
sudo apt update
sudo apt install -y haproxy
sudo nano /etc/haproxy/haproxy.cfg
/etc/haproxy/haproxy.cfg — HTTP load balancer example
global
log /dev/log local0
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option forwardfor # Add X-Forwarded-For header
option http-server-close
frontend web_frontend
bind *:80
bind *:443 ssl crt /etc/ssl/certs/example.com.pem
redirect scheme https if !{ ssl_fc } # Force HTTPS
default_backend web_servers
backend web_servers
balance roundrobin
option httpchk GET /health # Health check endpoint
http-check expect status 200
server web-01 192.168.1.10:8080 check inter 5s fall 2 rise 3
server web-02 192.168.1.11:8080 check inter 5s fall 2 rise 3
server web-03 192.168.1.12:8080 check inter 5s fall 2 rise 3
listen stats
bind *:8404
stats enable
stats uri /stats
stats auth admin:StrongPassword42!
# Validate config before applying:
haproxy -c -f /etc/haproxy/haproxy.cfg
# Apply changes:
sudo systemctl reload haproxy
# Zero-downtime config reload:
sudo haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -st $(cat /var/run/haproxy.pid)
HAProxy statistics and health checks
# Access stats page (if enabled in config):
# http://YOUR_LB_IP:8404/stats
# Shows: connections, bytes, response times, server status
# Check HAProxy status via CLI:
echo "show stat" | sudo socat stdio /var/lib/haproxy/stats | cut -d',' -f1,2,18,19 | column -t -s ','
# View backend server status:
echo "show servers state" | sudo socat stdio /var/lib/haproxy/stats
show servers state output
1 web_servers 1 web-01 2 0 0 0 0 ... # 2 = UP
1 web_servers 2 web-02 2 0 0 0 0 ... # 2 = UP
1 web_servers 3 web-03 0 0 0 0 0 ... # 0 = DOWN (failed health check)
Keepalived for HA load balancers
# Keepalived provides a floating VIP between two HAProxy nodes:
sudo apt install -y keepalived
sudo nano /etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf (on primary LB)
vrrp_script chk_haproxy {
script "systemctl is-active haproxy"
interval 2
fall 3
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 200 # Secondary uses 100
authentication {
auth_type PASS
auth_pass mypassword42
}
virtual_ipaddress {
10.0.0.100/24 # Floating VIP
}
track_script {
chk_haproxy # Drop to BACKUP if haproxy fails
}
}
Conclusion
Configure health checks with fall 2 rise 3: a server is marked DOWN after 2 failed checks (fast failure detection) but only marked UP again after 3 consecutive passing checks (prevents flapping). The stats page at :8404/stats is indispensable for monitoring — it shows real-time connection counts, error rates, and server status in a browser-accessible format. Pair HAProxy with Keepalived for a fully redundant load balancer layer that handles HAProxy itself failing.
Need help with Ubuntu administration?
Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.
Work with Muhammad Irfan Aslam