Setup High-Availability Load Balancer with ‘HAProxy’ to Control Web Server Traffic..

Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.

Jatin Lodhi
7 min readAug 29, 2021
Setup High-Availability Load Balancer with ‘HAProxy’ to Control Web Server Traffic

🤩So guys this is my new article which give you some high level idea about ANSIBLE , HAProxy , Apache Webserver & php.

I am confident to tell you , you will definitely learn something new from this article . 🔥🔥

I am going to discuss about Load Balancer concept in depth.

Starting this article from the scratch.



Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.


If you want to learn about Ansible 👇go through this article which will give you crystal and clear idea about ANSIBLE .


HAProxy is free, open source, highly available, load balancer software written by Willy Tarreau in 2000. It is implemented in the C programming language. It is known for its high performance and is extremely reliable and secure. It supports both Layer 4 (TCP) and Layer 7 (HTTP) based application load balancing, and is released under the GPLv2 licence. Nginx is a Web server created by Igor Sysoev, and is also written in the C programming language. It can be used as a reverse proxy, mail proxy and as an HTTP cache. It was first released in 2004 and uses the 2-clause BSD licence.

Learn more about HAProxy then walk with this Article👉

Apache Web Server

Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web. It’s widely thought that Apache gets its name from its development history and process of improvement through applied patches and modules but that was corrected back in 2000. It was revealed that the name originated from the respect of the Native American tribe for its resiliency and durability.


PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP.Start learning PHP now »

→ Step 1 : Install & Configure the ansible.

So now the question is how do we install the ansible in our RHEL 8. So for the installation process we will use python module to install the ansible tool.

To install the ansible we use this command

pip3 install ansible

So after the installation we have to configure the ansible to use it & before that we have to define the managed node that means which system gonna we will use as a remote system. So for that we have to create a file inside the /root/ folder ( to define the hosts )with the ip.txt . So we use the command to create a host like.

vim /root/ip.txt

In My case Load Balancer ip is

& Webserver1 ip’s is

Webserver2 ip’s is

After creating the hosts file we will create the configuration file for ansible because without configuration any software we can not use it properly & we can not use our hosts to work on it.

To configure the ansible first of we create a directory inside the /etc/ansible folder , after that we have to create the file with the name of ansible.cfg to use this file as a configuration file & we have to write the inventory name ( that will be the host file where we write our remote system IP ) inside that file. Then we have to pass the SSH Key authentication , So we have to make false this option so that there will no problem while doing the authentication.

# create configuration file#command to create the directory
mkdir /etc/ansible
#to create the file inside the ansible directory
vim /etc/ansible/ansible.cfg

write this code inside the file .

After we have to install the SSH pass with help of yum command , we use the command like :

yum install sshpass

After that we have to check that our ansible command is working or not properly simply means which version is install . Use below mentioned command:

ansible --version

So with the help of this command we can find that our ansible file is successfully configured & command is working properly.

Now we can check how many hosts we have with the help of below mentioned command:

ansible all --list-hosts

It shows outputs a list of matching hosts; does not execute anything else .

After checking the hosts name we have to check that there is proper connection established between the controller node & managed node or not . So we can ping the managed node from our controller node to check the connectivity . Command will be like

ansible all -m ping

It shows ping : pong means CN is pinging to all MN.

So after checking this connection we have to write the code for the entire deployment of the docker with the help of ansible.

→ Step 2 : Write the ansible playbook for Reverse Proxy i.e, HAProxy.

So to deploy the entire setup we can use the command line or ansible-playbook option , but through the command line it is difficult for us to deploy the entire setup , so we use the ansible-playbook option for this task.

But here you first know what is Ansible-playbook…

What is Ansible-playbook ?

An Ansible playbook is an organized unit of scripts that defines work for a server configuration managed by the automation tool Ansible.

A playbook is a configuration file written in YAML that provides instructions for what needs to be done in order to bring a managed node into the desired state. Playbooks are meant to be simple, human-readable, and self-documenting. They are also idempotent, meaning that a playbook can be run on a system at any time without having a negative effect upon it. If a playbook is run on a system that’s already properly configured and in its desired state, then that system should still be properly configured after a playbook run.

So Now we write the code for the entire deployment. first we have to define the host name & then multiple task.

In my case I will create file /root/ansible-ws/haproxy location

# command to create a file 
vim /root/ansible-ws/haproxy/haproxy.yml

We have to update it’s configuration file automatically on each time new Managed node join the inventory.

So HAProxy configuration file look like this 👇

Now it`s time to run our Ansible-playbook

ansible-playbook -v filename.yml

After successfully run Ansible-playbook .

Now it`s time to check via Browser our Load Balancer is work or not .

So you can see that our Task will run successfully




Thanks, Everyone for reading. That’s all… Signing Off… 😊



Jatin Lodhi

I am an IT Enthusiast, who is passionate about exploring/learn all the latest technologies from research perspective.