Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE!
đź”…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.
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.
HA Proxy Load Balancer: HA Proxy (High Availability Proxy) is a TCP/HTTP load balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints. This is useful in cases where too many concurrent connections over-saturate the capability of a single server.
Step1: Install Amazon Web Service (AWS) Software Development Kit (SDK)
Boto is AWS SDK. For connecting AWS we need to install boto library in our local environment.
In my case I have already installed boto library. (Note: In case if boto/boto3 is not installed on your env, install by pip3 install boto and pip3 install boto3.
Step2: Launch AWS Instance with ansible yml file.
Write a playbook as “aws_ec2.yml”
Note: Now we have to create one more playbook called secret.yml to store aws_access_key and aws_secret_key. Now run ansible playbook to provision EC2 Instances.
AWS Instance Launched Successfully !!!
3. Retrieve IP of Launched Instance using dynamic Inventory.
Fetching IP of instance dynamically we want to download ec2.py and ec2.ini scripts from GitHub.
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
This scripts help to fetch IP dynamically.
Now to execute this files for retrive IP we should change their permission
chmod +x ec2.py
chmod +x ec2.ini
For execution we need to export some variables.
export AWS_REGION=’us-east-1'
export AWS_ACCESS_KEY_ID=’aws_access_key’
export AWS_SECRET_ACCESS_KEY=’aws_secret_key’
This variables export to our system to AWS and retrieve IPs.
Now we can check our IPs.
Now check that our ip is properly pinging or not
4. Create roles for webserver and Load balancer and deploy it.
First we create roles for webserver and load balancer
ansible-galaxy init lb
ansible-galaxy init web
Before creating code for roles first install haproxy on controller node and configure the haproxy.cfg file.
yum install haproxy -y
In haproxy.cfg file we have to give port number. Here I am giving 8080 port. Also we have to give public IPs for webservers. Here I have used Jinja template to extract IPs.
Now write yaml code in loadbalancer role to configure haproxy load balancer on ec2 instance
/lb/tasks/main.yml
/lb/handlers/main.yml
Now we write web role for webservers.
/web/tasks/main.yml
5.Lets create ansible playbook to run instance role and run playbook.
6. Now it’s time to check haproxy.cfg file and status of HAProxy in Load Balancer.
7.Now we can check in browser that our Load Balancer is working or not.
Thank You !!!!
I would like to Thanks to Mr. Vimal Daga & Mrs. Preeti Chandak from LinuxWorld Informatics Pvt Ltd for Training and Support to emerging technologies to all become India Future Ready.