Haproxy (Load Balancer) Configuration using Ansible.
By: Yash Hirulkar
Tags: Load Balancing, Ansible, Ansible Playbook, Haproxy, Web Server

In this article we are going to:

Configure Reverse Proxy i.e Haproxy and update its configuration file automatically each time a new Managed node (configured with Apache Web-Server) joins the inventory. To configure the same setup as 1 over AWS using ec2 instance over there.

We are going to use Ansible to Automate the process. Ansible helps us to automate a wide range of technologies:-

To carry out the above task, I have installed two different RHEL8 Virtual Machines on the top of Oracle Virtual Box. Out of which one VM will work as the backend servers of the Load Balancer.

So, I will be configuring one backend server as a web-server. And I am going to configure my localhost (Controller Node of Ansible) as an Haproxy server.

So, here’s the Ansible Configuration and Inventory file:

Now, let’s start with configuring the Ansible control node as Haproxy server (Load Balancer):

I have used haproxy software for load balancing purposes. For the configuration file of Haproxy, I have a pre-created configuration template in which I have used some of the Jinja2 programs in order to dynamically identify the IP of the backend servers.

Here is a part of that program in the configuration file:

Here’s the Ansible Playbook I have created to configure the control node as Haproxy Server (Load Balancer).

And finally, I have started the Haproxy service:-

But, now we have to configure the Managed node which we were going to use as Web-server.

Checking Connectivity with all the Managed nodes:-

Now, let’s start with configuring the Managed node as a web server:

Here’s the playbook which I have created to configure the Managed node as a Web server.

At first, I have configured yum in the Managed nodes (as they are RHEL8) and then, I have installed httpd package for the webserver. In the next step, I have used the copy module to create content for the webpages.

And in the next step, I have started the service.

So, this was a play using which I configured the Managed nodes as Web Servers.

And hence, the Haproxy (load balancer) has been configured successfully.

The entire setup has been done successfully using Ansible.

To Configure the same setup on AWS cloud:-

Now, if we want to do the same setup on the top of AWS Cloud, we have to make some changes in the configuration file of Ansible, in order to provide the sudo access and key to log in.

Here, I am going to launch four ec2 instances out of which, one instance will work as the Load balancer and the remaining will work as Backend Servers.

Here is the configuration file of ansible for ec2 instance:-

Here, I have provided some extra information such as: the location of the key to log in, the remote username, and the privilege escalation details i.e., when ansible will log in using the remote user then it will require root permissions in order to perform tasks.

Then, I have used the above playbook to provision 4 instances:

Before running this playbook, we need boto and boto3 library of python to connect to the API of AWS:

The output of the playbook:

And the instances have been provisioned successfully:

Now, in the next step, I have updated the inventory with the IP address of all the instances in separate groups:

Here, we do not need to give the username and password for the remote system as we already provided the required information in the configuration file.

Now, to check the connectivity with the ec2 instances:-

And now, almost everything would be the same in the playbook for the configuration of the Web-Server and Haproxy (Load Balancer). We just need to change the group names in the playbook and the haproxy.cfg file.

Here is the playbook:

The updated part of haproxy.cfg file:-

The output of the plays:

And the same setup has been configured over AWS cloud as well:

And hence, the Haproxy (load balancer) has been configured successfully.

The entire setup has been done successfully using Ansible.

Thank You :)

Hope you learned something new…!

👋 Join FAUN today and receive similar stories each week in your inbox! ️ Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬