Load-balancing is the most common practice of distributing incoming web traffic among multiple back-end servers. This makes the application highly available even if some of the servers go down for some reason. Load Balancing increases the efficiency and reliability of a web application. HAProxy load-balancer is used for the same purpose. It is the most widely used load-balancer in industries. As per the official website, HAProxy is used by leading companies like AWS, Fedora, Github, and many more.
HAProxy or High Availability Proxy provides high availability and proxying solution. It is written in C and works at network and application layers of the TCP/IP model. The best thing is that it has a free community edition, and it is an open-source application. It works on Linux, FreeBSD, and Solaris operating systems. The enterprise edition is also there, but it has a price tag.
In this guide, we will see How to Install HAProxy and Configure the Load Balancing Server on Debian 10.
Prerequisites:
- “sudo” access to all the machines and basic knowledge of running commands in Linux terminal.
- Private IP addresses added to load-balancer and backend servers.
- Debian 10 Operating System installed on all machines.
Installing HAProxy on Debian 10
For our guide, we will assume the following IP address configuration :
- HAProxy load-balancer 10.0.12.10
- Web server1: IP Address: 10.0.12.15
- Web server2: IP Address: 10.0.12.16
Step 1. Update Debian System repository and packages
First, run the below commands on all systems to update software packages to the latest one.
$ sudo apt upgrade -y
Step: 2 Install Nginx on back-end servers
Prepare your back-end servers by installing Nginx web server on each. You can also choose to install other web servers like apache.
To install Nginx, run the following commands on each back-end server in your environment:
Step: 3 After Nginx is installed on your back-end servers, start the service, as shown below:
TIP: We can also manage the nginx web server using the below command:
option: start reload restart status stop
Step: 4 Create custom index pages in the web folder of each Nginx web server. This will help us to distinguish which back-end server is serving the incoming requests.
On each web server, perform the following tasks:
Backup the original index file using the following command:
Add custom text to the index.html file. We are adding the IP address of each web server.
For web server 1:
For web server 2:
You can also use vi editor if you feel more comfortable with that. This is shown below:
When the file is opened, enter the text and save the file.
Open the default virtual host file in the “/etc/nginx/sites-available/” directory.
Now inside the server block, change the root directive from “/var/www/html” to “/usr/share/nginx/html”.
To check the Nginx configuration, run the following command:
Step 5: Now restart the service using the command:
You can check the status of nginx using the following command:
Step: 6 To install HAProxy on Debian 10 (Buster), run the following command on the load-balancer.
Tip: Once HAProxy is installed, you can manage HAProxy via an init script. For this, set the “enabled” parameter to 1 in “/etc/default/haproxy” as shown below:
ENABLED=1
Now the following option can be used with an init script:
option: start reload restart status stop
Step: 7 Now configure HAProxy load-balancer by editing the haproxy default configuration file, i.e. “/etc/haproxy/haproxy.cfg”. To edit this file, run the following command
Tip: Please backup the original file so that in case something goes wrong, we will be all safe. To perform the backup, use the following command:
Now go to the end of the file and edit the following information:
bind 10.0.12.10:80
mode http
default_backend webserver
backend webserver
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1rnHost:localhost
server web1 10.0.12.15:80
server web2 10.0.12.16:80
Note: Do not forget to change the IP addresses in the above file to the one you have added to your web servers.
Step: 8 Verify the configuration syntax of the above file with the following command:
If everything goes right, it will show an output like: “Configuration file is valid.” If you get any error in the output, recheck your configuration file and verify it again.
Step: 9 Now restart the HAProxy service to apply the changes
Testing The Configuration
Now it is time to see if our setup is working properly. Enter load-balancer system IP on a web browser (In our case, it is 10.0.12.10) and refresh the page continuously for 2-4 times to see if HAProxy load-balancer is working properly. You should see different IP addresses or whatever text you have entered in the index.html file when you continue to refresh the page multiple times.
Another way to check is to take one web server offline and check if another web server is serving the requests.
That’s all for now! Try experimenting with HAProxy to learn more about how it works. For e.g., you can try:
- Integrating different web server beside nginx.
- Changing the load-balancing algorithm to something other than round-robin.
- Configuring HAProxy health check to determine if a back-end server is working or not.
- Applying sticky sessions to connect a user to the same back-end server.
- Using HAProxy stats to get insights about the traffic on servers.
HAProxy has extensive documentation available for both the HAProxy community edition and HAProxy enterprise version. Explore this documentation to get more insights into improving the performance and reliability of your server environment.
This guide has been successfully performed on Debian 10(Buster). Try to install HAProxy on other Debian based distros like Ubuntu, Linux Mint etc. Please do not forget to share this guide with others.