HAProxy As HTTP Load Balancing

Installation HAProxy and configure Load Balancing Server.
This example based on the environment:
10.59.60.30 10.59.60.31 10.59.60.32
|
| Frontend | | Web Server | | Web Server |
| HAProxy |     | 1 |                   | 2 |
Install HAProxy.
yum -y install haproxy

Configure HAProxy.

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.org
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 web1 10.59.60.31:80 check
server web2 10.59.60.32:80 check

service haproxy start
Starting haproxy: [ OK ]
chkconfig haproxy on

Configure Rsyslog to get logs for HAProxy.

vi /etc/rsyslog.conf
line 13,14: uncomment, line 15: add
$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP, 127.0.0.1
line 42: change like follows
.info;mail.none;authpriv.none;cron.none,local2.none /var/log/messages
local2.
/var/log/haproxy.log
service rsyslog restart
Change httpd settings on Backends to logging X-Forwarded-For header.
vi /etc/httpd/conf/httpd.conf
line 497: change like follows
LogFormat “\”%{X-Forwarded-For}i\” %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
service httpd restart
 

Be the first to comment

Leave a Reply

Your email address will not be published.


*