AWS and Configuring Reverse Proxy and dynamically adding backend servers using Ansible…

Arjun Nigam
5 min readDec 14, 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 before jumping on to other things , Please have a look at how an ansible config file and the inventory looks like for this task…

Ansible config. File( /etc/ansible/ansible.cfg)

Please to do take a note : The key.pem file that you are adding here, has to have reading permissions.

For that , command : chmod 400 file_name.pem

And my inventory file…

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

So now that we are done lets create a .php file which would be our webpage in this task.

The image below is of my webpage.php file

You can view the image of my playbook below:

Now it’s time to check whether we have a connection with our managed nodes or not.

Command used: ansible all — list-hosts

And: ansible all -m ping

And as you can observe in the image below , we have a connection…

Now it’s time to run it…

Now as you can observe in the image 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 public 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.php

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

Let’s 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 on AWS.

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

View the Playbook used on Github:

https://github.com/Arjun0071/awsrproxy

Arjun Nigam

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

Signing Off!!!

--

--