LAUNCH LOAD BALANCER USING HAPROXY AND CONFIGURE WEB SERVER USING ANSIBLE PLAYBOOK ON AWS
KEY LEARNINGS -
WHAT IS ANSIBLE ?
Ansible is an open-source software provisioning, configuration management, application-development tool enabling infrastructure as a code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.
What is AWS?
Amazon Web Services(AWS) is a cloud service from Amazon, which provides services in the form of building blocks, these building blocks can be used to create and deploy any type of application in the cloud.
These services or building blocks are designed to work with each other, and result in applications that are sophisticated and highly scalable.
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.
Statement- Deploy a Load Balancer and Multiple Web Servers on AWS instances By ANSIBLE
?? Provision EC2 instances through ansible.
?? Retrieve the IP Address of instances using the dynamic inventory concept.
?? Configure the Web Servers through the ansible.
?? Configure the Load Balancer through the ansible .
?? The target nodes of the load balancer should auto-update as per the status of web servers.
SOLUTION -
Since AWS doesnt provide their instances/os to manage by other platform , so we need to make our own controller node as a AWS ClIENT , and than by using "BOTO" sdk/software in CN we can deploy instance at AWS.
SO , run this two commands in CN to install BOTO sdk.
pip3 install boto pip3 install boto3
Now we have to write the yml file for provisioning the aws instance. In this file I launch 4 instance 3 for webserver and 1 for load balancer. I launch the instances in two zones(1a and 1b) for disaster recovery.
mkdir aws vi task3.yml
Now, we are creating a VAULT to store the ACCESS_KEYS AND SECRET KEYS
NOW in this file store -
access_key: xxxxxxxxxxxxxx
private_key: xxxxxxxxxxxxxxx
We will check by ping module -
Now , run the playbook command in CN
>>> ansible-playbook --vault-id aws@promot task3.yml
TOTAL 4 SEVER HAS BEEN LAUNCHED ON AWS INCLUDING , FIRST IS FOR CONFIGURING HAPROXY LB , REMAINING THREE ARE FOR WEBSERVER
Now we have to find the ip of this instance in our CN , by using python dynamic inventory code. So we download this code 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
Inside ec2.py, just change one line => #!/usr/bin/python3
NOW , We Need To Export access and secret keys and region
export AWS_REGION='ap-south-1' export AWS_ACCESS_KEY_ID='XXXXXXXXXXX' export AWS_SECRET_ACCESS_KEY='XXXXXXXXXXX'
In the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY you have to write your aws access key and secret key which you got at the time of user creation in aws.
Now we have to make these files executable. So for this we have to type.
chmod +x ec2.py chmod +x ec2.ini
Now we can see the instance ip by dynamic inventory by typing
ansible all --list-hosts
Now we have to set the inventory according to this ip. my invntory is present at /etc/ansible/myhosts and I already copy the key from windows to Virtual Machine by using WinScp.
Now you have to configure the ansible config file which is present at /etc/ansible/ansible.conf location.
Now I have created two roles one for webserver and one for load balancer. You can create roles by typing.
my roles path is >> cd /etc/myroles
Create roles in this folder only and give location in config file.
ansible-galaxy init ws_task3 ansible-galaxy init lb_task3
WRITE TASK FOR CONFIGURING WS -
WRITE TASK FOR CONFG. LB_SERVER
HERE I HAVE GIVEN SOURCE OF COPY FILE > haproxy.cfg , right now it is not present in our CN , So I am providing you my GITHUB link , YOU CAN COPY FROM THERE AND PASTE IT INTO > /templates/haproxy.cfg
https://github.com/vaibhavjain2099/haproxy.cfg.git
NOW GO TO FILE OF templates/haproxy.cfg AND EDIT BACKEND AS :8080 AND IN FRONTEND WE GIVE A LOOP , SO THAT IT AUTO ASSIGN IP OF ALL WS.
NOW EDIT THE handler FILE
Now we have write the ansible- playbook for configure web server on this aws instance.
Make a file > vi task3.yml inside this file give info about roles and hosts
Now for running this playbook > ansible-playbook task3.yml
You can check the haproxy.cfg file at lb server
Now in the last you can see that all the ip of webserver is written here means load balancer balance the load.
HERE WE NOTICED THAT IF LOAD COME ON SERVER , HAPROXY LB AUTOMATICALLY BALANCE THE LOAD BY SWITCHING TO ANOTHER WEBSERVER , WITHOUT CHANGING IP , AND HENCE CLIENT WILL NOT FACE ANY PROBLEM .
............................... END .................................