How to configure Load Balancer and webserver on AWS using Ansible Playbook?
By: Shubham Rasal
Tags: AWS, Load Balancing, Ansible, Haproxy, Automation

ANSIBLE-5

How to configure Load Balancer and webserver on AWS using Ansible Playbook?

Before starting how to configure the load balancer and web server let’s understand what is load balancer and webservers.

Load Balancer

The load balancer is software by which we can distribute some tasks to a set of resources to achieve more efficiency in the overall processing.

Why we need a load balancer?

Suppose you have a single server and it has standard hardware and software... and as you grow or suddenly traffic for your app increases. Now each server has some limitations for serving requests at a time, it can be hardware as well as software. Now you decided to add one more server and it comes with a new IP address and then you again need to add this IP as A record in the domain name provider such as godaddy.com or namecheap.com or AWS route 53.

Now you gain more and more clients and more web servers you start adding then it gets complicated… because we need to add these new IPs in the update the DNS provided by the domain name provider. But to reflect these changes in DNS it takes some time (hell lot of time)

Due to a lot of stuff and more complexity... we want something that will manage our servers and face the client's requests and then spread requests to our webservers and again respond back to clients. — — A Load Balancer.

we can use this load balancing in many places such as for web servers or you can use it in API servers or database servers.

So we are going to use “HAPROXY” Load Balancer because it free, open-source software that provides a high availability load balancer and proxy server for TCP and HTTP-based application that spreads requests across multiple servers. It is written in C and very fast and efficient and uses the Round Robin algorithm.

Let's understand the action plan first… before getting hands dirty.

Problem Statement:

Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update its configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.

And set up this on AWS cloud.

I have created a small video explaining how to configure load balancer using ansible-playbook?

I am assuming that you have knowledge about AWS. You can check the below article to launch instances using AWS CLI.

So let’s understand the architecture we need to solve this use case.

We have two security groups. One for the load balancer and the second for webservers. Create a security group for load balancer allowing ports 80 and 5000 port and 22 port for ssh. Create another security group and allow load balancer group in its inbound rule for port 5000 and 22 port for ssh. so that outsiders/clients can not directly connect to our webservers.

2. As you can imagine the architecture we want to create from the above image.. So create one instance as a load balancer and assign a load balancer security group. Create 2–3 instances and assign a webserver security group.

Now we have done with AWS setup… So let's start with Ansible and write some interesting playbooks. I am using my local machine as the controller node here.

You can get a brief idea about the setup from the below diagram.

We want to configure webservers using apache server and then add that new server entry in load balancer that way load balancer can also spread requests to the new node.

Now we have a private key file of instances but our ansible should be able to connect to these instances...

The simplest method would be to add your own public keys to your EC2 instance and ignore the PEM file for all future logins

$ ssh-add keyfile.pem

now you are ready to connect using ssh as well as from ansible also.

so the next step is to add instances IP in the inventory file.

Check the connectivity using

$ ansible all -m ping

Now let’s see the folder structure I have created for configuring load balancers as well as web servers.

Here, we have load balancer directory will have load balancer specific files i.e configuration jinja template file and variables file, and then webservers directory will have its configuration template and variables file.

source code has simple index.php which echoes the IP address of the machine.. so we can identify easily that our setup is working or not.

now let’s see how to configure the webserver

You can read more in brief about configuring web server in the below article

This is the configuration file of the apache server where we are changing the document root and port for our project.

Now let’s see web servers vars.yml file

In this file, we are giving the project name, source code directory path, config file path, the port number where we want to deploy our webserver, and the document root of the project on the server.

also, we have to make an entry of the webserver after it is configured as a webserver into the load balancer file so the load balancer can use that webserver. for that we have to update its config file. To dynamically update its file again we will use the jinja template.

we need to add these jinja lines into haproxy.cfg file. It will read the IP addresses from the webserver group in an inventory file and automatically add all the entries in the load balancer.

Now let us see ansible-playbook.

The above playbook will download apache and PHP on webservers then transfer the .conf file and configure the webserver.

In the second play, it will configure a load balancer and also register the webserver into the load balancer.

To see the output, I recommend you to watch the video mentioned above.

You can find all the code and files in below Github repository.

If you have any doubts or something improvement needed in this blog, please feel free to reach out to me on my LinkedIn account.

I hope you learned something new and find ansible more interesting.

Let me know your thoughts about ansible and how do plan to use ansible?

Thank you.

About the writer:

Shubham loves technology, challenges, is open to learning and reinventing himself. He loves to share his knowledge. He is passionate about constant improvements.

He writes blogs about Cloud Computing, Automation, DevOps, AWS, Infrastructure as code.

Visit his Medium home page to read more insights from him.

👋 Join FAUN today and receive similar stories each week in your inbox! ️ Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬