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:

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.

2. Configure HAProxy

The primary configuration file is located at /etc/haproxy/haproxy.cfg. This file is divided into four main sections:

Open the configuration file in a text editor:

sudo nano /etc/haproxy/haproxy.cfg

Add 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:

3. Validate and Start the Service

Before restarting the service, verify that the configuration syntax is correct:

haproxy -c -f /etc/haproxy/haproxy.cfg

If the output confirms the configuration is valid, enable and restart the HAProxy service:

sudo systemctl enable haproxy
sudo systemctl restart haproxy

Check the runtime status to confirm it is operational:

sudo systemctl status haproxy

4. Configure Firewall Rules

Ensure that the Linux firewall permits inbound traffic on the load balancer's configured port:

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.