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
tasks:
- name: install httpd
package:
name: "httpd"
state: present
- name: PHP Installation
package:
name: php
state: present
- name: copy content
copy:
src: index.php
dest: /var/www/html/index.php
- name: sevice restart
service:
name: "httpd"
state: started
This block of code will Install HTTPD, PHP, copy the index.php file and finally start the HTTPD service.
Content inside index.php file
<pre>
<?php
print ` /usr/sbin/ifconfig ' ;
?>
</pre>
Now, We will configure LoadBalancer.
- hosts: loadbalancer
tasks:
- name: install haproxy
package:
name: "haproxy"
state: present
- name: copy my conf
template:
src: "haproxy.cfg"
dest: "/etc/haproxy/haproxy.cfg"
- name: start
service:
name: "haproxy"
state: started
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:
ansible-playbook <file_name.yml>
It’s done.
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.
Github Link:
Thanks Everyone for reading this article. 😇