High Availability architecture using HAProxy load balancer and Ansible
By: Ankush Chavan
Tags: Load Balancing, Ansible, High Availability, Haproxy, Apache

In the real world, when our website goes viral over the night, the huge traffic will start to come to our web servers. At this point, we have to launch extra web servers to meet the requirements. The best solution for this problem is to use some intelligent technologies like Kubernetes, EKS, or RedHat OpenShift. But all of these tools use load balancers behind the scene to balance the traffic among the multiple servers. To understand the working of load balancers let's build a high availability architecture using the HAProxy load balancer and automate this configuration using Ansible Playbook.

Ansible is a configuration management tool and HAProxy is a software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spread requests across multiple servers.

Objectives:

Automatically configure the HAProxy load balancer using Ansible Playbook. When a new web server is added to the ansible inventory, the HAProxy configuration file should be updated with the new webserver.

Prerequisites:

Ansible should be installed Webservers should be configured

Let’s begin with Ansible Playbook…

Step 1: Writing a Ansible Playbook for configuring HAProxy

I will configure the HAProxy load balancer on the controller node itself. So, I will run this playbook on the localhost.

This file has the code that will install, configure the HAProxy load balancer and start the HAProxy services on the controller node.

I had added handler to this ansible-playbook that will trigger the code to restart the haproxy services only if the haproxy configuration file is changed.

The loadbalancer_port variable will have the port number on which the loadbalancer will run. In this case, the load balancer port will be 8080.

Step 2: Writing the HAProxy configuration file

The haproxy configuration file will be named as haproxy.cfg.

On line no 44, we are providing the haproxy loadbalancer port number that will be taken from a variable defined in the haproxy.yml file.

Now, we have to provide the details of the backend that is the IP addresses of the servers where our application is running. But we have multiple servers and we have to load balance all of them. Here, we are grouping the server running with the application that we have to load balance. For this, we will group all of these servers with the same tag name that is ‘apache_webserver’.

For using all of these IP addresses, we will be using Jinja templating(line no 63 to 65) that will go through the list of all IP addresses and provide them as the backend. Our server with the application is running on the port 80.

Finally, we have the ansible playbook for configuring HAProxy load balancer.

Ansible Playbook:

Step 3: Add the webserver IP to the ansible inventory

Let’s first add only one webserver to the ansible inventory. We will add the IP of this webserver to the ansible_webserver group.

Step 4: Run the Ansible-Playbook

Now, we are all set to run the Ansible-Playbook.

$ ansible-playbook haproxy.yml

Step 5: Go to the loadbalancer and check if we can access the website through it

For going to the load balancer, we must know the IP of the system where load balancer is running.

Go to the system where we had installed HAProxy load balancer and get the IP address.

The IP address of the system with load balancer is 192.168.43.36 and as our load balancer is running on port 8080, we have to go to the port 8080 on this IP address.

So, open up the browser from another system that is connected to the same network and go to http://192.168.43.36:8080

The above image is showing that our website is accessible through the load balancer.

Step 6: Add another webserver

Suppose, the load to our website is increased and we have to add another webserver.

So, go to the ansible inventory and add the IP of another webserver.

And after updating the ansible inventory, let’s run ansible playbook again.

You can see in the above image that HAProxy configuration file is updated with the new webserver IP.

Step 7: Check if we can access both webservers through the load balancer

Go to the load balancer IP on the port 8080 and check if we can access website running on both webservers, simultaneously.