Deploy 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.

What is Load Balancer?🤔

A load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load balancers are used to increase capacity (concurrent users) and reliability of applications. They improve the overall performance of applications by decreasing the burden on servers associated with managing and maintaining application and network sessions, as well as by performing application-specific tasks.

What is HAproxy?🤔

HAProxy, which stands for High Availability Proxy, is a popular open source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris, and FreeBSD. Its most common use is to improve the performance and reliability of a server environment by distributing the workload across multiple servers (e.g. web, application, database).

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. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency. And Ansible, as noted above, is rapidly rising to the top in the world of automation tools.

🌎Now we are going to perform the following steps-

👉Using Ansible playbook we will Configure Haproxy.

👉Update it’s configuration file automatically on each time new Managed node join the inventory.

👉Two machines are used one as a WebServer and another as LoadBalancer.

👉Now, we will create an inventory and IP address will be defined.

To list all the hosts:

ansible all --list-hosts

We will ping all machine by this command

ansible all -m ping

Now, we will be configuring our WebServers.

- hosts: webserver
- name: install httpd
name: "httpd"
state: present
- name: PHP Installation
name: php
state: present
- name: copy content
src: index.php
dest: /var/www/html/index.php
- name: sevice restart
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

print ` /usr/sbin/ifconfig ' ;

Now, We will configure LoadBalancer.

- hosts: loadbalancer
- name: install haproxy
name: "haproxy"
state: present
- name: copy my conf
src: "haproxy.cfg"
dest: "/etc/haproxy/haproxy.cfg"
- name: start
name: "haproxy"
state: started

Here we will install haproxy, configure haproxy.cfg file and finally start service.

Full Code of Playbook

Command to run the playbook:

ansible-playbook <file_name.yml>

It’s Done

Our httpd is installed on all the web servers. Content is copied in the folder and service is also started.

Our LoadBalancer is also configured successfully.

haproxy is installed on the machine which works as LoadBalancer.

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 For Reading😊