Configuring Reverse Proxy and dynamically adding backend servers using Ansible…

Arjun Nigam
4 min readDec 10, 2020

Load balancer comes as a great help to your web servers in balancing the load of your website and helping you to have a better efficiency with your website…

But before going on and explaining to you how I configured a load balancer for my website . Let’s first make you guys comfortable with the terms like Ansible and load balancer.

Ansible

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.

You can find more on it on my previous blog on Ansible where I have talked more on it…

https://arjunnigam10.medium.com/ansible-and-todays-world-86b31edc290e

Now that you are familiar with what is Ansible , let me tell you about Load Balancer which in our case is provided by HAProxy.

HAProxy

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers. It is written in C and has a reputation for being fast and efficient.

Load Balancer

Load balancing is the process of distributing network traffic across multiple servers. This ensures no single server bears too much demand. By spreading the work evenly, load balancing improves application responsiveness. It also increases availability of applications and websites for users.

So now that you got the whole idea of it , it’s time to show you how I configured load balancer using Ansible.

So to configure Load Balancer using Ansible ,I had installed HAProxy.

Install it using command: yum install haproxy

Now that i was all set. I did the changes mentioned in the image below to have a dynamic Load Balancer ( i.e. can add multiple backend servers and can manage it in load balancer).

I did all this change in the haproxy config file — /etc/haproxy/haproxy.cfg

You can view the images of my playbook below:

Now it’s time to run it…

Now as you can observe in the images below ,it was executed successfully…

Now it’s time to check whether our load balancer has been configured or not.

For that we would be requiring our load balancer IP.

Now that we have the IP it’s time to check whether the web server is running currently or not.

Type the following on your browser: Ip_of_Load_balancer:Port_number_you_gave/Web_page_name.html

Now that it Ran successfully its time to add another member i.e. web server to it ( for that i just updated the info in my group inventory file.

Lets run the Playbook now.

And as you can observe a new backend server has been successfully added.

And now let’s check the web server again.

And as you can see it’s running perfectly fine too…

So that is how one can configure a load balancer and add multiple backend servers to it dynamically using Ansible.

That was all from my side . Hope it was beneficial ,keep learning :)

View the Playbook used on Github:

https://github.com/Arjun0071/rproxy

Arjun Nigam

https://www.linkedin.com/in/arjun-nigam-4728b11b8

Signing Off!!!

--

--