Configuring HaProxy and Web server using Ansible
What is Ansible?
Ansible is a simple open-source IT engine that automates application deployment, intra- service orchestration, cloud provisioning, and many other IT tools.
Ansible is easy to deploy because it does not use any agents or custom security infrastructure.
Ansible uses playbook to describe automation jobs, and playbook uses very simple language i.e. YAML (It’s a human-readable data serialization language & is commonly used for configuration files, but could be used in many applications where data is being stored)which is very easy for humans to understand, read and write. Hence the advantage is that even the IT infrastructure support guys can read and understand the playbook and debug if needed (YAML – It is in human-readable form).
What is Load Balancer?
Load balancing is a key component of highly available infrastructures commonly used to improve the performance and reliability of websites, applications, databases, and other services by distributing the workload across multiple servers.
Task:-
🔰 Install haproxy and httpd in respective nodes
🔰 Configure haproxy
🔰 Start the service of Haproxy
🔰 Create web page
🔰 Start web server services
Solution:-
- hosts: localhost
tasks:
- name: "Installing haproxy"
package:
name: "haproxy"
state: present
- name: "Copying configuration file"
template:
src: "haproxy.cfg"
dest: "/etc/haproxy/haproxy.cfg"
- name: "starting haproxy services"
service:
name: "haproxy"
state: started
- hosts: webserver
tasks:
- name: "Installing httpd"
package:
name: "httpd"
state: present
- name: "copying webpage"
copy:
content: "hi from {{ ansible_facts['hostname'] }}"
dest: "/var/www/html/index.html"
- name: "starting httpd services"
service:
name: "httpd"
state: restarted
We have to copy the template haproxy.cfg after doing the following changes
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# https://www.haproxy.org/download/1.8/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
# utilize system-wide crypto-policies
ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM
#---------------------------------------------------------------------
# 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 hosts in groups['webserver'] %}
server app1 {{ hosts }}:80 check
{% endfor %}
Run the code using the command below
ansible-playbook lb-webserver.yml
After successful execution, you will see below output:-
By going to the IP and port number of the Load balancer we can see our website
By refreshing the page we can see the hostname is changing. Hence Load balancer and Webserver are configured successfully.
That's all!!
For any queries or suggestions feel free to mail me at: akashtiwari370@gmail.com
For the complete code check my GitHub