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.
apt-get update && apt upgrade -y
Step 2 – Installing HAProxy
Install HAProxy using apt-get
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
rm /etc/haproxy/haproxy.cfg
Then create a new configuration file:
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
Round Robin selects servers in turns. This is the default algorithm.
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
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.
Step 3 - Restart HA Proxy
You can now restart the HAProxy service & test your connection.
systemctl restart haproxy
Check the service is running again:
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.