HAProxy is a high-performance, open source load balancer and reverse proxy for TCP and HTTP-based applications. Using HAProxy, one can distribute workloads and improve the performance of websites and web-based applications with faster response times, higher availability and increased throughput.
HAProxy, when combined with DNS Geolocation Diversion, can load balance the traffic to your website/application so users from different areas of the world will be served from a regional HAProxy server and the request will then be forwarded to a backend server (e.g. another web server or a database).
The advantage in this kind of setup is that you can avoid the bottleneck of a single web server handling all the requests & traffic by itself. Secondly, by combining a web farm of HAProxy instances with DNS Geolocation is that you can serve different content or rules to specific regions of the world, as per your business requirements.
In this article we will demonstrate how to easily setup HAProxy on a Debian 11 Server with the basic configuration, which you can then optimize or extend as per your bespoke requirements.
Steps to Install HAProxy on Debian 11
HAProxy is pretty useless when used in one server. So in this example we will install it on two Debian servers which we will then configure with roundrobin balancing.
Any command you see further below must be executed on both/all of your servers.
Step 1 – Update System
Execute the following command to install and use the latest available software packages on your Debian system.
root@localhost:~$ apt update -y root@localhost:~$ apt upgrade -y
Once completed, reboot the server as follows:
root@localhost:~$ reboot or root@localhost:~$ shutdown -r now
Step 2 – Install Apache (optional)
This step is optional as you may want to use HAProxy without Apache. However, for the purpose of demonstrating the successful installation and usage of HAproxy we will install and configure Apache as follows.
root@localhost:~$ apt -y install apache2
Once apache2 is installed, run the following command to insert a pre-defined message in index.html. This will help us understand the HAProxy server that is used every time we will be refreshing our domain.
root@localhost:~$ echo "Server1 says Hello" | sudo tee /var/www/html/index.html
root@localhost:~$ echo "Server2 says Hello" | sudo tee /var/www/html/index.html
Step 3 – Install and Configure HAProxy on Debian 11
Run the following command on both servers to install haproxy:
root@localhost:~$ apt install haproxy -y
Now, lets configure HAproxy to use the roundrobin balance mode between the two servers.
Open the file /etc/haproxy/haproxy.cfg and insert the following configuration. Then save and close your file.
frontend haproxy_apache_front bind *:80 default_backend haproxy_apache_backend option forwardfor backend haproxy_apache_backend balance roundrobin server backend01 10.10.10.111:80 check server backend02 10.10.10.112:80 check
Now, restart the haproxy on both servers as follows:
root@localhost:~$ systemctl restart haproxy
Let’s test our creation! For the purpose of this tutorial, our Apache configuration was setup with vhost “haproxy.netshop.global”.
Then reload the page a few times and you should be seeing the message from the 2nd server. This means that HAProxy with roundrobin balance mode work!
Success! Stay tuned for more HAProxy tutorials with advanced HTTP and TCP configurations!