Configuring reverse proxy with Ansible and also updating HAProxy conf file each time new node(aws instance)join inventory

Rohitbhatt
4 min readDec 24, 2020

This blog is about how we can Configuring Reverse Proxy i.e. Haproxy and updating it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory on AWS.

First we have to know what is HAProxy?

Basic Architecture of HAProxy

So What HAProxy actually is..?

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 terms of processor and memory usage)

Now lets do the practical..

🔹First we have to update the Ip addresses in Inventory file(#vim /root/ip.txt) of controller node on which we want to configure something.

In this Inventory I have 2 IP’s , one for Load-balancer and another one for Backend server.

🔹Ansible Configuration file

In this file have to update both , Inventory file location and also our key to login into AWS Instance(key should be present in controller node). The path of file is #vim /etc/ansible/ansible.cfg

Ansible Configuration file.

Now check the connectivity of Managed Nodes with Controller Node.

#ansible all -m ping all command is used to check the connectivity.

here we can clearly see that our both Managed nodes are connected with Controller node.

Now we see our Ansible Playbook

🔹Ansible playbook

host: load_balancer ➯ For Load balancer

host: b_server ➯ For Backend server

🔹HAProxy file configuration

🔹 Running Playbook

Now we are done with the Controller node configuration..!!

Lets check it manually on managed node.

🔹Managed node

Lets have a look of what we configured actually.

Load balancer:-

⭐HAproxy service started⭐

⭐HAproxy config file⭐

here you could see that this file is dynamically updated with the Ip of the Backend server.

🔹checking on browser

You can match the IP of backend server here ⬇

Now we can add as many new instances as managed nodes to configure them as a backend servers by just updating their IP’s to the inventory file and dynamically it will update it to the configuration file of haproxy.

So let’s configure one more backend server as a apache httpd webserver with the same webpage by just adding its IP i.e 3.7.45.127to the inventory file.

Now Ping again ,just to confirm that new instance is pinging or not

Now we just have to run the playbook again and thats it.

This server is launched successfully.

Lets check whether this Backend server is updated in Haproxy file of loadbalancer or not.

🔹Checking haproxy config file

You could see both the iP’s of the both the servers are updated here dynamically.

You can match the IP of backend server here ⬇

🔹Finally ..lets check both the servers on browser.

Hence verified..!!

✨Loadbalancer is working very fine…✨

For Furthur Queries, Suggestion’s Feel Free to Connect with me On Linkedin.

www.linkedin.com/in/rohit-bhatt-97499b188.

Thank you!!!!!!!!!

--

--