Low Orbit Flux Logo 2 F

HAProxy

HAProxy is a free, software based load balancer. If you want the best, free, software based solution, this is it. We’re going to assume that you are running Ubuntu Linux. If you are using another platform, like RHEL, the instructions are still very similar.

To get started, you are going to want to install the HAProxy package:


sudo apt-get install haproxy

Next


vi /etc/default/haproxy
ENABLED=1
service haproxy

Backup the config and then edit it:


mv /etc/haproxy/haproxy.cfg{,.original}
vi /etc/haproxy/haproxy.cfg

Setup the file to look like this:



global
    log 127.0.0.1 local0 notice
    maxconn 2000
    user haproxy
    group haproxy

Defaults:

    log     global
    mode    http
    option  httplog
    option  dontlognull
    retries 3
    option redispatch
    timeout connect  5000
    timeout client  10000
    timeout server  10000

listen appname 0.0.0.0:80
    mode http
    stats enable
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth A_Username:YourPassword
    stats auth Another_User:passwd
    balance roundrobin
    option httpclose
    option forwardfor
    server lamp1 10.0.0.1:80 check
    server lamp2 10.0.0.2:80 check

http://1.1.1.1/haproxy?stats

balance - you can set different algorithms:


server <name> <address>[:port] [param*]
server lamp2 10.0.0.2:80 check

service haproxy start

Test File:


/var/www/file.php

<?php
header('Content-Type: text/plain');
echo "Server IP: ".$_SERVER['SERVER_ADDR'];
echo "\nClient IP: ".$_SERVER['REMOTE_ADDR'];
echo "\nX-Forwarded-for: ".$_SERVER['HTTP_X_FORWARDED_FOR'];
?>

Watch it alternate:


curl http://1.1.1.1/file.php
curl http://1.1.1.1/file.php
curl http://1.1.1.1/file.php

Watch it fail and only be redirected to one server:


service apache2 stop

curl http://1.1.1.1/file.php
curl http://1.1.1.1/file.php
curl http://1.1.1.1/file.php

Session Stickiness

Session stickiness is important if you actually have user login sessions.


/var/www/session.php

<?php
header('Content-Type: text/plain');
session_start();
if(!isset($_SESSION['visit']))
{
        echo "This is the first time you're visiting this server";
        $_SESSION['visit'] = 0;
}
else
        echo "Your number of visits: ".$_SESSION['visit'];

$_SESSION['visit']++;

echo "\nServer IP: ".$_SERVER['SERVER_ADDR'];
echo "\nClient IP: ".$_SERVER['REMOTE_ADDR'];
echo "\nX-Forwarded-for: ".$_SERVER['HTTP_X_FORWARDED_FOR']."\n";
print_r($_COOKIE);
?>

Cookie named SRVNAME with value S1 or S2 ( different from the PHPSESSID cookie which will also be set )


   cookie SRVNAME insert
   server lamp1 10.0.0.1:80 cookie S1 check
   server lamp2 10.0.0.2:80 cookie S2 check

service haproxy restart

Initial request:


curl -i http://1.1.1.1/session.php

Respond back with the cookie:


curl -i http://1.1.1.1/session.php --cookie "PHPSESSID=l9haakejnvnat7jtju64hmuab5;SRVNAME=S1;"
curl -i http://1.1.1.1/session.php --cookie "PHPSESSID=l9haakejnvnat7jtju64hmuab5;SRVNAME=S1;"

Stickiness for all files ….. ( what was the case for the first sticky example? )

If you:

Combine S1 / S2 with existing PHPSESSID


cookie PHPSESSID prefix
server lamp1 10.0.0.1:80 cookie S1 check
server lamp2 10.0.0.2:80 cookie S2 check

Logging


vi /etc/rsyslog.conf

$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514

Haproxy syslog rule:


vi /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy') then -/var/log/haproxy.log

service rsyslog restart

HAProxy Keepalives

In case you want to configure connection keepalives you can use the following options. This might cause performance problems.


option http-server-close
timeout http-keep-alive 3000

Resources

Official Documentation HAProxy Additional Resource 1 HAProxy Additional Resource 2

Haproxy Websocket

A lot of people seem to be interested in Haproxy websockets. We thought it might be a good idea to provide a working example.