Showing posts with label HAProxy. Show all posts
Showing posts with label HAProxy. Show all posts

Sunday, 6 March 2016

HAProxy Installation CentOS


01. Edit kernel configuration file to add below line. Add this at the end-of-file.
Load balancing in HAProxy and Keepalived at the same time also requires the ability to bind to an IP address that are nonlocal, meaning that it is not assigned to a device on the local system. This allows a running load balancer instance to bind to an IP that is not local for failover.

]# vim /etc/sysctl.conf   


net.ipv4.ip_nonlocal_bind = 1




 02. Reload kernel settings without restart system.
]# sysctl -p



03. Install haproxy package.

]# yum install haproxy -y




04. Edit the file. Note, we have ready-made configuration file 'haproxy.cfg' with this notes. Just copy/paste it over here.



]# vim /etc/haproxy/haproxy.cfg



05. Check the configuration file for any errors.

]# haproxy -c -f /etc/haproxy/haproxy.cfg




06. Start haproxy service.

]# service haproxy start


]# chkconfig haproxy on





08. TESTING: Use the IP of HAPROXY server in browser.


Browser url : http://ip-address-of-server



09. View haproxy logs. (Press CTRL+C to quit)


]# tail -f /var/log/messages









STATISTICS:


Browser : http://IP-ADDRESS/haproxy?stats





Note, Username and Password specified in haproxy.cfg file.




10. Restart haproxy everytime you update the configuration file.

]# service haproxy restart

11. Use below command to stop haproxy service.

]# service haproxy stop





Now Setup Cron:

=============




[root@ip-172-31-9-134 web]# crontab -l
#0 0 * * * /bin/bash /opt/backup/web/folder-bak.sh >> /opt/backup/web/folder-bak.log
1 1,3,5,7,9,11,13,15,17,19,21,23 * * * /opt/backup/web/log-rotate.sh >> /opt/backup/web/log-rotate.log
[root@ip-172-31-9-134 web]# cat log-rotate.sh
#!/bin/sh

#############################################################
# File:         folder-backup.sh
# Description:  A shell script to backup Multiple FOLDERs
# Cron Example: 0 2 * * * sh /path/to/direbackup.sh
#############################################################
# a. create a folder /opt/backup/web b. check DISKFREE parameter c. --exclude option in tar
####DEFINE THESE#############################################
FOLDERLIST="/var/log/messages";
DUMPBASEDIR="/opt/backup/web";
SCPTOREMOTE=0;

####END CHANGE HERE###########################################

DATE=`/bin/date +%d-%b-%Y_%k:%M`;
DUMPDIR="$DUMPBASEDIR/$DATE";

    if [ ! -d $DUMPDIR ];
    then
          echo "Creating Directory: ".$DUMPDIR;
            mkdir $DUMPDIR;
    fi

for FOLDER in $FOLDERLIST
do
        echo "FOLDER $FOLDER Backup Started";
    WOBFOLDER=${FOLDER/#\//}
    tar -czf $DUMPBASEDIR/$DATE/${WOBFOLDER//\//-}.tar.gz $FOLDER
        echo "FOLDER $FOLDER Backup Completed";

done

cat /dev/null > /var/log/messages   

exit;



:wq



=======================



FC main sample config file :

 haproxy]# cat haproxy.cfg
#---------------------------------------------------------------------
# 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 local0

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid

    maxconn    20000
    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         1m
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 20000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80

      acl has_url_begning url_beg -i /?betaexit=1 /m/ /m? /svc/ /r/ /robots.txt /svcstage/
      use_backend webfarm-redirect if has_url_begning

      acl has_url_pathdir path_dir -i m svc r svcstage
      use_backend webfarm-redirect if has_url_pathdir

      acl is_match_string url_sub betaexit=1 /?betaexit=1 /m/ /m? /svc/ /r/ /robots.txt /svcstage/
      use_backend webfarm-redirect if is_match_string

      acl has_url_extension path_end -i xml txt .xml .txt
      use_backend webfarm-redirect if has_url_extension

      acl cookie_found hdr_sub(cookie) betaexit
      use_backend webfarm-redirect if cookie_found

      default_backend    webfarm

#---------------------------------------------------------------------
# backend for above conditional redirection
#---------------------------------------------------------------------
backend webfarm-redirect
       balance roundrobin
       cookie SERVERID insert indirect nocache
       server web01 www.example.com.edgesuite.net:80 check cookie web01

#---------------------------------------------------------------------
# the default backend
#---------------------------------------------------------------------
backend webfarm
    balance roundrobin
    cookie SERVERID insert indirect nocache
    server web01 www.example.com.edgesuite.net:80 check cookie web01 weight 89
    server web02 Beta-example-Application-LB-1080059167.ap-southeast-1.elb.amazonaws.com:80 check cookie web02 weight 11 

#---------------------------------------------------------------------
# statistics for haproxy
#---------------------------------------------------------------------
stats enable
stats uri /haproxy?stats
stats realm Haproxy\ Statistics
stats auth haproxy:stats
stats refresh 1s

===========================

haproxy]# cat haproxy.cfg
#---------------------------------------------------------------------
# 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 local0

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid

    maxconn    20000
    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    10m
    timeout queue           10m
    timeout connect         10m
    timeout client          10m
    timeout server          10m
    timeout http-keep-alive 1m
    timeout check           1m
    maxconn                 20000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80

    redirect prefix http://www.example.com code 302 if { hdr(host) -i beta.example.com }
      default_backend    webfarm

#---------------------------------------------------------------------
# the default backend
#---------------------------------------------------------------------
backend webfarm
    balance roundrobin
    cookie SERVERID insert indirect nocache
    server web01 www.example.com:80 check cookie web01 weight 100
#    server web02 Beta-example-Application-LB-1080059167.ap-southeast-1.elb.amazonaws.com:80 check cookie web02 weight 80

#---------------------------------------------------------------------
# statistics for haproxy
#---------------------------------------------------------------------
stats enable
stats uri /haproxy?stats
stats realm Haproxy\ Statistics
stats auth haproxy:stats
stats refresh 10s