Configure Load Balancer with HAproxy using AWS EC2 instances.

Configure Load Balancer with HAproxy using AWS EC2 instances.

?? Task Description

Statement: 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.


Let us discuss some Basic concepts.

What is HAProxy?

No alt text provided for this image


HAProxy (High Availability Proxy) is a TCP/HTTP load balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints. HAProxy is known as “the world’s fastest and most widely used software load balancer.” This is useful in cases where too many concurrent connections over-saturate the capability of a single server. Instead of a client connecting to a single server which processes all the requests, the client will connect to an HAProxy instance, which will use a reverse proxy to forward the request to one of the available endpoints, based on a load-balancing algorithm.

HAProxy Architecture

No alt text provided for this image

Above image shows the architecture of Reverse Proxy i.e HAProxy.

Note: To implement this architecture we should have 4 VM(s) or 4 instances. I am using AWS cloud to launch the 4 instances. 1 instance works as Controller Node, 1 instance for Load Balancer and other 2 instances are Managed Nodes(webservers).


In my previous article we've launched a EC2 instance and configured webserver using dynamic inventory. Check out this article:

I've talked about dynamic inventory, by which we can extract information about system to be configured dynamically using some python scripts. Here we'll be using same concept to gather facts about multiple ec2 instances and use it to configure HaProxy on one instance and other with Apache Httpd.

Before we begin, check out prerequisite :

  1. Ansible controller node
  2. AWS account

That's all, now let's begin:

First things first, checking ansible config file:

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

Notice, inventory, roles path set them accordingly and also set the privilege escalation as we need root power to operate.

And to talk about inventory, we've used same scripts as in previous article, but keep one thing in mind that here we've two different configuration to be executed one for load balancer and other for webserver, now for that we need two groups in inventory to differentiate it.

we'll be creating ansible roles:

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

Let's write the Playbook:

No alt text provided for this image

we're launching 1 instance with loadBalancer tag and 3 instances with webserver tag.

Now let's launch this role with play book, don't forget to put access key in loadbal.yml as we did in previous article.

vim launchEC2.yml

No alt text provided for this image

Let's Execute the playbook-

ansible-playbook launchEC2.yml

No alt text provided for this image

Now let's try to ping our hosts:

ansible all -m ping

No alt text provided for this image

To check, the group name we can check it by executing the python script in bash:

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

The highlighted portion, i.e. the groups generated by script.

let's check the connectivity to these instances by pinging:

ansible tag_name_loadbalancer -m ping

ansible tag_name_webserver -m ping


Now let's configure load balancer, for that we'll create another role :

ansible-galaxy init loadbalancer

No alt text provided for this image


Let's write the PlayBook for LoadBalancer:

No alt text provided for this image

Second task is calling a handler, which will restart the haproxy server as the changes in configuration file is made.

No alt text provided for this image


Let's write one more playbook for load balancer:

No alt text provided for this image


Now execute this above playbook:

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

It ran successfully....

------

let's configure webservers:

ansible-galaxy init Webserver

No alt text provided for this image
vim webser.yml
No alt text provided for this image

That's it, now execute this role also with a playbook:

vim taskWEB.yml

No alt text provided for this image

Lets run this playbook now Webserver:

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

Our Playbook is working very fine.

Now Lets check whether our AWS ec2 get setup.

No alt text provided for this image


Note down the public IP of LoadBalancer instance, and

No alt text provided for this image


now to check if load balancing is working or not, refresh the page, and see what comes

First refresh:

No alt text provided for this image

Second refresh:

No alt text provided for this image

 it's redirected to different server both time, observe the Hostname, now lets confirm.

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

That's all, setup is successful...!!


THANK YOU to All...!!



Akshat Jain

DevOps @Plane | DevSecOps Engineer @TechAlchemy | SRE intern @EnableIT | GSoC'22 @Suse | ML | Devops | Linux

4 年

Best of luck

回复
Aakash O.

SDE @Billdesk | Ex-DevOps Intern @SimplifyVMS | 1530 @Leetcode | Spring Boot | Data Structures | Algorithms | System Design | DevOps | ReactJs

4 年

Great work

要查看或添加评论,请登录

Ananya Jain的更多文章

  • Configuring Webserver, VPC on AWS

    Configuring Webserver, VPC on AWS

    ?? Write terraform code to configure the following things on AWS - ??Launch One VPC in north verginia having name as…

  • Configuring Multi-Cloud using Terraform

    Configuring Multi-Cloud using Terraform

    ??Task 1. Create 3 different workspace and create a full stack webserver on 3 different cloud.

  • Hosting WordPress and MySQL on AWS using Terraform

    Hosting WordPress and MySQL on AWS using Terraform

    Task 1. Create an AWS EC2 instance.

    3 条评论
  • Configure AWS EC2 using Terraform

    Configure AWS EC2 using Terraform

    Task: To create and setup key pair, security groups, ec2 instance and attaching it with EBS with storage of 1 GB. First…

  • Coursera Case Study

    Coursera Case Study

    Coursera is an educational technology company with a mission to provide universal access to the world’s best curricula.…

  • MY EXPERIENCE WITH GIT AND GITHUB

    MY EXPERIENCE WITH GIT AND GITHUB

    Git and GitHub are not the same thing. Git is an open-source, version control tool created in 2005 by developers…

    1 条评论
  • Ansible Training Experience

    Ansible Training Experience

    What is Ansible? Ansible is an open-source software that automates software provisioning, configuration management, and…

    2 条评论
  • Configuration of Apache Webservers Dynamically using Ansible on AWS

    Configuration of Apache Webservers Dynamically using Ansible on AWS

    ?? Task Description Statement: Deploy Web Server on AWS through ANSIBLE! ??Provision EC2 instance through ansible…

  • Ansible Playbook that will dynamically load the variable file.

    Ansible Playbook that will dynamically load the variable file.

    ?? Objective : Create an Ansible Playbook which will dynamically load the variable file named same as OS_name and just…

    2 条评论
  • Configuring Docker Container As WebServer Using Ansible

    Configuring Docker Container As WebServer Using Ansible

    >Task Description ?Write an Ansible Playbook that does the following operations in the managed nodes: ?Configure Docker…

社区洞察

其他会员也浏览了