Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE

Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE

Task Description 📝

🔹 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

Before getting started, let's discuss about some of the basic keywords :

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 (High Availability Proxy) :

HAProxy is a tool which helps in TCP/HTTP load Balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints.

No alt text provided for this image

Ansible:

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.

Let's Start!!

First we have to write a playbook to provision our ec2 instance which I have already shown in my previous task. For detailed description have a look 👀

This is my playbook for provisioning ec2 instance :

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

Let's check on aws whether they are launched or not

No alt text provided for this image

Now,we have to retrieve the Ip of the instances.

For dynamic inventory, we have to write scripts in python file. We can also download it given by Ansible team

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
No alt text provided for this image

In ec2.py we have to change the extension of python by python3 or you can install python version 1

No alt text provided for this image

In ec2.ini file we have to give our aws region, aws secret key and access key

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

Now you need to export the below command in the terminal itself

export AWS_REGION = 'ap-south-1'
export AWS_ACCESS_KEY = ' '
export AWS_SECRET_KEY = ' '

Then make both the file executable

chmod +x ec2.py

chmod +x ec2.ini

Let's check whether it worked or not

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

Let's check the connectivity between our localhost and all the instances

ansible all -m ping
No alt text provided for this image

Now we have to configure our instances as load balancer and webservers

Let's do it by making separate roles for loadbalancer and webserver

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.

We can create roles using ansible galaxy

ansible-galaxy init webserver

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

Write your all the tasks in the file in folder named Tasks inside your webserver role which will do the task in your webservers only.

In this we have to install httpd and php by using package module, copy the webpages in /var/www/html folder and then started our httpd services.

No alt text provided for this image

And your load balancer tasks in your lbserver role.

In this we have to download HAPROXY using yum and copy the configuration file of haproxy (haproxy.cfg) with following changes in the file:

◾ First we have to change port no. (if you want to) to 8080

◾ Apply jinja code to update all the IP's of backend servers

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

And our final playbook which include both the roles

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

Everything done successfully!!

Let's check our webpages:

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

THANK YOU ALL!!



To view or add a comment, sign in

More articles by Aditya Khandelwal

  • Continuous Integration of Jenkins, Docker and GitHub

    We are going to build following jobs: 1. Create container image that’s has Jenkins installed using dockerfile 2.

  • Automation using Jenkins

    What is Jenkins ? Jenkins is an open-source Continuous Integration server written in Java for orchestrating a chain of…

  • K-means Clustering and it's Use-Cases

    What is K-means Clustering ? K-means clustering is a very famous and powerful unsupervised machine learning algorithm…

  • Use Case of JavaScript in DevOps

    IT field has seen the emergence of Node.js, a development platform that enables more streamlined software development…

    3 Comments
  • Cyber Crimes and Confusion Matrix

    It is easy to forget in a time of huge advances in Computer Science and Artificial Intelligence that by its very nature…

  • GUI Container on Docker

    Task Description 📄 🔹 Launch a container on docker in GUI mode. 🔹 Run any GUI software on the container.

  • Summer Program 2021

    Task Description 📄 🔹 Pull the Docker container image of CentOS image from DockerHub and create a new container 🔹…

    2 Comments
  • RH294 Training Experience

    Let's summarize what I have learned in my Ansible RH294 training Ansible is a software tool that provides simple but…

  • Configuring Target Node Dynamically

    Task Description📝 Create an Ansible Playbook which will dynamically load the variable file named same as OS_name and…

  • Deploy Web Server on AWS through ANSIBLE

    Task Description 📝 🔰Write an Ansible PlayBook that does the following operations in the managed nodes: 🔅Provision…

    2 Comments

Others also viewed

Explore content categories