Sunday, 9 January 2011

A basic HAProxy config for load balancing HTTP traffic

HAProxy is an ultra fast, reliable TCP/HTTP load balancer developed by Willy Tarreau. The following post outlines a brief example on how to load balance two web servers. My distro of choice for this example was Ubuntu 10.10 running as a virtual machine in Amazon EC2.

The installation of HAProxy on Ubuntu 10.10 was very straightforward with apt-get and is as follows:
sudo apt-get install haproxy


This installs version 1.4.8 of HAProxy and is almost immediately ready for use.

First, let's move the current config over to a backup file:
mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak


Then, create a new config file with VI or whatever your favorite text editor might be:
vi /etc/haproxy/haproxy.cfg


This text can be pasted right into your text editor window and adjusted to fit your needs.

global
#Run as a daemon
daemon
 
#Fix the maximum number of concurrent connections on a frontend.
maxconn 4096

#User and group to run under
user haproxy
group haproxy

#Enable per-instance logging of events and traffic.
log 127.0.0.1   local0
log 127.0.0.1   local1 notice

defaults
 #connection, client and server timeouts
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option redispatch
option dontlognull
retries 3

#IP address of your HAProxy server and 
#the port it will be listening on
frontend http-in 10.12.24.39:80

 #Set the running mode or protocol of the instance.
mode http

#Specify the backend to use when no "use_backend" rule has been matched.
default_backend http

#
backend http

#Set the running mode or protocol of the instance.
mode http
 
#Enable stats
stats enable
        
#Enable statistics and define the URI prefix to access them.
stats uri     /admin?stats

#Enable statistics with authentication and grant access to an account.
stats auth admin:[email protected]!n

#Enable cookie-based persistence in a backend.
cookie SERVERID insert indirect

#Enable insertion of the X-Forwarded-For header to requests sent to servers.
option forwardfor

#Enable or disable passive HTTP connection closing.
option httpclose

#Enable HTTP protocol to check on the servers health.
option httpchk GET /keepalive.php HTTP/1.0
 
#Define the load balancing algorithm to be used in a backend.
balance leastconn
        
 #Declare your backend servers
server web1 10.95.16.80:80 cookie w1 weight 1 check inter 10000
server web2 10.95.19.79:80 cookie w2 weight 1 check inter 11000

Once you have made the necessary adjustments to the IP addresses and any other tweaks you see fit, you can test your new config by entering this command:

haproxy -c -f haproxy.cfg

If everything is good you will see:
Configuration file is valid

If not, HAProxy will show you the lines and errors within your config.

When you are ready to pull the trigger you can launch HAProxy:
haproxy -D -f haproxy.cfg
For a deeper look into just how powerful HAProxy can be, I reccomend you check out the latest (at this time) HAProxy Configuration Manual. Or a quick, searchable command reference guide here.

No comments:

Post a Comment