Deploying HAProxy Loadbalancer on AWS Cloud with backend AWS Instances using Ansible

Kanishka Shakya
5 min readJan 5, 2021

INTRODUCTION TO HAPROXY:-

HAProxy, which stands for High Availability Proxy, is a popular open source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris, and FreeBSD. Its most common use is to improve the performance and reliability of a server environment by distributing the workload across multiple servers (e.g. web, application, database). It is used in many high-profile environments, including: GitHub, Imgur, Instagram, and Twitter.

In this guide, we will provide a general overview of what HAProxy is, basic load-balancing terminology, and examples of how it might be used to improve the performance and reliability of your own server environment.

INTRODUCTION TO LOADBALANCER:-

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

Modern high‑traffic websites must serve hundreds of thousands, if not millions, of concurrent requests from users or clients and return the correct text, images, video, or application data, all in a fast and reliable manner. To cost‑effectively scale to meet these high volumes, modern computing best practice generally requires adding more servers.

A load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load balancers are used to increase capacity (concurrent users) and reliability of applications. They improve the overall performance of applications by decreasing the burden on servers associated with managing and maintaining application and network sessions, as well as by performing application-specific tasks.

In this guide we will be provisioning an HAProxy LoadBalancer on AWS Cloud and integrate it with multiple web servers running on AWS Instances.The whole setup will be deployed using Ansible.

AWS has an SDK for python boto3 which should also be installed using pip installer:-

For using AWS we have to provide our Access Key ID and Secret Access Key.We can provide AWS credentials using the following environment variables.

Configure Ansible on Controller Node

Ansible by default uses a file named ansible.cfg as it’s configuration file.Create a directory /etc/ansible and in this create a file ansible.cfg

In the configuration file we have to provide the path to our inventory,name of remote-user(optional) and the private key file which we will use as AWS key-pair for SSH connection.Additionally,we will be using ec2-user as sudo in AWS instance so we have to provide some privilege escalations as well.

Create roles for Launching AWS Instances for LoadBalancer and Webservers:-

In Ansible,we have to create a directory in which we will maintain all our roles and provide it’s path in ansible.cfg file as roles_path parameter.For creating role,we can use ansible-galaxy command.

Here,we have created two roles,one for launching the ec2 instance which will work as LoadBalancer(aws_ec2) and the other for instances which will work as web servers(aws_webserver_instances).

For launching ec2 instance on AWS,we will use a module named ec2.In this,we will provide our VPC Subnet Id,AMI Id,Security group Id,tags,key name etc as properties.Since,this is a task we have to write this in the main.yml file of tasks folder.

In the task file of LoadBalancer the count is 1,whereas in the task file of Webservers the count is 3.

We have mentioned the values of most properties inside braces “{some_value}”.The values inside these braces are called variables.Now,that we have used these variables we have to provide values to these variables somewhere.The values of these variables are written in the main.yml file of vars folder.

Create Role for Webserver

We creating webserver role again use the ansible-galaxy init command.

In the configuration of webserver the steps required are as follows:-

  • Install httpd software.This can be done using packages module.
  • Copy the contents of the webpage from the Controller node to the Managed Nodes/AWS Instances in the/var/www/html folder.This can be done using the copy module.
  • Start httpd services using services module.

Create Role for LoadBalancer

Again, use the ansible-galaxy init for creating a new role.

Now,in the Haproxy LoadBalancer setup we have to configure the LoadBalancer and provide the frontend port number and the IP addresses of the backend servers.All these configurations are done in the /etc/haproxy/haproxy.cfg file.

Install haproxy in Controller Node:-

Create ec2 Playbook:-

Instances have been launched.

After successful launching of Instances now we have to fetch their public IP as to configure them as Webserver and Loadserver using Ansible.Here we use the concept of Dynamic Inventory which automatically fetch target IP.

for fetching the IP dynamically we have to download ec2.py and ec2.ini python scripts.

AWS Instances in the /var/www/html/folder.

and ,the loadbalancer is successfully deployed on AWS cloud using Ansible.

THANK YOU ALL!!!

--

--

Kanishka Shakya

Aviatrix Certified Engineer | DevOps | Python | Big Data | RHCSA 8 | AWS-CSA | AWS-DEVELOPER | Ansible | Docker | CKA & CKAD | GIT & GITHUB |