Configuration of Load balancer using Haproxy on Aws cloud with the help of Ansible.

Configuration of Load balancer using Haproxy on Aws cloud with the help of Ansible.

Task:

♦️ 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.

What is ansible?

*Ansible is an open source IT Configuration Management, Deployment & Orchestration tool. It aims to provide large productivity gains to a wide variety of automation challenges. This tool is very simple to use yet powerful enough to automate complex multi-tier IT application environments. 

what is webserver?

Web Server is a computer that runs websites. It's a computer program that distributes web pages as they are requisitioned. The basic objective of the web server is to store, process and deliver web pages to the users.

What is a Load Balancer?

In computing, load balancing refers to the process of distributing a set of tasks over a set of resources, with the aim of making their overall processing more efficient. In terms of Cloud Computing, Load Balancing automatically distributes incoming application traffic across multiple targets, such as Amazon EC2 instances, containers, IP addresses, and Lambda functions. It can handle the varying load of your application traffic in a single Availability Zone or across multiple Availability Zones.

what is haproxy?

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.

PRACTICAL WORK :-

Step1- First of all, we have to install boto before launching the instance.

pip3 install boto

pip3 install boto3

Step2- Create a role for launching the instance on the top of AWS.

mkdir /etc/myroles
mkdir /etc/myroles

cd /etc/myroles


ansible-galaxy init ec2_role
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Step3- Inside the vars/main.yml file, we have to pass AWS Access Key and Secret Key. We have to provide Access key inside "myuser" variable and Secret key inside "mypass" variable -

vim vars/main.yml
    
   myuser: xxxxxxxxxxx
   myuser: XXXXXXXXX
   

Step4- Inside ansible configuration file.

vim /etc/ansible/ansible.cfg
No alt text provided for this image
cd 

cd /projects

vim launch.ym
No alt text provided for this image

Step5- Now, we have to run the ansible playbook using below command.

ansible-playbook launch.yml
No alt text provided for this image

OUTPUT OF Step5 -

No alt text provided for this image

Step6- To get the Instance IP dynamically in the inventory, we have to download ec2.py and ec2.ini files

mkdir /mydb

cd /mydb

yum install wget 

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py

No alt text provided for this image
No alt text provided for this image
Inside ec2.py, just change one line => #!/usr/bin/python3
No alt text provided for this image
# wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
No alt text provided for this image
export AWS_REGION='ap-south-1'

export AWS_ACCESS_KEY_ID='XXXXXXXXXXX'


export AWS_SECRET_ACCESS_KEY='XXXXXXXXXXX'

Step7- Now inside the ansible configuration file, change the inventory to /mydb -

No alt text provided for this image

Step8- Use command given below which will show all the host IP's.

ansible all --list-hosts
No alt text provided for this image

Step9 - First we have to create roles for configuring the webserver and haproxy software inside the EC2 instances.

cd /etc/myroles

ansible-galaxy init Webserver

ansible-galaxy init lbserver


No alt text provided for this image
No alt text provided for this image

For lbserver -

No alt text provided for this image

*Make some changes inside the haproxy.cfg file.

vim haproxy.cfg
No alt text provided for this image
vim tasks/main.yml
No alt text provided for this image
vim handlers/main.yml
No alt text provided for this image

Step10- Now we have to create a file inside the projects folder for the webserver and lbserver roles .

cd 

cd /pojects

vim setup.yml


No alt text provided for this image

Step11- Make some changes inside the inventory file and the ansible.cfg file.

vim /etc/myhosts.txt
No alt text provided for this image
vim /etc/ansible/ansible.cfg
No alt text provided for this image

Step12- Now, for running the setup.yml playbook we have to use command given below.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

THANK YOU

To view or add a comment, sign in

More articles by Ritikesh Chauhan

Others also viewed

Explore content categories