Load Balancing With HAProxy On AWS Instance Using Ansible Playbook (12.2)

Lakshmi Priya Patro
4 min readJan 18, 2022

What is a Load-Balancer?

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 the capacity (concurrent users) and reliability of applications.

Load balancers ensure reliability and availability by monitoring the “health” of applications and only sending requests to servers and applications that can respond in a timely manner.

What is a Reverse Proxy?

A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client, appearing as if they originated from the reverse proxy server itself.

A reverse proxy is an intermediary for its associated servers to be contacted by any client. In other words, a proxy is associated with the client(s), while a reverse proxy is associated with the server(s); a reverse proxy is usually an internal-facing proxy used as a ‘front-end’ to control and protect access to a server on a private network.

What is 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 requests across multiple servers. It is written in C and has a reputation for being fast and efficient.

In this article, we will set up a reverse proxy using haproxy program on one of our AWS instances with the help of Ansible and run it.

Let’s start…

Step-1: Install Ansible in Controller Node

yum install ansible

Step-2: Add hosts in Inventory File and check them by pinging all hosts

Step-3: Write an Ansible Playbook having the name task12.2.yml

Here I have written in two parts i.e. target node group is for Backend Servers and host group is for Load Balancer Server

- hosts: host
tasks:
- name: "install haproxy"
package:
name: haproxy
state: present
- name: "configure haproxy.cfg file"
template:
src: "/root/ansible_wks/ans_task_12/haproxy.cfg.j2"
dest: "/etc/haproxy/haproxy.cfg"
- name: "starting haproxy service"
service:
name: haproxy
state: restarted


- hosts: targetnode
tasks:
- name: "installing httpd in ManagedNode"
package:
name: httpd
state: present
- name: "copying data to target node"
copy:
dest: /var/www/html/haproxy.html
content: "hiii from managed node"
- name: "start httpd service"
service:
name: httpd
state: started
enabled: yes

Step-4: Now copy Haproxy configuration file in Controller Node from Load Balancer Target Node and then there convert it into .j2 extension and write code in haproxy.cfg.j2 configuration file in json format

Step-5: Run the Ansible Playbook

Step-6: Check the Load Balancer Target Node and Backend Server Target Node

Load Balancer:

Backend Server:

Step-7: Now check the Web Browser that using IP:Port of Load Balancer we can retrieve data of Backend Server

Step-8: Now we have made our playbook dynamic so after adding one more IP of Backend Server in Inventory File we can follow all the steps again so that dynamically its IP will add in the haproxy.cfg.j2 configuration file and all other tasks assigned in the playbook

Inventory File:

Run Playbook:

Check Load Balancer:

Check Backend Server:

Check Web Browser:

So we can see that dynamically IP of Backend Servers gets added in HAProxy configuration file in Load Balancer.

Below is the GitHub link that consists of some important files we used to configure HAProxy

https://github.com/priya231299/ConfiguringHAProxyUsingAnsible

Thanks for Reading!!

--

--