Configuring Reverse Proxy using Ansible-Playbook….

Ansh@24
4 min readMar 29, 2021
Haproxy Configuration using Ansible

📚Our goal is to configure Haproxy Server using ansible , while the configuration files of haproxy will be dynamically updated as number of hosts incremented.

First of all, lets understand :
🤔what is Reverse Proxy?

In computer networks such as the internet, a reverse proxy is a common type of proxy server that is accessible from the public network. Large websites and content delivery networks use reverse proxies –together with other techniques– to balance the load between internal servers.

Now lets go more deep ….

🤔What is Haproxy ?

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.

We have gathered together the concept now , thus lets see how to achieve this….

Before commencing any automation / configuration , its recommended to jot down the steps….

🔑STEPS TO CONFIGURE HAPROXY :

1.Setting up yum repository in managed nodes

2.Installing httpd package as well as PHP in managed node (chosen for Webserver(Backend Server))

3.Copying web pages to Backend Servers

4.Starting httpd services

5.Enabling firewall rules for port no 80

6.Installing haproxy server in managed node (Chosen as LoadBalancer(Front End Server))

7.Configuring haproxy server

8.Starting reverse proxy server

Those are the basic steps need to achieve above setup….

Now lets have hands on….

Pre-requisite : Ansible must be installed on Controller node /Local host

# pip install ansible

I will be configuring the setup both in AWS cloud and VM - RHEL8:

lets see how …..

🧱First of all we need to setup ansible inventory :

Go to ansible config folder:

# cd /etc/ansible

Create new txt file : ip.txt

#vim ip.txt

This will contain :

👉Ip of manage nodes.

👉Ansible user .

👉Ansible ssh password.

👉Ansible connection protocol

ip.txt

Now need to specify inventory path in conf file.

Create new file : ansible.cfg

# vim ansible.cfg

Add below context to it…
Here we have specified path of inventory file as well as disable the host key checking…

Also create “index.php” , this would be PHP web-page need to be copied to web server..

#vim index.php

we have printed the IP of the webserver for demonstartion purpose

Also we need to update the haproxy about new backend servers added to group…..

Thus in haproxy.cfg file we need to do following changes , using jinja templating….

At line #88 we have added for loop so that , every host included in webserver group will be added as a backend server for LB…….

Now lets start to write playbook:

Make seperate workspace:

#mkdir haproxy

#cd haproxy

Create yml file:

#vim setup.yml

Run this playbook using :

#ansible-playbook haproxy.yml

Our Backend as well Front End server have been configured …lets check.

Backend Server One IP
Backend Server two IP
Load balancer IP

Now to access the backend servers , in your browser type :

<LB_IP>:PORTNO

in our case: we have 192.168.0.122 as LB IP and exposed port is 8080

192.168.0.122:8080

Backend server two is connected
Backend server one is connected

Thus haproxy balances load among the backend / internal servers distributing the traffic coming to the server as well as abstracting the backend Server from internet….

You can find whole configuration code on my giuthub repo :

Here , both the playbooks are included for AWS and Local VM configurations….

Happy learning ……..🤗

--

--

Ansh@24

Technological Enthusiast , Like to express what is need of time, Relates real world to philosophical insights