Configuring and Updating Haproxy Dynamically using Ansible

Neha sonone
4 min readJan 1, 2021

--

Why we need Load balancer…

if you want to manage the load between the backend server. the client doesn't hit the IP of the backend server because to secure our database we never give direct connectivity to the client to access any application, instead of doing this we can provide the load balancer the client directly hit the IP of the load balancer and the task which this load balancer does is, the name itself suggests that it balance the load between server, the IP which we provide to the client is the load balancer IP

HAProxy (High Availability Proxy) is an open-source proxy and load balancing server software. It provides high availability at the network (TCP) and application (HTTP/S) layers, improving speed and performance by distributing the workload across multiple servers. HAProxy runs on Linux, FreeBSD, and Solaris operating systems.

Haproxy architecture
  • first, you need to create a workspace in your VM
mkdir /ws6
  • create inventory
vim ip.txt
  • here at the initial stage, I'm using one web server, which is a was an instance, and the load balancer is my another VM
  • after the inventory check the connection
ansible all --list-hosts
  • to check the connectivity we use ping
  • after checking, create playbook in the workspace using vim lb.yml
  • what this playbook will do is, in the webserver it will install httpd and in the load balancer, we haproxy software
  • after this run the playbook using the command
ansible-playbook  lb.yml
  • after the playbook run successfully you will see in the load balancer the haproxy. cfg has come using ls u can see
  • now copy the haproxy.cfg file in the lb to the controller node
  • now see in the controller node whether the file exists or not by command
  • now edit the haproxy.cfg file, add the port no.
  • jinja code that can dynamically upload the IP here
  • here is the complete code
  • to run the playbook we use ansible-playbook lb.yml command
  • now after the playbook run successfully check in the load balancer the haproxy.cfg file is updated or not
  • here u can see the port 8080 and the backend server IP is added in the conf file
  • so when you add a new webserver in the inventory the config file will update
  • here I'm adding another webserver to the inventory file
  • run the playbook
  • now check the load balancer config file updated or not
  • u can also see the load balancer working properly or not. by getting loadbalancer IP:8080
  • now if you refresh the page new IP come up

Thank you for reading.

--

--

Neha sonone
Neha sonone

No responses yet