Use Ansible to setup HAproxy as a Load Balancer

Load Balancer

What is HAProxy ?

HAProxy Architecture :

Load balancers can be used to distribute workloads across computers, networks, disk drives or CPU’s. A reverse proxy accepts a request from a client, forwards it to a server that can fulfill it, and returns the server’s response to the client. A load balancer distributes incoming client requests among a group of servers, in each case returning the response from the selected server to the appropriate client.

What is Ansible ?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.

Let’s start the configuration in Ansible …👇

Follow the below steps in Controller Node(CN) :

Step-1 : In CN, create a inventory file which consists of IP’s of load balancer and webservers.

Step-2 : Check the network connectivity to the webservers as well as load balancer using ping module.

ansible host_name -m ping

Step-3 : Create an ansible playbook (lb.yml) containing all configuration of webserver and HAProxy server(Load balancer).

Step-4 : To update HAProxy Configuration file automatically on each time , write the below code at the last in “haproxy.cfg.j2"

Step-5: Run the playbook using command :

ansible-playbook lb.yml (playbook_name)

Step-6: Check the Managed Node (load balancer) , configuration file is updated dynamically. (/etc/haproxy/haproxy.cfg)

  • The port no. is changed to 8081.
  • It’s adding entry of every webserver present in the inventory file of CN.

Step-7: Check the working of load balancer on client browser, as the Client uses load balancer IP only, it doesn’t know from which webserver it is getting the content.

Task completed !✌️

Thanks for Reading !!🙏

Started Learning New Technologies