HAProxy (Load Balancer) Configuration Using Ansible on AWS

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).

Task Description :

  1. Provision EC2 instances through ansible.
  2. Retrieve the IP Address of instances using the dynamic inventory concept.
  3. Configure the web servers through the ansible role.
  4. Configure the load balancer through the ansible role.
  5. The target nodes of the load balancer should auto-update as per the status of web servers.

So lets begin the task …..

Launching EC2 instance using Ansible and setting up dynamic inventory

In this blog I have configured Ec2 instance using Ansible . You dont need to follow whole blog , just follow upto setting up of dynamic inventory.

Instance launced using Ansible by running script one at a time however we can make changes in script to launch more than 1 instance

Updating Inventory File

Updating Ansible Config File

Listing all host

Create a folder which contains all the roles

ansible-galaxy  init  webserver   ---> role created for webserveransible-galaxy  init  lbserver    ---> role created for LoadBalancer

Structure of Role Folder (make sure to set the path of role inside ansible configuration file (ansible.cfg)).

Writing main.yml that will run all roles

Now configuring webserver

vim webserver/tasks/main.yml

Now configuring loadbalancer

vim lbserver/tasks/main.yml

There is a file that is called “haproxy.cfg”. This particular file is working as a template that is used to configure the haproxy dynamically according to the requirement. So we have to make some changes in this file .

We need to change the port number binding. You can use any port eg 1234. Here i have used 8080. Also we need to provide the public ip of all the instances with 80 port. To give Ip randomly here we can use Jinja Template to extract the hostname of each ec2 instance.

Running Playbook

ansible all -m ping

This ad-hoc command will check the host whether it is connected or not and give the output “pong” if the nodes are connected successfully or pingable.

The following output can be seen

18.191.192.66| SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3.139.69.93| SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3.21.159.243 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}

Great! It’s working fine…

Now, it’s time to run the playbook by using the following command

ansible-playbook main.yml#############Go to the Load Balancer Ip and port nohttp://<load_balancer_IP>:><port_no>/<web_page>
http://<load_balancer_IP>:>8080/index.php ####in my case

I’m an undergraduate student at IIIT Ranchi, pursuing my B-Tech in Electronics and Communication Engineering.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store