CentOS 7
Ads

HAProxy : HTTP Load Balancing
2015/02/18
 
Install HAProxy to configure Load Balancing Server.
This example based on the environment like follows.
       |
-------+-----------------------------------------------
       |
       +-------------------+--------------------+
       |10.0.0.30          |10.0.0.31           |10.0.0.32
 +-----+-----+     +-------+------+     +-------+------+
 | Frontend  |     |   Backend#1  |     |   Backend#2  |
 |  HAProxy  |     |  Web Server  |     |  Web Server  |
 +-----------+     +--------------+     +--------------+

 
Configure Servers that HTTP connection to HAProxy Server is forwarded to backend Web Servers.
[1] Install HAProxy.
[root@dlp ~]#
yum -y install haproxy
[2] Configure HAProxy.
[root@dlp ~]#
mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.org

[root@dlp ~]#
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             www01 10.0.0.31:80 check
    server             www02 10.0.0.32:80 check
    
[root@dlp ~]#
systemctl start haproxy

[root@dlp ~]#
systemctl enable haproxy

[3] Configure Rsyslog to get logs for HAProxy.
[root@dlp ~]#
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@dlp ~]#
systemctl restart rsyslog

[4] Change httpd settings on Backends to logging X-Forwarded-For header.
[root@www ~]#
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@www ~]#
systemctl restart httpd

[5] Make sure all works fine to access to the frontend server from a Client with HTTP like follows.
 
Tweet