DEV Community

Cover image for Configuration of HAProxy using Ansible
Nitesh Thapliyal
Nitesh Thapliyal

Posted on

Configuration of HAProxy using Ansible

Hello everyone,

In this blog, we are going to Configure Reverse Proxy i.e. Haproxy and update it's configuration file automatically on each time a new Managed node (Configured With Apache Webserver) join the inventory

Before we start we should know what is HAProxy?

What is HAProxy?

Alt Text

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

Let's get started...

My setup is something like this, I have configured 3 servers in my virtual machine, one is load balancer or Frontend and the other two are Backend Server 👇

Alt Text

Configure the Control node

Create an Inventory and add Frontend and Backend servers IP, username, and password.

Check the connectivity of servers using ping command

Alt Text

Now create Playbook

Alt Text

In this playbook, HAProxy is installed in Frontend that is in load balancer and in Backend webserver is configured and PHP file is uploaded for demo purpose.

HAProxy Configuration

Alt Text

Now run the playbook

Alt Text
Alt Text

  • Now check-in Load Balancer HAProxy is installed or not

Alt Text

Here you can see it is installed successfully!!

Now let's check HAProxy is working or not

For this demo, I have created a PHP code that will open in the browser and will show IP address, from this we can clearly see in the backend how traffic is managed or how the load is distributed.

  • from my Load Balancer or Frontend Ip i will on PHP code in the browser.

Alt Text

Here you can see PHP code works fine and notice the IP Address, this IP is of one of the Backend server.

  • Now let's see what happens when I will refresh the page or when traffic comes

Alt Text

Here you can see the IP of another Backend server. That's how we can manage load in web server.

That's how we can configure HAProxy using Ansible..

Thank you!!❄

Top comments (0)