LOAD BALANCER USING ANSIBLE
In this blog, We are going to launch HAPROXY LoadBalancer and multiple WebServers on the top of the RHEL8 Linux through Ansible.
What is Load Balancer?
Load balancing refers to the process of distributing a set of tasks over a set of resources, with the aim of making their overall processing more efficient.
HAProxy (High Availability Proxy) is a TCP/HTTP load balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints.
Ansible
Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.
Let’s dev into a practical
Here we are taking three OS’s, one OS for LoadBalancer and twoOS for Managed nodes
Prerequisite:
Ansible should be Installed in the system
Linux basics
Step 1: Now we are going to add IP’s which need to configured in the static inventory file by the name of Inventory1
>> vim Inventory1
Here I used vim editor you can use any like vi, gedit, etc
[WebServer]
192.168.43.208 ansible_user=root ansible_ssh_pass=password ansible_connection=ssh
192.168.43.163 ansible_user=root ansible_ssh_pass=password ansible_connection=ssh
[Controller]
192.168.43.202 ansible_user=root ansible_ssh_pass=password ansible_connection=ssh
Add this code inside the Inventory1 file
Step 2: Configure ansible.cfg file and below code
Note: don’t use the inventory file name to any file
[defaults]
inventory= Inventory1
host_key_checking=false[privilege-escalation]
become=true
become_method=sudo
become_user=root
become_ask_pass=false
Step 3: Copy HAproxy from /etc/haproxy/haproxy.cfg file to this directory then edit as below to get Dynamic IP from Inventory file
>> yum install haproxy -y
>> cp /etc/haproxy/haproxy.cfg /ReverseProxy/haproxy.cfg
After copying edit that file
Go to last and edit backend app at
backend app
balance roundrobin
{% for i in groups['WebServer'] %}
server app{{ loop.index }} {{ i }}:8080 check
{% endfor %}
#server app2 192.168.43.254:80 check
#server app3 192.168.43.89:6085 check
# server app4 127.0.0.1:5004 check
In the below code replace with backend app session even this frontend main
bind *:80
global
log 127.0.0.1 local2chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend main
bind *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .jsuse_backend static if url_static
default_backend app
backend static
balance roundrobin
server static 127.0.0.1:4331 checkbackend app
balance roundrobin
{% for i in groups['WebServer'] %}
server app{{ loop.index }} {{ i }}:8080 check
{% endfor %}
Step 4: Now create an ansible playbook to setup Load Balancer using HAPROXY
Add this code in ReverseProxy.yml file
- hosts: Controller
tasks:
- name: "Haproxy install or check"
package:
name: haproxy
state: present- name: "Start HAPROXY Service"
service:
name: haproxy
state: started- name: "Configure haproxy.cfg file"
template:
src: haproxy.cfg
dest: "/etc/haproxy/haproxy.cfg"
notify:
- Restart servicehandlers:
- name: "Restart service"
service:
name: haproxy
state: restarted- hosts: WebServer
tasks:
- name: "Haproxy install or check"
package:
name: httpd
state: present#- name: "ifconfig IP"i
- name: "Copy Content to a file index.html"
copy:
content: "<pre>
<h1 style='color:green;font-size:100px;text-align:center'>This is LoadBalancer</h1>
<h1 style='color:skyblue;text-align:center;font-size:40px;'>This is Server IP {{ hostvars[inventory_hostname]['ansible_env'].SSH_CONNECTION.split(' ')[2] }} </h1>
</pre>"
dest: "/var/www/html/index.html"
notify:
- Restart httpd Service
- name: "Start a httpd Server"
service:
name: httpd
state: started
handlers:
- name: "Restart httpd Service"
service:
name: httpd
state: restarted
Successfully configured both Load Balancer and Servers
Here we can see Server is configured and automatically fetched IP and PORT number from a inventory file
>> ansible-playbook <playbookName>
Now we can check whether our load balancer is working or not. Take public IP of load balancer with port 80 (binding port) as we see in the video
Step 5: Adding one more SERVER only thing needed to do is just configure inventory everything will be automated
Now run playbook to configure
Now we can check whether our load balancer is working or not. Take public IP of load balancer with port 80 (binding port) as we see in the video
The below image shows us by clicking on load balancer IP and port(192.168.43.208:80) it's connecting us to different servers i.e., Server1: 192.168.43.202:80 and Server2: 192.168.43.220:80 and reducing traffic
GitHub code: