Configuring HAProxy using Ansible playbook

Configuration Management is the most important task, mostly task belonging to configuring different types of web servers with a different use-case and mostly in the industry, we have normally millions of webservers running behind the scene and manually configuring every webserver is practically not possible so the only key is automation.

In this article, we would be configuring 1 system as Haproxy and 2 systems as a webserver using Ansible automation.

HAProxy is software that provides 2 services in a single packet, which stands for High Availability Proxy, It acts as a load balancer that helps in balancing the load between web servers and it also acts as a Reverse Proxy which improves the security of the webservers.

have created my inventory on the “/root/ip.txt” location of the Controller Node, and it will mainly consist of few details about Target Node.

vim /root/ip.txt

Here I have created a group of web server now we will use this name instead of the IP address in the ansible-playbook. We will use our local system as a load balancer and we don't have to include its IP in the inventory as it is pre-created in the inventory in the name of localhost.

Now we will check if we have the connection between the nodes or not using the Ansible ping module.

For this first, we have to decide on which port we want to run our services and let us say we will run our load balancer services on port 8080 and backend services at port 80 so this we have created a separate variable file where all this info is saved as when other use my playbook then don't have to look into code for changes they just go into the variable file and can make changes according to the requirement

vim var.yml

And we will include this variable file in the playbook so that all the variable of this file can be accessible.

I have used the name of the group as “localhost” this localhost is my system where Ansible is installed and this system only I will configure as the load balancer, for including variable file we used vars_files module of ansible and used package module to install HAProxy software which will provide the service of reverse proxy as well as of load balancing.

Now using the template module it will copy and parse the configuration file of the haproxy where we have given the port number where my backend server is running the port on which we want to run our load balancer.

Let’s check the configuration file of the haproxy

Instead of writing the IP of a backend server, we used the jinja which will automatically extract the Ip of the backend server from the inventory file and using the for loop It will add all the IP of the backend server with the port of the backend server which is running on the port 80, we also used the variable port_lb which will tell the load balancer to run the services on the port 8080 as port_lb contains 8080 value in the variable file.

After running the playbook this file will look like this

Completing the remaining task as well

Here we have restarted the service of haproxy you can make this as idempotent using certain methods which also have explained in my previous articles and allowing firewall so that anyone can connect to port 8080 as on this port my haproxy service is running.

It is same we have configured webserver using ansible and if you don't know how to configure web server you can refer to this link in which I have talked about how to configure webserver using Ansible automation and I have also given the link of the playbook at the last you can refer that as well.

Now let's run the playbook “lb.yml” (here lb.yml is the name of my playbook) using the command

ansible-playbook lb.yml

So as we can see there no error in our playbook it ran successfully and I am going to change the content of the webserver manually so that we can differentiate between webserver to confirm our load balancer is working.

So in Webserver 1, I have written the content as

In the Webserver 2

So now let's check if my load balancer is balancing load between webserver or not so we will use“<ip_of_load_balancer>:8080/index.html” Ip of my load balancer is 192.168.0.104 so let's check

Now on refreshing let's check what happens

So we can see that our load balancer is working fine it's just balancing the load equally between the 2 webservers and the main part client don't have to remember the both the IP of the webserver they can access services of both the webservers from the single IP of the load balancer.

Here is the Github link to the playbook and other files used in this practice.

Today is the world of advancement and all the content is hosted on web servers and normally we have millions of webservers but the challenge is all the webserver have a different IP address so the client has to remember all the IP which is practically not possible so to overcome this challenge we use Load Balancer and to automate this process we used Ansible.

Thanks for reading this article! Leave a comment below if you have any questions.

I would be writing more about Ansible so stay tuned

Comments

Popular posts from this blog

How to configure Hadoop cluster using Ansible playbook

Automation Using Python Menu-Driven Program