Tuesday, November 26, 2013

Quick HAProxy install and TCP load balancing on Debian

Had to test modified echo server and one of requirements was to create cluster. Wanted simple round robin with three servers at backend. After trying balance from repositories I find out that it doesn’t do well on new distros, used to work nicely on Ubuntu 8.04. There was significant loss of packages. So, after some googling decided to go with HAProxy. Only problem is that HAProxy doesn’t have quick start or I couldn’t find it. So here is quick start for TCP load balancer.
Downloaded source from HAProxy website. Installed requirements:

# apt-get install build-essential zlib1g-dev libpcre3-dev libssl-dev

Unpacked sorce and cd to root of sorce directory. Make is for many targets and many options, Debian Wheezy with installed libs would be:

$ make TARGET=linux2628 CPU=native USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1


When build is successfully executed we can install it:

# make install

Then I created config file called hapconfig with following content:

global
    daemon
    user tcpstuff
    group tcpstuff
    chroot /home/tcpstuff
    maxconn 1024

defaults
    mode tcp
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend tcp-in
    bind *:7000
    default_backend servers

backend servers
    balance roundrobin 
    server s1 127.0.0.1:7001
    server s2 127.0.0.1:7002
    server s3 127.0.0.1:7003


And now to run HAProxy:

# /usr/local/sbin/haproxy -f /home/tcpstuff/work/haproxy-1.4.24/hapconfig


It daemonizes itself, as requested in config file, and goes into background, to verify it is up we can execute:

ps aux | grep haproxy


To test echo server cluster I used:

$ echo ‘Hello world!’ | nc -q 1 192.168.1.101 7000

Why -q 1? If we omit it on Debian it will default to waiting for timeout to expire, on Linux Mint we can skip -q. And that was really quick.