Installing HAProxy to Configure a Load Balancing Server on Debian 10

Load balancing is the most common practice for distributing incoming web traffic across 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 your web application. The HAProxy load balancer is used for the same purpose. It is the most widely used load balancer in the industry. According to the official site, HAProxy is used by leading companies like AWS, Fedora, Github and many more.

HAProxy or High Availability Proxy provides a high availability and proxy solution. It is written in C and operates at the network and application layers of the TCP / IP model. Best of all, it has a free community version and it is an open source application. It runs on Linux, FreeBSD and Solaris operating systems. There is also a corporate version, but it has a price.

In this article, we will see how to install HAProxy and configure a load balancer server on Debian 10.

Prerequisites:

  1. “Sudo” access to all machines and basic knowledge of running commands in the Linux terminal.
  2. Private IPs added to load balancer and backend servers.
  3. Debian 10 operating system is installed on all machines.

Installing HAProxy on Debian 10

In this article, we will assume the following IP address configuration:

  1. HAProxy Load Balancer 10.0.12.10
  2. Web Server1: IP Address: 10.0.12.15
  3. Web Server2: IP Address: 10.0.12.16

Step 1. Update the Debian system repository and packages.

First, run the commands below on all systems to update the software packages to the latest version.

$ sudo apt update

$ sudo apt upgrade -y

Step: 2 Install Nginx on internal servers

Prepare your backend servers by installing an Nginx web server on each. You can also install other web servers like apache.

To install Nginx, run the following commands on each backend server in your environment:

$ sudo apt install nginx

Step: 3 After Nginx is installed on your backend servers, start the service as shown below:

$ sudo systemctl start nginx

TIP: We can also manage the nginx web server using the following command:

$ sudo /etc/init.d/nginx “option”

option: start reload restart status stop

Step: 4 Create your own index pages in the web folder of each Nginx web server. This will help us determine which internal server is serving incoming requests.

Perform the following tasks on each web server:

Make a backup copy of the original index file with the following command:

$ sudo cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.orig

Add your own text to the index.html file. We add the IP address of each web server.

For web server 1:

$ sudo echo "Web server 1: 10.0.12.15"  | sudo tee /usr/share/nginx/html/index.html

For web server 2:

$ sudo echo "Web server 2: 10.0.12.16"  | sudo tee /usr/share/nginx/html/index.html

You can also use the Vim editor if you prefer. This is shown below:

$ sudo vi /usr/share/nginx/html/index.html

When the file opens, enter your text and save the file.

Open the default virtual host file in the “/ etc / nginx / sites-available /” directory.

$ sudo nano /etc/nginx/sites-available/default

Now, inside the server block, change the root directive from “/ var / www / html” to “/ usr / share / nginx / html”.

To check your Nginx configuration, run the following command:

$ sudo nginx -t

Step 5: Now restart the service using the command:

$ sudo systemctl restart nginx

You can check the nginx status using the following command:

$ sudo systemctl status nginx

Step: 6 To install HAProxy on Debian 10 (Buster), run the following command on your load balancer.

$ sudo apt install haproxy -y

Tip: After installing HAProxy, you can manage HAProxy using an init script. To do this, set the “enabled” parameter to 1 in “/ etc / default / haproxy” as shown below:

$ sudo vi /etc/default/haproxy

ENABLED=1

Now you can use the following option with the init script:

$ sudo service haproxy “option.”

option: start reload restart status stop

Step: 7 Now configure the HAProxy load balancer by editing the default haproxy configuration file, that is, “/etc/haproxy/haproxy.cfg”. To edit this file run the following command

$ sudo vi /etc/haproxy/haproxy.cfg

Tip: make a backup of your original file so we are safe in case something goes wrong. To back up, use the following command:

$ sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.orig

Now go to the bottom of the file and edit the following information:

frontend Local_Server

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 Don’t forget to change the IP addresses in the above file to the one you added to your web servers.

Step: 8 Check the configuration syntax of the above file using the following command:

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

If all goes well, you will see the following output: “Configuration file is valid.”. If you get any error in the output, recheck the config file and check it again.

Step: 9 Now restart the HAProxy service to apply the changes.

$ sudo service haproxy restart

Testing the configuration

Now is the time to check if our setup is working properly. Enter the IP address of the load balancer in your web browser (in our case, it’s 10.0.12.10) and continually refresh the page 2-4 times to make sure the HAProxy load balancer is working correctly. You should see different IP addresses or whatever text you entered in the index.html file as you continue to refresh the page several times.

Another way to check is to turn off one web server and check if the other web server is serving requests.

That’s all for now! Try experimenting with HAProxy to learn more about how it works. For example, you can try:

  • Integration of another web server next to nginx.
  • Changing the load balancing algorithm to something other than circular.
  • Configure a HAProxy health check to determine if the back-end server is running.
  • Apply sticky sessions to connect a user to the same backend server.
  • Using HAProxy statistics to get information about traffic on servers.

HAProxy has extensive documentation available for both the community version of HAProxy and the enterprise version of HAProxy. Review this documentation for more information on improving the performance and reliability of your server environment.

This tutorial was successfully completed on Debian 10 (Buster). Try installing HAProxy on other Debian based distributions like Ubuntu, Linux Mint, etc. Please don’t forget to share this tutorial with others.

Sidebar