Configuring HAProxy using Ansible playbook

Image for post
Image for post

Configuration Management has always been an important task, mostly for installing, uninstalling, tracking and controlling software changes or rather we can say in an Operating System. But manually configuring multiple no. of OS or servers is a tedious & error-prone job. Generally in companies, we have 100s or1000s or millions of servers depending upon the scale of the companies. Hence, there is no other way other than Automation.

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

HAProxy, which stands for High Availability Proxy, is a popular open-source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris, and FreeBSD. Its most common use is to improve the performance and reliability of a server environment by distributing the workload across multiple servers (e.g. web, application, database). It is used in many high-profile environments, including GitHub, Imgur, Instagram, and Twitter. HAProxy acts as a Proxy server, as well as a LoadBalancer.

A load balancer acts as the “traffic cop” sitting in front of your servers and routing client requests across all servers capable of fulfilling those requests in a manner that maximizes speed and capacity utilization and ensures that no one server is overworked, which could degrade performance. If a single server goes down, the load balancer redirects traffic to the remaining online servers. When a new server is added to the server group, the load balancer automatically starts to send requests to it.

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

vim /root/ip.txt

Image for post
Image for post

Here I have created a group of web server now we will use this name instead of 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 ansible ping module.

Image for post
Image for post

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 these 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

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

Now using 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

Image for post
Image for post
Image for post
Image for post

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

Image for post
Image for post
Image for post
Image for post

Completing the remaining task as well

Image for post
Image for post

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 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

Image for post
Image for post

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

So in Webserver 1, I have written the content as

Image for post
Image for post

In the Webserver 2

Image for post
Image for post

So now let's check the 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

Image for post
Image for post

Now on refreshing let's check what happens

Image for post
Image for post

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

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

Today is the world of the Internet and all the content is hosted on the webserver and millions of client use the services and to serve this huge number of client company require many webservers running 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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store