Webserver Load Balancing by HAProxy using Ansible.
By: Lalit Yadav
Tags: Web Server, Haproxy, Ansible, Vimal Daga, Right Mentor

In this article, I am going to discuss on the HAproxy load balancer, how to configure load balancer and managing webserver with HAproxy service by ansible.

What is Ansible ?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.

To install ansible:

$ pip3 install ansible



What is HaProxy LoadBalancer ?

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

Here I am using ansible roles to manage all the things easily and simply.

Ansible Roles:

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.

To create a role:

ansible-galaxy init <rolename>

I have created two roles : one for haproxy and other for webserver. You have to provide the roles path in ansible.cfg file before creating the role.

Here is a look on ansible.cfg file:

$ ansible-galaxy list

The inventory file contain all the IP’s of the target and the username and password of the target node systems.

You can check the connectivity by pinging with the targeted nodes.

$ ansible <groupname> -m ping

The content of a role is:

The content of the lbalancer/tasks/main.yml is :

And you have to install the haproxy software in your controller node to get the the haproxy.cfg file and edit this file as:

Here you have to provide the port number (8080) and by using the jinja format and using group[webserver] it automatically add all the ip’s of the ‘webserver’ group from the inventory file.

Now lets see the awsweb/tasks/main.yml file:

To play a role you have to mentioned in your playbook as:

test.yml in lbalancer role

---

- hosts: localhost

roles:

— lbalancer

- hosts: webserver

roles:

— awsweb



Both the roles (awsweb and lbalancer) , I have shared on my github account. You can see the content and share it also. Github URL:

On running the playbook:

I am using localhost as a haproxy server and other three are the webservers.

On successfully completed the playbook tasks, you can check your result on browser by searching the webpage.

http://<haproxy_server_ip>:<port_no>/<web_page>

On refreshing the page , you can see the different ip address.

That’s the way load balancer works, it distributes the traffic to all the servers equally.

Hope you enjoy reading the article.