Creating Ansible role to configure HAproxy LoadBalancer

RAJNISH MISHRA
7 min readFeb 3, 2021

We have discussed how to configure the Apache webserver with help of Ansible. But if we want to launch multiple, web servers due to an increase in traffic. So, we have to set up a load balancer to manage the load.

As we are configuring load balancer to manage load, so there might be instances when we have to do scaleup or scale down. So, entering the IP of every new server in the load balancer can be a very tough task. So, we will be using dynamic inventory.

What is HAProxy?

HAProxy Enterprise combines HAProxy, the world’s fastest and most widely used open-source software load balancer and application delivery controller, with enterprise-class features, services, and premium support.

HAProxy Enterprise is a powerful product tailored to the goals, requirements, and infrastructure of modern enterprises.

it provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers.

HAProxy is used by a number of high-profile websites including GoDaddy, GitHub, Bitbucket, Stack Overflow, Reddit, Slack, Speedtest.net, Tumblr, Twitter, and Tuenti and is used in the OpsWorks product from Amazon Web Services.

What is Load Balancing?

In computing, load balancing refers to the process of distributing a set of tasks over a set of resources (computing units), with the aim of making their overall processing more efficient. Load balancing techniques can optimize the response time for each task, avoiding unevenly overloading compute nodes while other compute nodes are left idle.

Load balancing is the subject of research in the field of parallel computers. Two main approaches exist static algorithms, which do not take into account the state of the different machines, and dynamic algorithms, which are usually more general and more efficient, but require exchanges of information between the different computing units, at the risk of a loss of efficiency.

Pre-requisites

  • Install boto and boto3 Python Library
#pip3 install boto3

Configuration File

As we are configuring ec2-instance, so we need to provide a key location and use become to log in as root.

Creating Roles

Roles are like an empty skeleton of an architecture you have to add steps by typing code into the modules presents inside these roles.

  • Ansible playbooks can be very similar: code used in one playbook can be useful in other playbooks also
  • To make it easy to re-use the code, roles can be used.
  • An Ansible role is composed of multiple folders, each of which contain several YAML files.
  • By default, they have a main.yml file, but they can have more than one when needed.
  • This is a standardized structure for all Ansible roles, which allows Ansible playbooks to automatically load predefined variables, tasks, handlers, templates, and default values located in separate YAML files.
  • Each Ansible role should contain at least one of the following directories, if not all of them.
roles
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml

Here we will be creating three roles: one is for launching ec2, one for load balancer configuration, and another for web server configuration.

As roles are successfully configured, we will create tasks for each respective role.

ec2_provisioning

After creating the role we create the task for launching the ec2 instance.

./tasks/main.yml

We launch 4 instances: 3 as a webserver and 1 as a load balancer. Tags are very important as they will be used in dynamic Inventory.

We use variables here for security.

./vars/main.yml

We have vars folder for storing the variables, from there ansible picks all the variable values by default. We also need to encrypt the variable file as it contains sensitive information.

webserver

After creating the role we create the task for deploying the webserver.

./tasks/main.yml

handlers/main.yml

haproxy_LB

After creating the role we create the task for deploying the load balancer.

./tasks/main.yml

./handlers/main.yml

./templates/haproxy.cfg

inventory

wget https://raw.githubusercontent.com/Rajnish-TheGreat/Ansible_playbook_for_haproxy_loadserver/master/inventory/ec2.py
wget https://raw.githubusercontent.com/Rajnish-TheGreat/Ansible_playbook_for_haproxy_loadserver/master/inventory/ec2.ini

To create a dynamic inventory, we need a python script. The script will go to AWS and retrieve the IP from there.

Here we need 2 files one is ec2.py and another one is ec2.ini.

Before running the file we have to export the AWS access key and AWS secret key so that authentication will be done successfully

export AWS_REGION='ap-south-1'
export AWS_ACCESS_KEY_ID='IAM User Access Key Here'
export AWS_SECRET_ACCESS_KEY='IAM User Secret Key Here'

then we are capable of getting the IP of instance or os running on the cloud (AWS CLOUD).

After This will need to make these files executable. So, that we can use them

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

You may face these errors while executing the file. So, you can solve the error by the method shown in the image.

The structure of roles and playbooks will be like this:

.
├── ansible.cfg
├── config.yml
├── ec2_provisioning
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ │ └── main.yml
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── README.md
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
├── ec2.yml
├── haproxy_LB
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── README.md
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── haproxy.cfg
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
├── inventory
│ ├── ec2.ini
│ └── ec2.py
└── webserver
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml

Now, we just need to run the playbook and see the magic.

First, we will run the playbook of ec2. As we have stored variables inside the ansible vault, we have to provide password also.

Now, we will run the playbook to configure the web server and HAproxy.

We can connect to the load balancer and see load balancing.

Now we can create as many Load Balancers as we like and do scale up and scale down as we like. The playbooks can be downloaded from the GitHub link below.

I hope the article was able to solve your manual configuration issues. If you have any feedback or suggestion, you can comment below.

You can appreciate the article by giving it a like and posting comments about your feedback here or on LinkedIn.

--

--