Load Balancing with HAProxy on Linux Explained
Load balancing is a foundational networking technique that distributes incoming application traffic across multiple backend servers to ensure high availability, fault tolerance, and optimal performance. This article explains the core concepts of load balancing, introduces HAProxy as an industry-standard solution on the Linux operating system, and provides a clear, step-by-step walk-through on how to configure and run HAProxy to balance traffic across your server infrastructure.
What Is Load Balancing?
Load balancing prevents any single server from becoming a single point of failure or performance bottleneck. When user requests arrive, a load balancer sits in front of the infrastructure, acting as a reverse proxy, and directs traffic to capable backend nodes based on predefined algorithms.
Load balancing generally operates at two main layers of the OSI model:
- Layer 4 (Transport Layer): Directs traffic based on network and transport protocols like IP addresses and TCP/UDP ports, without inspecting message contents.
- Layer 7 (Application Layer): Directs traffic based on application-level data, such as HTTP headers, URLs, or cookies, enabling intelligent routing decisions.
Common balancing algorithms include Round Robin (distributes requests sequentially), Least Connections (routes traffic to the server handling the fewest active sessions), and Source IP Hash (ties specific clients to specific servers based on their IP address).
What Is HAProxy?
HAProxy (High Availability Proxy) is a free, open-source, and extremely fast TCP/HTTP load balancer and reverse proxy designed for Linux environments. Known for its efficiency, low memory footprint, and high reliability, it is widely used by high-traffic websites to handle concurrent connections, perform health checks, and manage SSL termination.
Achieving Load Balancing with HAProxy on Linux
Setting up HAProxy on a Linux distribution involves installation, configuration of frontends and backends, and enabling health checks.
1. Install HAProxy
Update package repositories and install the package using your distribution's package manager.
On Ubuntu/Debian:
sudo apt update sudo apt install haproxy -yOn RHEL/CentOS/Rocky Linux:
sudo dnf install haproxy -y
2. Configure HAProxy
The primary configuration file is located at
/etc/haproxy/haproxy.cfg. This file is divided into four
main sections:
global: Defines system-level parameters, process owners, and logging.defaults: Specifies default timeout settings and operation modes applied to proxies.frontend: Defines the IP address and port that clients connect to.backend: Defines the pool of servers that process the requests forwarded by the frontend.
Open the configuration file in a text editor:
sudo nano /etc/haproxy/haproxy.cfgAdd or modify the configuration to create an HTTP Layer 7 load balancer:
global
log /dev/log local0
log /dev/log local1 notice
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
frontend http_front
bind *:80
default_backend web_servers
backend web_servers
balance roundrobin
server web1 192.168.1.101:80 check
server web2 192.168.1.102:80 check
In this configuration:
bind *:80listens for inbound traffic on port 80 across all network interfaces.balance roundrobinalternates incoming requests equally between backend servers.checkenables automatic health checks. If a server stops responding, HAProxy removes it from the pool until it recovers.
3. Validate and Start the Service
Before restarting the service, verify that the configuration syntax is correct:
haproxy -c -f /etc/haproxy/haproxy.cfgIf the output confirms the configuration is valid, enable and restart the HAProxy service:
sudo systemctl enable haproxy
sudo systemctl restart haproxyCheck the runtime status to confirm it is operational:
sudo systemctl status haproxy4. Configure Firewall Rules
Ensure that the Linux firewall permits inbound traffic on the load balancer's configured port:
Using UFW (Ubuntu/Debian):
sudo ufw allow 80/tcp sudo ufw reloadUsing Firewalld (RHEL/CentOS):
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reload
Once configured, client requests sent to the HAProxy server's IP address will be dynamically routed to the available backend web nodes, delivering reliable, balanced application delivery across your Linux environment.