Using Ansible to set up a Load Balancer & Webservers with dynamic binding!

This blog aims to explain the process of creating an architecture involving HAProxy & Apache Webserver for Load Balancer & Webserver usage respectively using Ansible!

Harshit Dawar
LinuxWorld Informatics Pvt. Ltd.
4 min readDec 20, 2020

--

In this rapid world, DevOps is a very important technology. It is one of the most demanding technologies in the present, therefore understanding it conceptually is a very important factor today for anyone.

I used “understanding” in the above paragraph because learning is not required, & also learning is not for long term goal, understanding is for the long term goal. So, everyone should focus on understanding the concepts instead of learning them.

In this blog, a complete practical will be covered which includes setting up the load balancer or reverse proxy using HAProxy software, Webserver using the Apache Webserver software, & using Ansible to automate everything. In addition to that, Web servers will be attached/bound to the load balancer dynamically, instead of making their entry in the configuration file of the Load Balancer.

Note: If you are not comfortable or if you do not know anything about Ansible, I would suggest to please go through my below-mentioned blog. It covers Ansible in brief, & that much knowledge is sufficient to proceed. But, if you have good knowlegde about Ansible that it will be very easy for you to proceed. Otherwise, even if you didn’t understand the internal working, you can use the code to fulfill the need.

Brief about HAProxy!

It is software that is used for Load Balancing i.e. it has the capability to redirect the requests of the client to one out of the multiple machines bound to it, & this redirection is possible by using the concept of the reverse proxy.

Load Balancing is very much important because when the online business grows, then only a single machine cannot handle a huge amount of requests, therefore multiple machines are used.

The reason for using an extra machine as a load balancer is to provide only 1 single website to the customers, instead of providing multiple IP Addresses of the multiple web servers to the customers which will highly degrade the business reputation.

Now, that being said, let’s proceed towards the main project!

Implementation of the main Project!

For ease of understanding & implementation, the project is been broken down into various steps & those steps are as follows:

  1. Creating a basic HTML file for webserver usage.
  2. Making the HAProxy Load Balancer configuration file dynamic so that it can automatically update(add or delete) the entries of the web server machines.
  3. Writing an Ansible-Playbook to implement the complete set-up.

Creating a basic HTML File!

Using the code shown below, a very basic webpage is created. (You can add anything according to your convenience in the code!)

Basic HTML Code!

Creating dynamic Load Balancer Configuration File!

Ansible is built using Python Language & Ansible uses Jinja2 framework for resolving the variable values while performing the operations requiring value resolution. Therefore Jinja2 templating is used in the configuration file of the Load Balancer so that it can dynamically update the webserver machines IP Addresses using the Ansible Inventory.

Below shown code is used to dynamically retrieve the IP Addresses from the “Webservers” group from the Ansible Inventory.

Dynamic Load Balancer Configuration File!

Note: Use the group name of webserver machines as “Webservers” & “LB” as the group name of the machines which has to be configured as a Load Balancer in the Ansible Inventor if you want to use the same code as shown in this blog. Otherwise, if you use other group names, then update them in the code.

Ansible-Playbook to automate every task!

The below-shown code is for the ansible-playbook which will perform all the magic & implement everything.

Every task is given a proper, sensible, & meaningful name in the code so that everyone can easily understand what is happening.

Ansible-Playbook to configure the complete project!

By executing the above code successfully, you will be able to access the webservers using the Load Balancer IP Address. For Example, after the successful setup, when the Load Balancer is hit, you will see(if you have used the same webpage as used in this blog):

Image by Author!

To learn more about Ansible & its installation, please visit my GitHub Repo on Ansible which contains multiple Ansible Projects & Documentation.

To get the complete code of this project in one place, please visit the link given below:

Congratulations, you successfully implemented a super amazing project!

I hope my article explains each and everything related to the significance of Mean Squared Error with all the deep concepts and Mathematics. Thank you so much for investing your time in reading my blog & boosting your knowledge!

--

--

Harshit Dawar
LinuxWorld Informatics Pvt. Ltd.

AIOPS Engineer, have a demonstrated history of delivering large and complex projects. 14x Globally Certified. Rare & authentic content publisher.