Configure EC2 instances as Load Balancer using Haproxy s/w by Ansible
AJAY KUMAR YADAV
DevOps Engineer II at Devtron Inc || AWS || Azure || GCP || K8s || Eksctl || KOps || Terraform || Ansible II Jenkins
Problem Statements:-
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.
Note: One-Click Instance Launched, Web Servers provisioned and Load Balancer ready!
Prerequisites:-
- Ansible installed at Controller Node.
- Controller Node must have Boto Library .
- AWS account .
- AWS account must have one user have administrative power.
Approach :-
I used RHEL8 as controller Node , in this OS ansible is installed . I launch 2 instances of name "webserver" and 1 instance of name LB using ansible .Then I setup Load Balancer in LB instance , installing haproxy by ansible and also setup apache web server in webservers instances , installing httpd by ansible . I used ansible roles for configure Load Balancer and web server. At last i register these web servers to the load Balancer by ansible.
Ansible roles:-
Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.
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 s/w:-
HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers. It is written in C and has a reputation for being fast and efficient.
I follow the some steps for this task:-
Step1:-
Provisioning the 2 EC2 instances of name webserver and 1 of name LB by ansible automation tool:-
playbook:-
Now run this playbook:-
At AWS side:-
Now i update my inventory file dynamically by python script:-
->Link for this python script(ec2.py) and make this file executable:-
https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
->For executable:-
chmod +x ec2.py
->Link for ec2.ini file:-
https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
-->After download these files and update the inventory path of ansible , we have to export the AWS credentials:-
export AWS_REGION='ap-south-1' export AWS_ACCESS_KEY_ID='*****************************' export AWS_SECRET_ACCESS_KEY='*******************************'
Command:-
./ec2.py --list
Note:- There are two hosts group one is tag_Name_webserver(2 instances) and another is tag_Name_LB(1 instance).
Now just run ping module for check connectivity:-
Configuration file of ansible:-
Step2:-
I created 2 roles one is lbserver and another is webserver:-
ansible-galaxy init webserver ansible-galaxy init lbserver
Note:- At the controller node conf file of haproxy is present .
webserver role:-
lbserver role:-
Step3:-
Now apply these roles to hosts group(tag_Name_webserver and tag_Name_lbserver):-
Playbook file:-
Now we have to run the playbook:-
Output :-
That's All
Thanks to read