Deploying a Load Balancer and multiple Web Servers through ANSIBLE!
This article will help you to know that How we can launch HAPROXY Loadbalancer and Multiple WEBSERVERS with the help of ANSIBLE.
Before that let’s discuss some of the basic terminologies.
So, What are LoadBalancer and HaProxy?
Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool.
HAProxy (High Availability Proxy) is a TCP/HTTP load balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints. This is useful in cases where too many concurrent connections over-saturate the capability of a single server.
What is Ansible?
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.
What we are going to do throughout this article?
🔅 Using Ansible playbook we will Configure Reverse
Proxy i.e. Haproxy and update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.
We will be using two machines as a WebServer and one machine as LoadBalancer.
First, we will create an inventory and there we will be defining the IP address of all the machines.
To list all the hosts:
ansible all --list-hosts
Now, let’s check that if we have connectivity with all the machines or not.
To check connectivity we will ping each machine.
For this command is
ansible all -m ping
Now, we will be configuring our WebServers.
- hosts: webserver
- name: install httpd
- name: PHP Installation
- name: copy content
- name: sevice restart
This block of code will Install HTTPD, PHP, copy the index.php file and finally start the HTTPD service.
Content inside index.php file
print ` /usr/sbin/ifconfig ' ;
Now, We will configure LoadBalancer.
- hosts: loadbalancer
- name: install haproxy
- name: copy my conf
- name: start
Here we will install haproxy, configure haproxy.cfg file and finally start service.
First, let’s see how to edit haproxy.cfg file.
We have to configure haproxy.cfg file also through which our load balancer will come to know about our backend servers.
For this, we will copy the haproxy.cfg file from the internet, edit it, and then copy it to our load balancer.
Changes to be made in the file:
- In haproxy.cfg file in line number 68 the port no should be “8080”.
- In line 88, we need to mention our backend servers with IP addresses.
Here, we are updating this file dynamically therefore we will use Loops here.
Just update the block of code in the file same as given below.
Full code of Playbook:
Just run the playbook and we are good to go.
Command to run the playbook:
Let’s check that all the things were correct or not.
Our httpd is installed on all the web servers. Content is copied in the folder and service is also started.
Also, our Loadbalancer is configured successfully.
Now, let’s check that if our haproxy.cfg file is configured dynamically or not.
It has been successfully updated.
To check the output
Copy the IP address of LoadBalancer and paste it into the browser with port number(8080).
Great, It’s working.
To check that our load balancer is working or not just refresh the page.
Here, Just look at the marked lines. The IP address is changing as soon as the page refreshes even though the IP address which we are using in the browser is the same.
This proves that the HAproxy is working fine.
Our LoadBalancer is balancing the traffic.
Contribute to Uditanshu0110/Haproxy-and-Webservers development by creating an account on GitHub.
Thanks Everyone for reading this article. 😇