May 14

HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments. Indeed, it can : – route HTTP requests depending on statically assigned cookies ; – spread the load among several servers while assuring server persistence through the use of HTTP cookies ; – switch to backup servers in the event a main one fails ; – accept connections to special ports dedicated to service monitoring ; – stop accepting connections without breaking existing ones ; – add/modify/delete HTTP headers both ways ; – block requests matching a particular pattern ;

Install:
1.download haproxy.
wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.18.tar.gz
You can get the last version from the website

2.unzip haproxy
tar zxvf  haproxy-1.3.18.tar.gz

3.compile
make TARGET=linux26
More detail you can look for the file README of the soure files.
mv haproxy-1.3.18 /usr/local/haproxy

4.create config file
vi /usr/local/haproxy/proxy.cfg
global
        uid 99
        gid 99
        maxconn 48000
        chroot  /usr/local/haproxy
        daemon
        quiet
        nbproc  2
        #pidfile /var/run/haproxy-private.pid

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        log 127.0.0.1 local3
        retries 3
        option  redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen http-proxy :80
        mode http
        stats uri /haproxy-stats
        stats realm Haproxy statistics
        stats auth monitor:StatusM5776
        balance roundrobin
        #balance source
        cookie PHPSESSID prefix
        option httpclose
        option forwardfor
        option httpchk HEAD /adima/logo.gif HTTP/1.0
        appsession PHPSESSID len 32 timeout 86400000
        server ht1 192.168.10.1:80 cookie ht1 check inter 3000 rise 2 fall 3
        server ht2 192.168.10.2:80 cookie ht2 check inter 3000 rise 2 fall 3
        server ht3 192.168.10.3:80 cookie ht3 check inter 3000 rise 2 fall 3

5.start haproxy shell

#!/bin/sh
# chkconfig: 2345 80 32
# description: haproxy is a Web Agent, which is the program

ulimit -SHn 51200
MPRO="/usr/local/haproxy/haproxy"
DCFG="/usr/local/haproxy/proxy.cfg"

[ -x ${MPRO} ] || exit 0

RETVAL=0

start () {
        echo -n "Starting haproxy: "
        ${MPRO} -f $DCFG
        RETVAL=$?
       [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy
        echo
       return $RETVAL
}

stop() {
       # Stop daemons.
       echo -n "Shutting down haproxy: "
       killall haproxy
       RETVAL=$?
       [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy
       echo
       return $RETVAL
}

restart() {
        stop
        sleep 1
        start
}

case "$1" in
start)

       start
       ;;
stop)
       stop
       ;;
restart)
       restart
       ;;
*)
       echo "Usage: haproxy {start|stop|restart}"
       exit 1
esac

exit $?

One Response to “Load balancing using HAProxy”

Leave a Reply

preload preload preload