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

4y

Best of luck

Like
Reply
Aakash O.

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

4y

Great work

To view or add a comment, sign in

More articles by Ananya Jain

Insights from the community

Others also viewed

Explore topics