Skip to content

How To Setup HAProxy as a Load Balancer on Ubuntu 20.04

21/01/2023

:   Alexander

Information

HAProxy is a free, very fast and reliable reverse-proxy offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for very high traffic web sites and powers a significant portion of the world's most visited ones.

Over the years it has become the de-facto standard opensource load balancer, is now shipped with most mainstream Linux distributions, and is often deployed by default in cloud platforms

Step 1 - Update the system

Firstly update your system, on Debian you can use the below command.

sudo apt-get update && sudo apt upgrade -y

Step 2 - Installing HAProxy

Install HAProxy using apt-get

sudo apt install haproxy
Check HAProxy is running.
systemctl status haproxy
Output:
● haproxy.service - HAProxy Load Balancer
     Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-01-21 06:03:21 GMT; 6s ago
       Docs: man:haproxy(1)

Step 2 - Configuring HAProxy for Load Balancing

We'll start by removing the default configuration file, this can normally be found at /etc/haproxy/haproxy.cfg

sudo rm /etc/haproxy/haproxy.cfg
Then create a new configuration file:
sudo nano /etc/haproxy/haproxy.cfg
Paste the following into the file & change it suite your infrastructure:
defaults
    retries 3
    option  redispatch
    timeout client 30s
    timeout connect 4s
    timeout server 30s

frontend tcp_listner
 bind *:3128
 mode tcp
 default_backend tcp_backend_3128

backend tcp_backend_3128
 balance roundrobin
 mode tcp
 server infra-node1 10.0.0.1:1080 check
 server infra-node2 10.0.0.2:1080 check
For this example, I'm using port 3128 as the frontend listener which is then redirecting connections to two backends infra-node1 & infra-node1 on IP's 10.0.0.1 & 10.0.0.2 using a roundrobin algorithm

Load Balancing Algorithms

The load balancing algorithm that is used determines which server, in a backend, will be selected when load balancing. HAProxy offers several options for algorithms. In addition to the load balancing algorithm, servers can be assigned a weight parameter to manipulate how frequently the server is selected, compared to other servers.

You can explore the different types of load balancing algorithms here

roundrobin

roundrobin this selects servers in turns. This is the default algorithm.

source

source this selects which server to use based on a hash of the source IP address that users are making requests from. This method ensures that the same users will connect to the same servers.

leastconn

leastconn selects the server with the least number of connections. This is recommended for longer sessions. Servers in the same backend are also rotated in a round-robin fashion.

You can now restart the HAProxy service & test your connection.

sudo systemctl restart haproxy
Check the service is running again:
sudo systemctl status haproxy
Output:
● haproxy.service - HAProxy Load Balancer
     Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-01-21 05:52:32 UTC; 23min ago
       Docs: man:haproxy(1)
             file:/usr/share/doc/haproxy/configuration.txt.gz
    Process: 863 ExecStartPre=/usr/sbin/haproxy -Ws -f $CONFIG -c -q $EXTRAOPTS (code=exited, status=0/SUCCESS)
   Main PID: 888 (haproxy)
      Tasks: 3 (limit: 1076)
     Memory: 72.4M
        CPU: 670ms
     CGroup: /system.slice/haproxy.service
             ├─888 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
             └─935 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock

Jan 21 05:52:30 vps systemd[1]: Starting HAProxy Load Balancer...
Jan 21 05:52:32 vps haproxy[888]: [NOTICE]   (888) : New worker #1 (935) forked
Jan 21 05:52:32 vps systemd[1]: Started HAProxy Load Balancer.

(Optional) Step 3 - Adding a Stats Page

If you want, you can add a basic stats page to monitor your incoming & outgoing connections through the HAProxy server.

HAProxy Stats Page Screenshot

Edit your HAProxy configuration file again:

sudo nano /etc/haproxy/haproxy.cfg
Add the following below the defaults section & above the frontend tcp_listener
frontend stats
 bind *:8404 #(1)!
 mode http
 stats enable
 stats uri /
 stats refresh 10s

  1. Set this to whatever port you'd like to use for your frontend stats page.

Your configuration should look something like this:

defaults
    retries 3
    option  redispatch
    timeout client 30s
    timeout connect 4s
    timeout server 30s

frontend stats
 bind *:8404
 mode http
 stats enable
 stats uri /
 stats refresh 10s

frontend tcp_listner
 bind *:3128
 mode tcp
 default_backend tcp_backend_3128

backend tcp_backend_3128
 balance roundrobin
 mode tcp
 server infra-node1 10.0.0.1:1080 check
 server infra-node2 10.0.0.2:1080 check
Restart the HAProxy service again:
sudo systemctl restart haproxy
You should now be able to access the stats page through the Server IP & port 8404 as specified in the configuration.
http://haproxyip:8404
HAProxy Stats Page Screenshot