Mar 03 2008

Session Based Load Balancing with HAproxy

Published by Dave at 8:08 pm under Deploying, Django

A friend is asking how to install HAproxy because I’d just done it three weeks ago, so I’m publishing my install notes, most of which I stole from another friend.

HAproxy is a pretty sweet tool but currently has little written about it so installation and configuration takes reading a large text manual. Here though is a basic setup that should work for most cases.

mkdir /opt/haproxy
cd /opt/haproxy

#Download latest version from http://haproxy.1wt.eu/download/1.3/bin/ e.g.
wget http://haproxy.1wt.eu/download/1.3/bin/haproxy-1.3.14.2-pcre-40kses-splice-linux-i586.notstripped.gz -O haproxy.gz

gunzip haproxy.gz
chmod 700 haproxy

Now you have HA proxy installed but need a config file. Make a new file called /etc/haproxy.cfg and add the following contents.

global
log 127.0.0.1 daemon debug
maxconn 4096

pidfile /var/run/haproxy.pid
daemon
defaults
log global
mode http

option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webfarm <HAproxy host ip>:80
mode http
cookie MYSITECOOKIE insert nocache
balance roundrobin
server app1 <server1 ip>:8080 cookie app1inst1 check
server app2 <server2 ip>:8080 cookie app2inst2 check
server app3 <server3 ip>:8080 cookie app3inst3 check

This config file sets up session based balancing; meaning HAproxy will inject its own cookie into each person who visits the load balancer. Then every requests the person with that cookie makes from that point on will get directed to the same server! This is most important for apps with content heavily based around user information. Without it its really tough to make use of local caching.

Make sure you change all of the parameters inside <> brackets. You can add or remove servers by editing the “server” commands. I think you can see the trend there :). Also you’ll want to change the MYSITECOOKIE, which is the name of the cookie that HAproxy will inject and the variables app* and app*inst* are made up. You can call them whatever you want.

You should also notice that in this setup HAproxy is listening to port 80 ( <HAproxy host ip>:80 ) and that its redirecting to port 8080 on the re-direct servers (<server1 ip>:8080 ). This is because often one of the re-direct servers is usually the same as the HAproxy host. Also its not a good idea to have random IP’s that aren’t directly linked to your domain to be hosting your app on port 80.

So, next we have to change your app instance to listen to port 8080. I’ve only done this with Apache2 and will only go through those steps. Its very simple and is probably as simple with other servers. You need to edit your ports.conf (often found in /etc/apache2/ports.conf ) and add, or change the following line at the top.

#ports.conf
Listen 8080

Save it, restart Apache2, and that’s it.

Lastly here are the directions for running the proxy. You could bundle these in a script but I use them so rarely I didn’t. There is one command for kicking off the proxy, another for verifying a new configuration file if you make an update (for when you add or remove a server) and finally a third that will dynamically reload your new configuration.

First time start:
/opt/haproxy/haproxy -f /etc/haproxy.cfg -p /var/run/haproxy.pid

Verify a valid configuration:
/opt/haproxy/haproxy -f /etc/haproxy.cfg -c

Dynamically reload configuration:
/bin/sh -c “/opt/haproxy/haproxy -f /etc/haproxy.cfg -p /var/run/haproxy.pid -st $(</var/run/haproxy.pid)”

I’m no expert. Like I said I got most of this from my friend Justin (Thanks Justin!). But it was a pain sifting through the manual and I thought others might benefit from these notes. For more information visit the HAproxy docs.

Note: Interestingly this post by Justin.tv promoting Nginx over Pound just made it to the top of Hacker News as I was finishing. I want to add my recommendation of HAproxy. I also tried Nginx but ended up going with HAproxy. There is certainly more articles on using Nginx but it is more difficult to set up session based balancing.

4 Responses to “Session Based Load Balancing with HAproxy”

  1. Nick Grandyon 03 Mar 2008 at 9:48 pm

    dave, thanks a ton - big help to have some guidance for standard config. i’ll let you know how it goes!

  2. Ilya Grigorikon 04 May 2008 at 11:53 am

    Thanks for sharing these Dave, very helpful!

  3. […] load balancer such as HAProxy or nginx.   With notes from my friend Justin I wrote a cookbook for setting up HAProxy previously. 8.    Images taken from a Small instance cannot be loaded on L or XL instance, and […]

  4. Brian Guptaon 18 Jun 2008 at 1:03 pm

    We use nginx in front of haproxy for a couple of reasons.
    1) SSL proxy
    2) We can catch certain URLs and redirect/rewrite them before they get passed to haproxy (eg: We can directly serve images or cached html out of nginx and only send dynamic content to the load balanced appservers.)

Trackback URI | Comments RSS

Leave a Reply