Setting up a HTTP load balancer

HAProxy is a TCP load balancing application. It can be used to loadbalance TCP connections (i.e., email(smtp etc)) – not just webservers. Very conveniently, however, HAProxy does in fact have it’s own mode for HTTP specific applications. In this guide, we will setup HAProxy infront of 2 fictional Apache webservers with the HAProxy server running CentOS. If one server goes offline, we will begin serving connections to the other server in a roundrobin fashion.

Important thing to note is all requests will come from the HAProxy server! Awesomely enough, however, we will include the X-Forwarded-For value of the clients IP in our packets. This means you can install mod_rpaf on your Apache webserver, or similar, and log the correct IPs.

Lets start.

Login to your Linux server, and install HAProxy.

wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.23.tar.gz
tar zxvf haproxy-1.3.23.tar.gz
cd haproxy-1.3.23
make
cp haproxy /usr/sbin/haproxy

Now, grab an init file for HAProxy so we can easily start/stop HAProxy. The good guys over at Rack911 have provided a download for you to use.

wget http://layer1.rack911.com/haproxy/haproxy.init -O /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy

Below is what will be your /etc/haproxy.cfg – First thing we want to do is set the maximum connections HAProxy will handle simultaneously. Good thing to note is connections over this limit are queued, and not disregarded. We will set this under the global block. We will also configure the daemon and nbproc settings.

global
maxconn     4096 # Total max connections. Adjust as needed.
daemon      # We are going to run HAProxy as a daemon. :)
nbproc      2 # This HAProxy server is an Atom, so it has 2 cores visible. Set to number of processing cores you have #available.

#Next we’re going to set some other HAProxy settings under the defaults block.

defaults
mode http # Run HAProxy in HTTP mode. HAProxy can do a lot more than HTTP!
clitimeout 60000 # Wait 60,000ms or 60s for client to time out on us.
srvtimeout 20000 # We will only wait 30 seconds for server to reply to us.
contimeout 5000 # Server has 4 seconds to answer us initually.
option httpclose # Disable Keepalive. Check your webservers KeepAlive value. Default to off in Apache2.

#Finally, the below is the last piece of meat we need. Remember to use an IP not bound to port 80!

listen  http_proxy ip:80 # Place your selected IP in the ip spot. IP and port number separated by colon.
balance roundrobin # Load Balancing algorithm
option httpchk # We want to see which servers aren’t working for us. Can also specify URIs for this.
option forwardfor # This sets X-Forwarded-For. Remember to load mod_rpaf or similar to gain anything from this.
## Define your servers to balance
server web1 ip:80 weight 1 maxconn 512 check # 512 max connections.
server web2 ip:80 weight 1 maxconn 512 check # 512 max connections on this server.

We should now be able to start HAProxy. You put the above in /etc/haproxy.cfg – right?

service haproxy start

Lets add HAProxy to start on boot.

chkconfig add haproxy

You should now have a simple configured load balancer. When you visit the selected IP in a web browser, you should be bounced in a roundrobin fashion from web1-web2.

We’d like to thank Rack911 for their assistance with this tutorial.

Comments

Comments are closed.