How to Install and Configure HAProxy on CentOS (RHEL) 7 and 6 and 5

Saturday, January 16, 2016

How to Install and Configure HAProxy on CentOS/RHEL 7/6/5

There are following steps :-

1. Install HAProxy :-
    [root@mylappy ~]# yum -y install haproxy

2. Configure HAProxy :-
    
[root@mylappy ~]# mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.org
[root@mylappy ~]# vi /etc/haproxy/haproxy.cfg
# create new
 global
      # for logging section
    log         127.0.0.1 local2 info
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
      # max per-process number of connections
    maxconn     256
      # process' user and group
    user        haproxy
    group       haproxy
      # makes the process fork into background
    daemon

defaults
      # running mode
    mode               http
      # use global settings
    log                global
      # get HTTP request log
    option             httplog
      # timeout if backends do not reply
    timeout connect    10s
      # timeout on client side
    timeout client     30s
      # timeout on server side
    timeout server     30s

# define frontend ( set any name for "http-in" section )
frontend http-in
      # listen 80
    bind *:80
      # set default backend
    default_backend    backend_servers
      # send X-Forwarded-For header
    option             forwardfor

# define backend
backend backend_servers
      # balance with roundrobin
    balance            roundrobin
      # define backend servers
    server             wwwserver1 10.0.0.1:80 check
    server             wwwserver2 10.0.0.2:80 check
   
[root@mylappy ~]# systemctl start haproxy
[root@mylappy ~]# systemctl enable haproxy

3. Configure Rsyslog to get logs for HAProxy :-
   [root@mylappy ~]# vi /etc/rsyslog.conf
# line 15,16: uncomment, lne 17: add
$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP, 127.0.0.1
# line 54: change like follows
*.info;mail.none;authpriv.none;cron.none,local2.none   /var/log/messages
 local2.*                                                /var/log/haproxy.log

[root@mylappy ~]# systemctl restart rsyslog

4. Change httpd settings on Backends to logging X-Forwarded-For header :-
[root@mylappy ~]# vi /etc/httpd/conf/httpd.conf
# line 196: change like follows
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[root@mylappy ~]# systemctl restart httpd

5. Enable and start the haproxy service on each server:-
[root@mylappy ~]# systemctl enable haproxy
ln -s '/usr/lib/systemd/system/haproxy.service' \
  '/etc/systemd/system/multi-user.target.wants/haproxy.service'
[root@mylappy ~]# systemctl start haproxy
If you change the HAProxy configuration, reload the haproxy service:

[root@mylappy ~]# systemctl reload haproxy


6. Make sure all works fine to access to the frontend server from a Client with HTTP to check in browser














0 comments:

About This Blog

Lorem Ipsum

  © Copyright 2009 Linux-HelpLine.Blogspot.com

Back to TOP