Installing and configuring HA Proxy on Ubuntu

Installing and configuring HA Proxy on Ubuntu

Install HA Proxy using folowing command
sudo apt-get install haproxy

Save the original configuration as backup
cp -R /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bkp

And edit the configuration file using your favourite editor
sudo vi /etc/haproxy/haproxy.cfg

maxconn 4096
user haproxy
group haproxy
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
listen webcluster *:80
mode http
stats enable
stats auth us3r:passw0rd
balance roundrobin
option httpchk HEAD / HTTP/1.0
option forwardfor
cookie LSW_WEB insert
option httpclose
server websvr1 10.1.1..10:80 cookie LSW_websvr1 check
server websvr2 10.1.1..11:80 cookie LSW_websvr2 check
server websvr3 10.1.1..12:80 cookie LSW_websvr3 check
server websvr4 10.1.1..13:80 cookie LSW_websvr4 check

startup parameter for HAProxy set enabled as 1
sudo vi /etc/default/haproxy

# Set ENABLED to 1 if you want the init script to start haproxy.

sudo /etc/init.d/haproxy start

For enables HAProxy’s statistics interface which you can access by browsing to your HA proxy’s ip http://haproxy’s IP/haproxy?stats login with the username and password(stats auth us3r:passw0rd)

When you see load balancers ip in apache web servers logs and want to change with origins

Edit apache configuration file
vi/etc/apache2/apache2.conf  and replace logformat
#LogFormat “%h %l %u %t \”%r\” %>s %O \”%{Referer}i\” \”%{User-Agent}i\”” combined
LogFormat “%{X-Forwarded-For}i %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined

And restart your apache
apachectl -k restart

Using with keepalived for real scenario
Up to this stage we have only one proxy server and it was used with round robin DNS,now we are changing one proxy with multiple proxy load balancers.

For that we need to install another package called keepalived
sudo apt-get install keepalived

edit sysctl.conf
sudo vim /etc/sysctl.conf
# Allow HAProxy to bind to external address

and update sysctl using command
sudo sysctl -P

Now create keepalived configuration files on proxies
sudo vim /etc/keepalived/keepalived.conf
vrrp_script chk_haproxy { # Requires keepalived-1.1.13
script “killall -0 haproxy” # cheaper than pidof
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK

vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 101 # 101 on master, 100 on backup
virtual_ipaddress {
track_script {

Start up keepalived
sudo /etc/init.d/keepalived start

Note : Here we can change our web server listening ip with virtual ip( the haproxy.cfg

listen webcluster

And finally stop and start your HA proxy and test it switch over is working
sudo /etc/init.d/haproxy stop
sudo /etc/init.d/haproxy start


Posted on June 28, 2012, in LInux Based, Web. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: