Deploying Load Balancer Using HAProxy and Multiple Webservers on AWS Instances Through Ansible
In this blog We are going to launch HAPROXY LoadBalancer and multiple WebServers on the top of the ec2-instance through the Ansible.
At first, let’s discuss about some terminologies.
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 (High Availability Proxy) is a TCP/HTTP load Balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints.
Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.
What we are going to Do:-
-> 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
- Ansible should be Installed in the system
- Basic Knowledge of AWS cloud
Since Ansible is built on the top of python, a python sdk is required that enables the configuration of AWS services.The package is an object-oriented API named boto3.
pip3 install boto3
Now ,We have to launch ec2-instance in the AWS cloud.For this we will be writing a playbook.
This playbook will launch Three webservers and One LoadBalancer.And User authentication is done by providing the ACCESS_KEY and SECRET_KEY .
Now to launch the AWS instances we will create a playbook.
[root@localhost haproxy]# cat web.yml
- hosts: localhost
- role: ec2_role
Our three webservers and one Load Balancer has been launched at AWS
Now,we have to retrieve the Ip of the instances ,So that we can create an inventory of these Instances to configure them . We will retrieve the ips with the dynamic Inventory.
For dynamic Inventory
- First create a directory
- In this directory download two scripts created by Ansible team for AWS.
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.iniwget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
After downloading the file ,we have to change a little bit in the files.
In ec2.py we have to change python interpreter from python to python3,as our system have python3 installed.
And In ec2.ini , we have to set our region,ACCESS_KEY,SECRET_KEY.
Now,we have to also export these variables
After exporting the variables , we will make the both file ec2.py and ec2.ini executable.
chmod +x ec2.pychmod +x ec2.ini
We will get all our hosts by running the below command if all things were correct.
ansible all --list-hosts
Now ,We will add these ips to our static inventory file.In the inventory file we will be having two groups one for Webserver and other for Loadbalancer.
After creating the inventory file , we need to configure the ansible.cfg file.Because by default ,login to root user has been disabled.so you can’t login with the root power. So write the below code.
Now, Let’s check the connectivity with the instance by pinging each instance by using below command
ansible all -m ping
We have connectivity with all our Instances
In this step we will be configuring HAProxy and httpd in the launched ec2-instances.
For this ,we will be creating ansible role for deploying the webserver and HAProxy.
Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users. Role directory structure. Storing and finding roles. So use the below command for creating the role.
ansible-galaxy init webserveransible-galaxy init lbserver
We will first configure the WebServers.
For this open main.yml file in the task folder of the webserver role.
Here we have installed the httpd , and copy the content and started the httpd server.
Configuration of LoadBalancer
For this ,first we have to Download the haproxy.cfg file from internet and edit it and then copy it to the load balancer.
The changes to be made in the haproxy.cfg file:
Now we open the main.yml file from the task folder in lbserver role
Open the main.yml file from the handler folder of the lbserver role and edit the file.
So Now ,we have to just create a playbook for running our roles.
- hosts: LoadBalancer
- role: lbserver- hosts: webserver
- role: webserver
And just run the playbook
Everything is configured Properly!!
Let’s see the output: