Configuring Load Balancer on AWS using Ansible with dynamic inventory!
Shubhankar Thapliyal
DevOps Engineer ? at AAPC || Cloud Enthusiast! || Ex Engineer@Mindtree
Task To Be Done:
- Provision EC2 instances using ansible.
- Retrieve IP Addresses of instances using the dynamic inventory concept.
- Configure web servers through ansible role.
- Configuring load balancer through ansible role.
- The target nodes of the load balancer should auto-update as per status of web servers.
Summary: One-Click Instance Launched, Web Servers provisioned and Load Balancer is ready!
What is a Load Balancer?
A load balancer serves as a single point of contact for clients. A load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application. You add one or more listeners to your load balancer.
CREATING A LOADBALANCER USING ANSIBLE:
First, we will set up an Ansible environment in our system. For doing anything on aws using local system with the help of ansible, we will install the boto3 library of python.
# pip3 install boto3
Now we launch the ec2 instances on AWS by writing .yml code.
webserver.yml:
loadbalancer.yml:
Create one more pass.yml file for storing the my-access and my-secret keys.
# vi pass.yml
myaccess: "ACCESSKEY" mysecret: "SECRETKEY"
After creating and saving the pass.yml file we have a vault which we want to keep secured. For this we have used:
# ansible-vault encrypt pass.yml (File_Name)
Now we run the ansible-playbook to launch the Webservers and Load Balancer:
- webserver.yml:
- loadbalancer.yml:
After this, check if the aws instances if they have successfully launched or not.
Now, set up the Dynamic inventory environment by using python:
host.py:
Now to make these files executable run the following command:
# chmod +x host.py
Now, we need to configure inventory and add some other details.
After saving the inventory file and running the following commands, we will get the IP of the ec2-instances. The Python code fetches the IP of aws ec2-instances, and will also play the role of dynamic inventory. following are the IP of webserver and loadbalancer instances respectively.
Now we check if that IP is properly pinging or not:
- Webservers:
- Webservers:
Now we create roles for the configuration of haproxy into the Load-Balancer instance and httpd into the Web-Server instances.
We'll use the following command for creating roles load balancer and webserver:
# ansible-galaxy init <Role_Name>
For checking the Roles List we will execute:
Now configure Haproxy service inside the load balancer role:
handlers:
haproxy.cfg:
Now configuring httpd server inside the webserver role:
roles.yaml:
Running ansible-playbook command:
Now checking that haproxy service has started or not in the Load-Balancer instance:
haproxy.cfg:
Outputs:
Thank you for reading my article. I hope it will help you understand the concepts worked on. If any improvement is required, please let me know.
Associate Software Engineer @Veritas Technologies LLC | Building Kubernetes Protection | Top 1% @Leetcode | Specialist @Codeforces
4 年Great sir?