Configuring Haproxy and WebServer using Ansible

Configuring Haproxy and WebServer using Ansible

What is 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 allows you to configure not just one computer, but potentially a whole network of computers at once, and using it requires no programming skills. Instructions written for Ansible are human-readable. Whether you're entirely new to computers or an expert, Ansible files are easy to understand.

What is load balancing?

Load balancing is defined as the methodical and efficient distribution of network or application traffic across multiple servers in a server farm. Each load balancer sits between client devices and backend servers, receiving and then distributing incoming requests to any available server capable of fulfilling them.

Task

  1. Install haproxy and httpd in respective nodes
  2. Configure haproxy
  3. Start the service of haproxy
  4. Create webpage
  5. Start webserver services

Solution:

- hosts: localhost
  tasks:
          - name: "installing haproxy"
            package:
                    name: "haproxy"
                    state: present


          - name: " configuring haproxy"
            template:
                  src: "haproxy.cfg"
                  dest: "/etc/haproxy/haproxy.cfg"


          - name: "starting sevices"
            service:
                  name: "haproxy"
                  state: started


- hosts: webserver
  tasks:
          - name: "installing httpd"
            package: 
                  name: "haproxy"
                  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
             


        
       

Now change haproxy.cfg by copying the follwing template:

#---------------------------------------------------------------------
# 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 below command

ansible-playbook lb.yml

After successful execution of the code you will see the following output


No alt text provided for this image

By going to the ip and port no of the loadbalancer we can see the website:

No alt text provided for this image
No alt text provided for this image


Thank you!!

For any query or suggestions feel free to mail me at my email:

kritika.0321321321@gmail.com

You can see the entire code on my github account:


To view or add a comment, sign in

More articles by KRITIKA YADAV

Explore content categories