How to write a Ansible Playbook to Configure Reverse Proxy and Configure it with Apache Webserver?

Prerequisite:

What is ansible playbook?

An Ansible playbook is a file where users write Ansible code, an organized collection of scripts defining the work of a server configuration. They describe a set of steps in a general IT process or a policy for your remote systems to enforce. Playbooks consist of one or more plays run in a particular order.

What is HAProxy?

HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for very high traffic web sites. Over the years it has become the standard for open source load balancing. It’s also free and comes with most Linux distributions.

Understanding Load Balancing

Load balancing ensures the availability, uptime and performance of your servers, websites and applications during traffic spikes. It’s especially important for companies with multiple data centers and high traffic websites or apps.

Load balancing is defined as follow:

A performance optimization tactic that provides fault-tolerance by splitting incoming tasks across multiple computing resources, thereby preventing any one resource from becoming overloaded.

To further explain, a load balancer is a workload distributor across multiple computing resources. These resources can be computer clusters, network links, CPUs or disk drives.

Load balancing aims to achieve optimal resource usage, maximize server stability, and prevent individual components from becoming overloaded. It divides the amount of work that a server has to do between two or more servers, allowing more work to get done in the same amount of time.

What is web server?

A web server is a computer that runs websites. It’s a computer program that distributes web pages as they are requisitioned. The basic objective of the web server is to store, process and deliver web pages to the users. This intercommunication is done using Hypertext Transfer Protocol (HTTP).

In this task we will create 1 load balancer and 2 web servers, so let’s install haproxy and httpd services in the respective systems and start the services.

Codes to install and start haproxy in load balancer:

Codes to install and start httpd service:

To configure a reverse proxy we have to update the IP of the web server in the configuration file of the Haproxy(/etc/haproxy/haproxy.cfg). This can be achieved manually entering the ip or by dynamically updating the IP’s directly from the inventory in the controller node. We have to add certain codes in the configuration file of haproxy for updating it dynamically, Below are the codes to dynamically update the managed node ip’s in load balancer configuration file.

Now, run the playbook in which we have written the codes to Configure Reverse Proxy i.e. Haproxy and update it’s configuration file automatically each time a new Managed node (Configured With Apache Web Server) joins the inventory.

Now, go to the load balancer and web servers to check their status of connectivity in the following way:

As we can see the connections are up, we will now connect to the IP of the load balancer and it will redirect us to one the web server.

Checkout the codes here: Ansible Playbook to Configure Reverse Proxy and Configure it with Apache Webserver

Thank you for reading!!!

A B.tech undergrad, enthusiastic towards learning new technologies in the market and integrate the technologies with each other.