How to do installation and configuration of haproxy and load balancer using Ansible ?
What is Ansible ?
Ansible automates and simplifies repetitive, complex, and tedious operations. Everybody likes it because it brings huge time savings when we install packages or configure large numbers of servers. Its architecture is simple and effective. It works by connecting to your nodes and pushing small programs to them. These programs make the system comply with a desired state, and, when they have finished their tasks, they are deleted.
What is Haproxy ?
HAProxy is an open source, free, very fast and reliable solution offering high availability, load balancing and proxying for TCP and HTTP-based applications. It is particularly suited for web sites crawling under very high loads while needing persistence or Layer7 processing. It distributes a workload across a set of servers to maximize performance and optimize resource usage. HAProxy can be used for Web applications (HTTP/ HTTPS) as well as for TCP based applications (MySQL, SSL, SMTP) etc.
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.
Prerequisites for HAProxy and Load Balncer:-
- Ansible must be installed on controller node
- HAProxy is required to be install on one of Managed Nodes
- Apache Web Server (HTTPD) must be configure on Target Nodes.
Steps to Configure Load Balancer :-
- First of all configure ansible configuration file at location " /etc/ansible/ansible.cfg " .
- Second Step is to configure the Inventory of ansible on controller node by writing IP, User_name, Password of each Managed or Target Node.
- Third Step is to make php file so that it can be cpoied on managed node for future use :-
<pre> <?php print `/usr/sbin/ifconfig`; ?> </pre>
- Fourth Step is to configure or modify " /etc/haproxy/haproxy.cfg " file using jinja template:-
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
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
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:8080
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
{% for ip in groups['Web_Server'] %}
server app{{ loop.index }} {{ ip }}:80 check
{% endfor %}
- The Last Step of ansible-playbook on controller node for downloading, installing and configuring httpd and HAProxy on target node (instances) to make load balancer and web-server :-
- hosts: Web_Server tasks: - name: "Installling Apache " package: name: httpd,php state: present - name: "Copying Web Pages" template: src: "/Arth-task-12/index.php" dest: "/var/www/html/index.php" register: web - name: "Strating Web Server" service: name: httpd state: started - name: "Restarting Web Serer After Configuration" service: name: httpd state: restarted when: web.changed == True - hosts: Load_Balancer tasks: - name: "Installing and Configuring Load Balancer" package: name: haproxy state: present - name: "Configuring HAPROXY" template: src: /Arth-task-12/haproxy.cfg dest: /etc/haproxy/haproxy.cfg register: x - name: "Starting Haproxy" service: name: haproxy state: started - name: "Started Haproxy with new configuration" service: name: haproxy state: restarted when: x.changed == True
Note:- In this ansible-playbook, I had written two playbook one for load balancer and other for web server.
Now let's run the PlayBook -
Note:- Load Balancer works on Port 8080
Lets Check the OutPut -
Here you can see my Load balancer is working. I'm getting ip of my backend server that means this webpage is running on my backend server.
Thank You for reading....