Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE!

!! 𝗛𝗲𝗹𝗹𝗼 all!!

Welcome to my article based on TASK-3 of RH-294 -Linux Automation using Ansible (RHCE8) Training


🔅Provision EC2 instances through ansible.

🔅 Retrieve the IP Address of instances using the dynamic inventory concept.

🔅Configure the web servers through the ansible role.

🔅Configure the load balancer through the ansible role.

🔅The target nodes of the load balancer should auto-update as per the status of web servers.

What is Load Balancer?

Load balancing is defined as the methodical and efficient distribution of network or application traffic across multiple servers in a server farm. Each load balancer sits between client devices and backend servers, receiving and then distributing incoming requests to any available server capable of fulfilling them.

So let’s start this task.

Create 5 ec2 Instances. Here I am taking a mixture of t2.micro and t3.micro. You can take any instance type. But be sure take t3.micro for Ansible Node. In which you have to Install Ansible.

1 — — Ansible Node (Controller Node) — t3.micro

1 — — LoadBalancer — t2.micro

3 — — Webserver — t2.micro

here I have used ansible to launch ec2 instances. code for launching ec2 instance using Ansible.

An image guide for better understanding.

Now we have to first create a role inside ControllerNode. Create two roles one for LoadBalancer and another for Webservers.

Set the webservers and load balancers inside Ansible hosts.

create a role folder mkdir /etc/ansible/roles all the roles will be inside this roles folder.

Also, create a directory to manage hosts files. Here i have created

Here a complete picture of the above explanation looks like.

Also, you need to set the path of role inside ansible configuration file (ansible.cfg).

Now we can start writing the playbook inside projects directory.

After that, we have to configure the webserver of each instance. We have written the tasks in the tasks folder and handlers in the handlers folder.

now we can write the tasks inside main.yml.

Here we have completed with configuring webserver. You can also use Handlers to restart the apache server. Now we have to configure the Load Balancer. First, install haproxy inside Controller node.

Then you need to change the port number binding. You can use any port eg 1234. Here I have used 8080. also, you need to provide the public IP of all the instances with 80 port. To give Ip randomly here we can use Jinja Template to extract the hostname of each ec2 instance.

Now go to configuration file of haproxy → /etc/haproxy/haproxy.cfg and copy the haproxy.cfg and paste inside /etc/ansible/roles/lbserver/templates. You can also give the location of configuration file of haproxy.

Now we are ready to run the ansible-playbook. go inside /etc/ansible/roles/projects.From here we can run the playbook.

Now we can check the weather our load balancer is working or not. Take public IP of load balancer with port 8080 (binding port)

As You can see the load balancer using Ansible on AWS is in action.

Summary: One-Click Instance Launched, Web Servers provisioned and Load Balancer ready!

If you find it useful please upvote it.

Connect with me in Linkedin | Github

Lifelong learning technologies

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store