Deploying Load Balancer using HAProxy with Ansible Automation

Abhiroop Bas
4 min readOct 9, 2020

--

aws — ansible — HAProxy

About the project

The main objective is to deploy a Load Balancer and multiple Web Servers on AWS instance through Ansible. The steps to be followed for accomplishing this are as follows:

· Provision ec2 instances through Ansible

· Retrieve the IP address of the instance using 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.

About Ansible

Ansible 2.9 is a software/tool that is used for configuration management. To launch a server on an OS, it needs to be configured which can be done in the following ways.

1. Manual method

2. Automatic method:

a. Traditional scripts: This includes Imperative Language where the developer needs to specify how a particular action is to be performed.

b. Intelligent scripts: This includes Declarative Language where the developer only needs to enter what is to be done and the coding is done internally by the script.

Ansible is based on python and is thus installed using the pip command. The system (OS) on which the code of ansible is run is called the controller node. All the other devices connected to the network are referred to as managed node.

About HAProxy

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads the requests across multiple servers. It is written in C and has a reputation for being fast and efficient.

Procedure:

STEP 0:

Before you start off, it is important to have the following ready at your fingertips

AWS account

Ansible downloaded

ec2.py downloaded for the ansible documnetation

ansible-inventory set

STEP 1:

Creating an ansible playbook loadbalancer.yml which is run to deploy the web server and load balancer and acts as a one click mode of creating, launching an instance and downloading an starting the services.

ansible-playbook

STEP 2:

Creating ansible role launch which takes care of creating an ec2 instance with the image and the security group mentioned after logging into the console with the access key and security key. This sensitive information is in fact stored in a separate folder under the vars folder of the role or it may also be further protected in an ansible vault.

launch of ec2 instance

STEP 3:

Creating ansible role, loadbalancerserver, which launches and maintains the HAProxy load balancer. This is written in the tasks folder in the main.yml file.

STEP 4:

Creating ansible role, webservers, which creates and deploys a web page on the instance. This includes the file index.html.j2 which contains the code for the webpage. It waits for the load balancer to direct the client to the webpage and in case it is held, the service is restarted.

web server

STEP 5:

Go to your aws console, the ec2 instances with the tag names of LoadBalancer and Webserver are lanched and visiting the webpage at <ipaddress>:8080.

successfully completed.
aws console

Keep following for more such projects. Do drop a mail, with suggestions for improvement at abhiroop1.bas@gmail.com

--

--