Deploying a Load Balancer and multiple Web Servers on AWS through ANSIBLE!
By: Uditanshu Pandey
Tags: AWS, Ansible, Haproxy, Ec2, Load Balancer

This article will help you to know that How we can launch HAPROXY Loadbalancer and Multiple WEBSERVERS on the top of AWS Cloud with the help of ANSIBLE.

Before that let’s discuss some of the basic terminologies.

So, What are LoadBalancer and HaProxy?

Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool.

HAProxy (High Availability Proxy) is a TCP/HTTP load balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints. This is useful in cases where too many concurrent connections over-saturate the capability of a single server.

What is Ansible?

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

What we are going to do throughout this article?

🔅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.

There is some prerequisite before doing this task:

Ansible should be installed in the system.

Boto library should be installed in the system to work with aws through ansible.

Should have an AWS account.

Should have basic knowledge of AWS.

Let’s start doing this.

We will be launching Ec2 Instances in the AWS cloud. For this, we will be writing a playbook but before that, we have to create a Security Group in AWS Cloud.

We can also do this through ansible only but here we will be doing it manually.

For this go to your AWS account and search for Ec2 service. After that click on Security Groups on the left side. Here on the top, you will find the Create Security Group option click that.

Give Security group name, Description and in InBound rules and OutBound rules allow these rules.

Now, Let’s create a Playbook for the ec2 instance.

This playbook will launch One LoadBalancer and Three WebServers. In this Playbook, we will be using an ansible vault for storing our AWS credentials.

sec.yml is the name of our ansible vault file.

Now, let’s create an ansible vault to store the AWS access key and secret key.

ansible-vault create --vault-id Udit@prompt sec.yml

This command will create and file with the name sec.yml. It will ask password at the time of creation.

Now, let’s run this playbook and see whether our paybook works or not.

To run the playbook

ansible-playbook --vault-id Udit@prompt <playbook name>

Let’s move to the AWS portal look at our 3 backend servers and 1 load balancer has been launched.

Now, we have to create an inventory of these instances so that we can work on them. We can fetch the IP of all instances in two ways either in the static way going to each instance finding their IP and then updating it in the Inventory file or using Dynamic inventory.

In this article, we will be using Dynamic inventory.

To create a dynamic inventory -

Create a directory with any name.

Here we are working on AWS Ec2 Instance therefore we will be downloading python scripts for AWS created by the Ansible team.

We have to download two files

ec2.py

ec2.ini

To download both the files

Now, we have to change these files a little bit

In our system we have python3 installed so, we have to change a python interpreter from python python3 in ec2.py file.

In the ec2.ini file, we will set our working region, access key, and secret key.

We have choose region ap-south-1.

Put your access and secret key.

Now, we will also export these variables

export AWS_REGION='ap-south-1'



export AWS_ACCESS_KEY_ID='XXXXXXX'



export AWS_SECRET_ACCESS_KEY='XXXXXXXXX'

Just run these commands in the terminal with your credentials.

Now, we have to make ec2.py and ec2.ini file executable for that we will run commands in terminals.

chmod +x ec2.py chmod +x ec2.ini

Now, let’s check that if it is working or not by running

ansible all --list-hosts

If all things were correct we will see all the IP addresses of our launched instances in AWS.

Now, we will be adding all these IP’s to our static Inventory file.

For this, we will need the private key which will be useful to connect with the ec2 instance.

The user will be ec2-user only.

We will be creating two groups in our static inventory file one for LoadBalancer and the other for WebServer.

Now, let’s check that if we have connectivity with all the instances or not.

To check connectivity we will ping each instance. For this command is

ansible all -m ping

We have connectivity with all our instances.

Now, let’s move to further steps.

Till here we have launched instances over the cloud and fetched their IP’s dynamically.

Next, we will be installing HAPROXY and HTTPD in the launched instances.

For this, we will be creating two roles load balancer and webserver.

Ansible role is a set of tasks to configure a host to serve a certain purpose like configuring a service. Roles are defined using YAML files with a predefined directory structure. A role directory structure contains directories: defaults, vars, tasks, files, templates, meta, handlers.

For creating roles

ansible-galaxy init webserver ansible-galaxy init loadbalancer

First, we will be configuring our WebServers.

Go to the tasks folder inside the webserver role and open main.yml.

Here we will be install httpd, will copy the content, and then start the HTTP server.

For this, we will be writing a playbook.

Now, We will configure LoadBalancer.

Open loadbalancer role then go to task folder and open main.yml.

Here we will install haproxy, configure haproxy.cfg file and finally start service.

First let’s see how to edit haproxy.cfg file.

We have to configure haproxy.cfg file also through which our load balancer will come to know about our backend servers.

For this we will copy the haproxy.cfg file from the internet, edit it and then copy it to our load balancer.

We need to store the haproxy.cfg file inside this “templates” folder.

Changes to be made in the file:

In haproxy.cfg file in line number 68 the port no should be “8080”.

In line 88, we need to mention our backend servers with IP address.

Here, we are updating this file dynamically therefore we will use Loops here.

Just update the block of code in the file same as given below.

Now, we will write playbook for LoadBalancer.

Playbook for load balancer

Now, open the handlers folder and edit the main.yml file.

We have to tell ansible that we are using roles in our ansible file for this we have to add a variable in ansible.cfg file.

roles_path = path of your role folder

Just create a playbook for running our roles on inventory groups.

- hosts: webserver

become: yes

roles:

- role: webserver

- hosts: loadbalancer

become: yes

roles:

- role: loadbalancer

Just run the playbook and we are good to go.

To run the playbook

ansible-playbook <file_name.yml>

It’s done.

Let’s check that all the things were correct or not.

Our httpd is installed on all the web servers. Content is copied in the folder and service is also started.

Also, our load balancer is configured now. Haproxy installed, haproxy.cfg file copied, and service started.

Look our haproxy.cfg file is also configured. Port no and all our backend servers IP are here.

To check the output

Copy the public IP address of LoadBalancer and paste it in the browser with port number(8080).

Great It’s working.

To check that our load balancer is working or not just refresh the page.

Great our LoadBalancer is balancing the traffic.

Github Link:

Thanks Everyone for reading this article.