Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE
Task Description 📝
🔹 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
Before getting started, let's discuss about some of the basic keywords :
Load Balancer:
A load balancer acts as the “traffic cop” sitting in front of your servers and routing client requests across all servers capable of fulfilling those requests in a manner that maximizes speed and capacity utilization and ensures that no one server is overworked, which could degrade performance. If a single server goes down, the load balancer redirects traffic to the remaining online servers. When a new server is added to the server group, the load balancer automatically starts to send requests to it.
HAProxy (High Availability Proxy) :
HAProxy is a tool which helps in TCP/HTTP load Balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints.
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.
Let's Start!!
First we have to write a playbook to provision our ec2 instance which I have already shown in my previous task. For detailed description have a look 👀
This is my playbook for provisioning ec2 instance :
Let's check on aws whether they are launched or not
Now,we have to retrieve the Ip of the instances.
For dynamic inventory, we have to write scripts in python file. We can also download it given by Ansible team
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
In ec2.py we have to change the extension of python by python3 or you can install python version 1
In ec2.ini file we have to give our aws region, aws secret key and access key
Now you need to export the below command in the terminal itself
export AWS_REGION = 'ap-south-1' export AWS_ACCESS_KEY = ' ' export AWS_SECRET_KEY = ' '
Then make both the file executable
chmod +x ec2.py chmod +x ec2.ini
Let's check whether it worked or not
ansible all --list
Let's check the connectivity between our localhost and all the instances
ansible all -m ping
Now we have to configure our instances as load balancer and webservers
Let's do it by making separate roles for loadbalancer and webserver
Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users. Role directory structure. Storing and finding roles. So use the below command for creating the role.
We can create roles using ansible galaxy
ansible-galaxy init webserver ansible-galaxy init lbserver
Write your all the tasks in the file in folder named Tasks inside your webserver role which will do the task in your webservers only.
In this we have to install httpd and php by using package module, copy the webpages in /var/www/html folder and then started our httpd services.
And your load balancer tasks in your lbserver role.
In this we have to download HAPROXY using yum and copy the configuration file of haproxy (haproxy.cfg) with following changes in the file:
◾ First we have to change port no. (if you want to) to 8080
◾ Apply jinja code to update all the IP's of backend servers
And our final playbook which include both the roles
Everything done successfully!!
Let's check our webpages:
THANK YOU ALL!!