Deploying Load Balancer Using HAProxy and Multiple Webservers on AWS Instances Through Ansible

ADARSH KUMAR
Dec 30, 2020 · 5 min read

In this blog We are going to launch HAPROXY LoadBalancer and multiple WebServers on the top of the ec2-instance through the Ansible.

At first, let’s discuss about some terminologies.

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) is a TCP/HTTP load Balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints.

The Basic Architecture of Load Balancer

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.

What we are going to Do:-

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

Prerequisite:

  1. Ansible should be Installed in the system
  2. Basic Knowledge of AWS cloud

Let’s Begin!!

Step 1:

Since Ansible is built on the top of python, a python sdk is required that enables the configuration of AWS services.The package is an object-oriented API named boto3.

pip3 install boto3

Now ,We have to launch ec2-instance in the AWS cloud.For this we will be writing a playbook.

This playbook will launch Three webservers and One LoadBalancer.And User authentication is done by providing the ACCESS_KEY and SECRET_KEY .

Now to launch the AWS instances we will create a playbook.

[root@localhost haproxy]# cat web.yml
- hosts: localhost
roles:
- role: ec2_role

Our three webservers and one Load Balancer has been launched at AWS

Step 2:

Now,we have to retrieve the Ip of the instances ,So that we can create an inventory of these Instances to configure them . We will retrieve the ips with the dynamic Inventory.

For dynamic Inventory

  • First create a directory
  • In this directory download two scripts created by Ansible team for AWS.
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.iniwget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py

After downloading the file ,we have to change a little bit in the files.

In ec2.py we have to change python interpreter from python to python3,as our system have python3 installed.

And In ec2.ini , we have to set our region,ACCESS_KEY,SECRET_KEY.

Now,we have to also export these variables

export AWS_REGION='ap-south-1'

export AWS_ACCESS_KEY_ID='XXXXXXX'

export AWS_SECRET_ACCESS_KEY='XXXXXXXXX'

After exporting the variables , we will make the both file ec2.py and ec2.ini executable.

chmod +x ec2.pychmod +x ec2.ini

We will get all our hosts by running the below command if all things were correct.

ansible all --list-hosts

Now ,We will add these ips to our static inventory file.In the inventory file we will be having two groups one for Webserver and other for Loadbalancer.

After creating the inventory file , we need to configure the ansible.cfg file.Because by default ,login to root user has been disabled.so you can’t login with the root power. So write the below code.

vi /etc/ansible/ansible.cfg

Now, Let’s check the connectivity with the instance by pinging each instance by using below command

ansible all -m ping

We have connectivity with all our Instances

Step 3:

In this step we will be configuring HAProxy and httpd in the launched ec2-instances.

For this ,we will be creating ansible role for deploying the webserver and HAProxy.

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.

ansible-galaxy init webserveransible-galaxy init lbserver

We will first configure the WebServers.

For this open main.yml file in the task folder of the webserver role.

Here we have installed the httpd , and copy the content and started the httpd server.

Configuration of LoadBalancer

For this ,first we have to Download the haproxy.cfg file from internet and edit it and then copy it to the load balancer.

The changes to be made in the haproxy.cfg file:

Now we open the main.yml file from the task folder in lbserver role

Open the main.yml file from the handler folder of the lbserver role and edit the file.

So Now ,we have to just create a playbook for running our roles.

- hosts: LoadBalancer
roles:
- role: lbserver
- hosts: webserver
roles:
- role: webserver

And just run the playbook

ansible-playbook <playbook_name.yml>

Everything is configured Properly!!

Let’s see the output:

The Startup

Get smarter at building your thing. Join The Startup’s +786K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

ADARSH KUMAR

Written by

Tech learner

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store