Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE !!!
Tushar Joshi
Functional Consultant | Business Analyst| Client Communication | Project Management| Python | AWS| Data Analyst
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.
?? Follow the below steps:
Step 1:?Install boto and boto3 libraries..
Step 2:?Write the ansible code to launch ec2 instances and LB… Here is the code ~
This is code in web.yml to launch ec2 instances and configure it as web servers
This is code in lb.yml to launch ec2 instances and configure web servers
Now run the ansible-playbook to launch the Load Balancer and web servers:
Now we can see all the Instances are successfully launched through AWS GUI EC2 Console.
3] After successful launching of Instances now we have to fetch their public IP as to Configure them as Webserver and Load Balancer using Ansible . Here we use the concept of?Dynamic Inventory?which automatically fetch target IP .
To create the Dynamic Inventory we use some python script code which has capability to contact to AWS cloud and fetch the public IP of running Instances .
For fetching the IP dynamically we have to download?ec2.py?and?ec2.ini?python scripts .
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
Now we have to make these scripts files executable by changing the file permissions using command :
chmod +x ec2.py
chmod +x ec2.ini
As Ansible now contact to these python scripts files and go to AWS Cloud and to the tasks For that these scripts files required to export some variables as given below .
export EC2_INI_PATH='ec2.ini_file_path'
export AWS_ACCESS_KEY_ID='aws_access_key'
export AWS_SECERT_ACCESS_KEY='aws_secret_key'
4] We need to configure inventory and add some other details.
领英推荐
Save the inventory file and run?the following commands,?we will get ec2-instances IP. Python code fetches the IP of aws ec2-instances, also will play the role of dynamic inventory. following are the IP of web Use the following command for creating roles load balancer and web server:
Also check instances are pingable or not by using ping command.
5] Now we have to create roles for the configuration of?haproxy?into the Load-Balancer instance and?httpd?into the Web-Servers instances.
Use the following command for creating roles load balancer and web server:
ansible-galaxy init <Role_Name>
6] Now lets move towards the main factor of our task is?HAPROXY Load Balancer.
To install HAPROXY software use command :
yum install haproxy -y
Changing haproxy.cfg file :
7] Configuring?Haproxy service?inside the load balancer role:
handlers:
8]?Configuring httpd server inside the web server role:
9] Now we create one playbook named as?t3_role.yml?which performs the task of configuration of web server and HAPROXY Load Balancer on AWS Instances.
10] Finally running ansible-playbook command:
11] At last we can see the load balancing of web server Instances by HAPROXY Load Balancer using public ip of Load Balancer Instance .
Thanks for reading !!!