Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible
Vishal Dhole
Full Stack Web Developer | Angular | Node.js | AWS | DevOps | Immediate Joiner
Welcome you all to my article based on Integration of aws and ansible of RH-294 -Linux Automation using Ansible (RHCE8) Training
?? TASK DESCRIPTION:
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.
What is Ansible ?
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment .
Ex: In company there are 100s of operating systems may be some of windows, some of macs, some of linux ,ubuntu etc i.e heterogenous environment and we have to configure some software in all operating system then we need to remember commands of all operating systems and require lot of time to configure one by one...Then here come roles of ansible...In ansible Simply we write one playbook in yaml langauge and run from controller node then it configure the software in all managed nodes.
Amazon Web Services:
Amazon Web Services (AWS) is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.
Need of Load Balancer :
Load balancer basically needs to balance the loads among different nodes. as more visitor come to the site it increases the load on server therefore we create same copies of server . Now we have more than one servers(nodes) . here the role of load balancer come up. we set the load balancer layer on the servers layer. Now client come to visit the site load balancer decides to where to send the request of client either first node or second node or third node.
According to traffic load balancer distribute load among the server nodes...
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..
All prerequisite are done...Now starts our journey!!!
1. First of all, we have to set up an Ansible environment in our system. For doing anything on the aws using the local system with the help of ansible then you have to install boto3 library of python.
pip3 install boto3
2. Now write an ansible playbook for creating aws ec2 instances for webserver & load balancer in task3 directory.
3. Now create one ansible vault file key.yml to store access key &secret key
- # vi key.yml
myaccess: "ACCESSKEY" mysecret: "SECRETKEY"
After creating or save key.yml file we have a vault for more secure for this we have used:
ansible-vault encrypt key.yml (File_Name)
For check the vault is working or not:
4. Now run the ansible-playbook to launch ec2 instances for webserver & Load balancer.
check the instances launched or not..
5. Now here we are using python code to find the IP of instance dynamically. So we download this code from GitHub in the directory /etc/ansible:
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 to make this files executable run following commands:
chmod +x ec2.py chmod +x ec2.ini
We need to initiate them, run the following commands, and provide your aws credentials.
export EC2_INI_PATH=path_of_ec2.ini_file export AWS_ACCESS_KEY_ID="aws_access_key" export AWS_SECRET_ACCESS_KEY="aws_secret_key"
This will authenticate our OS to aws ec2 services and fetch public IP address of ec2 dynamically.
6. Now we have to set the inventory according to this ip.
Here, inventory is present at /etc/myinventory.txt and I have already copied the key from windows to Virtual Machine by using WinScp.
7. Now set ansible config file i.e give location of inventory in ansible.config file and to access/ authenticate we need private key, so we give location of our private key by copying it in our os using winscp. Ansible uses existing privilege escalation systems to execute tasks with root privileges or with another user’s permissions. Because this feature allows you to ‘become’ another user, different from the user that logged into the machine , we call it become.
8. Check all hosts are pingable or not..8. Check all hosts are pingable or not..
9. Now create the roles for httpd configuration in webserver instances & haproxy configuration in load balancer instance. For that I have created one directory in /etc i.e /etc/myroles and here i create two roles namely webserver and loadbalancer
10 First we require haproxy load balancer for that
yum install haproxy -y
set the config file for haproxy i.e haproxy.config in such way that having frontend port no 8080 so that user can access webpages of website and backend all nodes in roundrobin using for loop.
11. role for configure haproxy load balancer in loadbalancer instance...
10. Now i write one playbook roles.yml file which run both roles.
11. Now run the ansible playbook roles.yml ..
It runs both webserver & load balancer role and configure httpd in webserver instance & haproxy in loadbalancer instance.
12. Finally we done all steps now check our site is working or not and it actually uses load balancer concept or not using load balancer ip.
Great it actually uses load balancer concept ...see it goes to all ip's according to roundrobin principal..
Now there is no load on any server as traffic increases load balancer manage it...
In this way our task completed successfully!!!!
?? Finally I successfully completed the TASK-3 of Ansible... .
? I would like to thanks Mr.Vimal Daga for giving such challenging task which helps to solve real use cases of Ansible.
??For any queries or suggestions ping me .
!! Thanking you all for visiting my article !!
DevOps @Forescout ?? | Google Developer Expert | AWS | DevOps | 3X GCP | 1X Azure | 1X Terraform | Ansible | Kubernetes | SRE | Platform | Jenkins | Tech Blogger ??
4 å¹´Nice work bro ??
SDE At Siemens
4 å¹´Excellent Work ??
Software Engineer @Cognizant | .NET Full Stack Developer | Web APIs | .NET Core | React.js | Node.js | AWS Developer Tools | AWS Lambda | Amazon CloudWatch
4 å¹´Congrats