HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for very high traffic web sites and powers quite a number of the world’s most visited ones. Over the years it has become the de-facto standard opensource load balancer, is now shipped with most mainstream Linux distributions, and is often deployed by default in cloud platforms
Prerequisites
Make sure you have installed webserver and working fine
Install HAProxy on Debian
create a new file called “backports.list” in /etc/apt/sources.list.d
#cd /etc/apt/sources.list.d
#vi backports.list
Add the following lines
deb http://cdn.debian.net/debian wheezybackports main
Save and exit the file
Update the source list and install HAProxy
# apt get update
# apt get install haproxy
Configure HAProxy
In this tutorial, we assume that there are two HTTP web servers up and running with IP addresses 172.30.40.2 and 172.30.40.3. We also assume that the load balancer will be configured at a server with IP address 172.30.40.4.haproxy configuration file located at /etc/haproxy/haproxy.cfg.
First we need to backup the the existing config
# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.orig
# cat /dev/null > /etc/haproxy/haproxy.cfg
Add the following configuration
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
user haproxy
group haproxydefaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000listen webfarm 172.30.40.4:80
mode http
stats enable
stats uri /haproxy?stats
stats realm Haproxy\ Statistics
stats auth haproxy:stats
balance roundrobin
cookie LBN insert indirect nocache
option httpclose
option forwardfor
server webA 172.30.40.2:80 cookie node1 check
server webB 172.30.40.3:80 cookie node2 check
and now we need to ENABLE the haproxy in /etc/default/haproxy
# vi /etc/default/haproxy
and set ENABLE=1
to start haproxy just run:
# service haproxy start
HAproxy Statistics
In this configuration we enable the statistics of haproxy, you can acsses from you browser by enter
http://172.30.40.4/haproxy?stats
The user and the password is like you type in the configuration in “stats auth haproxy:stats”
Note that “deb http://cdn.debian.net/debian wheezybackports main” should be “deb http://cdn.debian.net/debian wheezy-backports main” otherwise it will fail when you update the repository list.