Installation, configuration, and testing of load-balancer (HAProxy) on POP!_OS/Ubuntu 22.04 LTS and Docker container

Yagnik Poshiya
5 min readMar 19, 2023

System Requirement

  1. POP!_OS 22.04 LTS
  2. Ubuntu 22.04 LTS
  3. Docker 23.0.1

HAProxy Load Balancer Installation

Step 1:
HAProxy is already installed on the system (Ubuntu/POP! OS). Nevertheless, it is not the newest version. Run the following command to see the system’s HAProxy package’s current version:

sudo apt update
sudo apt show haproxy

Step 2:
Yet HAProxy 2.6 is the most recent long-term support release. Thus, use the following command to enable the PPA repository before installing HAProxy 2.6:

sudo add-apt-repository ppa:vbernat/haproxy-2.6 -y

Step 3:
Use the command stated below to install HAProxy’s most recent version, 2.6.

sudo apt update
sudo apt install -y haproxy=2.6.\*

Step 4:
Use the following command to verify that the most recent version is currently installed:

haproxy -v

Step 5:
Once the installation is complete, the HAProxy service automatically launches and listens on port 80 for TCP traffic. Now, execute the following command to verify that the HAProxy service is up and running:

sudo systemctl status haproxy

Step 6:
It is recommended that you enable the service to automatically start when the system boots. Run the following command on the terminal to do it.

sudo systemctl enable haproxy

You can just use the following command to disable this feature.

sudo systemctl disable haproxy

Creating docker containers and Configuring HAProxy

Step 1:
The first prerequisite is that your machine must have either Docker engine OR Docker desktop. You can install docker engine from here. Run the following command to verify the version of the Docker engine after it has been installed.

docker --version

Step 2:
So, in order to operate the setup — that is, the HAProxy load balancer and the apache2 servers — on a single computer or operating system, we must first use the “docker network create” command to build a private network for docker containers.

docker network create apache-network

The network set up for the Docker containers is identified by the name “apache-network” in this case.

Step 3:
Use the following command to construct two Docker containers with the supplied Docker image (such as an image with Apache2):

docker run -d --name apache1 --net apache-network ubuntu/apache2:2.4-22.04_beta
docker run -d --name apache2 --net apache-network ubuntu/apache2:2.4-22.04_beta

Here,
-d: specifies the detached mode, meaning that the docker containers will operate in the background.
— name: specifies the names of the docker containers, apache1 and apache2
— net: specifies the name of the network on which docker containers will operate i.e. “apache-network”
ubuntu/apache2:2.4–22.04_beta: specifies the docker image name

Docker containers have now been created and are running smoothly in the background. Run the command to verify the Docker containers that are currently running.

docker ps

Step 4:
Now, there is a requirement of ipv4 address of running docker containers to configure the HAProxy load balancer file (i.e. haproxy.cfg). So for that use the following command:

docker network inspect apache-network
IPV4 addresses for the docker containers

Here, the docker containers’ IPv4 addresses are 172.18.0.2 and 172.18.0.3, respectively.

Step 5:
After the HAProxy service has been fully installed and completion of docker containers creation procedure, we must configure the load balancer. First, use the “cp” command to create a backup or copy of the haproxy configuration file (i.e. haproxy.cfg) in the “/etc/haproxy” directory.

sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bk

Step 6:
Use the following command to open original the haproxy.cfg file. And add the given lines of code to that file.

sudo gedit /etc/haproxy/haproxy.cfg

Here, nano can be used instead of gedit.

frontend <yagnikposhiya_proxy>
bind <ip-address for haproxy>:80
stats uri /haproxy?stats
default_backend <web-servers>

backend <web-servers>
balance roundrobin
server <apache1> 172.18.0.2:80 check
server <apache2> 172.18.0.3:80 check

listen stats
bind *:8080
stats enable
stats uri /
stats refresh 5s
stats realm Haproxy\ Statistics
stats auth <username>:<password>

Here, you can alter the names which are given in the <> braces. And The username and password are completely up to you.

After making changes to the haproxy.cfg file, use the command listed below to restart the haproxy.service and check the status of haproxy.service. (It must be in the active state.)

sudo systemctl restart haproxy.service # for restarting the service
sudo systemctl status haproxy.service # for checking the status of the service

Step 7:
We must now individually set the message for each container to the apache2 servers. To access the Docker container system, run the following commands in two distinct terminals.

docker exec -it apache1 /bin/bash # for container 1 (i.e. apache1)
docker exec -it apache2 /bin/bash # for container 2 (i.e. apache2)

Now run the commands listed below to set the messages for each servers in the two separate terminals.

echo "<H1> This is apache1 server </H1>" > /var/www/html/index.html # for container 1 (i.e. apache1)
echo "<H1> This is apache2 server </H1>" > /var/www/html/index.html # for container 2 (i.e. apache2)

Testing the HAProxy load balancer

Enter the <ip address for haproxy> you have provided above in your preferred web browser and then press the Enter. It is not necessary to pair an IP address and port number.

Server 1 index.html page

But, if you reload the browser tab, the following will appear:

Server 2 index.html page

As the HAProxy load balancer operates in a round-robin manner.
As a result, index.html is being shown from two different servers.
Moreover, more than two docker containers can be created here and used as apache2 servers. Now to display the statistics of front-end as well as back-end use <ip address for haproxy>:8080 port because in the listen section we have mentioned the 8080 port number for statistics. You will be prompted for the username and password you used in the haproxy.cfg configuration file. When the proper credentials are entered, the statistics are displayed.

front-end, back-end statistics

Thank You !!! Keep Learning

--

--

Yagnik Poshiya

Independent Researcher • Talks about Philosophy | Mathematics | Neuroscience | Quantumphysics | Machine Learning