Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

How to install and configure HAProxy as an HTTP load balancer

Michel Nadeau, 03-26-2009
HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for web sites crawling under very high loads while needing persistence or Layer7 processing. Supporting tens of thousands of connections is clearly realistic with todays hardware. Its mode of operation makes its integration into existing architectures very easy and riskless, while still offering the possibility not to expose fragile web servers to the Net.

This post will explain how to install HAProxy on FreeBSD and how to configure it as a simple HTTP load balancer.

1. Getting HAproxy

The first step is to download HAProxy. You can find the latest version at this address:

http://haproxy.1wt.eu/#down

As of writing this post, the latest version is 1.3.16, which you can find here:

http://haproxy.1wt.eu/download/1.3/s...-1.3.16.tar.gz

2. Prepare for installation

Log on your FreeBSD machine and change to the temporary directory of your choice. You can now issue the easy following commands:

$ wget http://haproxy.1wt.eu/download/1.3/s...-1.3.16.tar.gz
$ tar xvfz haproxy-1.3.16.tar.gz
$ cd haproxy-1.3.16

HAProxy is now downloaded, unpacked and ready to be compiled.

3. Compile HAProxy

If you are running FreeBSD, you can simply issue the following command:

$ make -f Makefile.bsd REGEX=pcre DEBUG= COPTS.generic="-Os -fomit-frame-pointer"

If you are running another version of Linux, please refer to the README file (included with HAProxy) to find the good way to compile HAProxy on your system.

4. Install HAProxy

Once HAProxy is compiled, you will find a light-weight, standalone "haproxy" executable.

You can copy it where you want, it's your choice! Something like this can be a good choice:

$ cp haproxy /usr/local/sbin

Why? Simply because /usr/local/sbin is usually in $PATH, so haproxy will then be easy to run simply by typing "haproxy".

5. Configuring HAProxy

The configuration of HAProxy, like its installation, is really easy. In fact, it can be very easy, or very complex. HAProxy is VERY flexible and it has literally thousands of parameters you can tweak. You can find the whole documentation on the HAProxy official Web site there:

http://haproxy.1wt.eu/#docs

As of writing this post, the latest official documentation is there:

http://haproxy.1wt.eu/download/1.3/d...figuration.txt

For this post, we'll keep it simple and configure a simple load balancer.

You can put the HAProxy configuration file where you wish, and name it as you wish! This post will be using this path/filename:

/etc/haproxy.conf

And here's our simple configuration file:


global
     
maxconn 4096
      pidfile
/var/run/haproxy.pid
      daemon

defaults
      mode http
      retries 3
      option redispatch
      maxconn 2000
      contimeout 5000
      clitimeout 50000
      srvtimeout 50000

listen GALAXY aaa
.bbb.ccc.ddd:80
      mode http
      cookie GALAXY insert
      balance roundrobin
      option httpclose
      option forwardfor
      stats enable
      stats auth myuser
:mypass
      server EARTH 192.168.0.2
:8080 cookie GALAXY_SERVER_01 check
      server MOON 192.168.0.3
:8080 cookie GALAXY_SERVER_02 check

> aaa.bbb.ccc.ddd should be the public IP of your server.
> 192.168.0.2 should be the LAN IP to your server 1.
> 192.168.0.3 should be the LAN IP to your server 2.

Everything that is in CAPS is customizable:

* GALAXY
* EARTH
* MOON
* GALAXY_SERVER_01
* GALAXY_SERVER_02

Basically, this configuration file is defining a group ("listen" block) called GALAXY, which contains 2 servers: EARTH and MOON. The "roundrobin" balance option is telling to HAProxy to alternate between the 2 servers all the time. For the other options in the global, defaults and listen blocks, they're pretty standard. If you need to tweak those or to add options to suit your needs, I'm sure HAProxy can do it! Simply refer to the whole documentation.

Cookie

When the user will reach the GALAXY group (using http://aaa.bbb.ccc.ddd), the cookie GALAXY will be created and the server ID specified for "cookie" in the servers definitions will be stored in it (GALAXY_SERVER_01 or GALAXY_SERVER_02).

Then, for the whole session, HAProxy will read the cookie and force the use of the server stored in it.

This behavior is controlled by the "cookie GALAXY insert" line and the "cookie GALAXY_SERVER_XX" parts in the GALAXY group block. If you don't want this feature, simply remove or comment (using a "#") these parts.

Stats

There's a pretty complete load balancer stats page built-in HAProxy. You can reach it there:

http://aaa.bbb.ccc.ddd/haproxy?stats

With the configuration above, the username will be "myuser" and the password "mypass". If you don't want the stats to be enabled, you can simply remove or comment these lines:

stats enable
stats auth myuser:mypass

Nginx

In the configuration above, we used the port 8080 for the local/private Web servers. If these Web servers are running Nginx, you will need to include the following configuration in your http, server or location block (nginx.conf):

port_in_redirect off;

This is needed because if you try to reach this:

http://aaa.bbb.ccc.ddd/test (NO ending slash)

Then Nginx will try to redirect you there:

http://aaa.bbb.ccc.ddd:8080/test/

Which will obviously not work. So if you're running Nginx and that you decided to use a port different of 80, you will need this setting.

6. Starting HAProxy

To start HAProxy, simply issue the following command:

haproxy -f /etc/haproxy.conf

For the initial testing, you might want to disable the cookie in the configuration file and test your Web page to actually see that you're switching from a server to another.

Conclusion

That's it! You've got a high-performance, high-scalability and highly-tweakable load balancer configured in a couple of minutes!

Mike Peters, 07-17-2009
One great things about HAProxy is that it supports several load-balancing algorithms:

* Simple round robin (traffic divided equally)
* Least connections (server with least connections gets the request)

and our favorite -

* Source (The source IP address is hashed and divided by the total weight of the running servers to designate which server will receive the request)

The source algorithm ensures that the same client IP address will always reach the same server as long as no server goes down or up. Very important when using HAProxy to load balance databases and avoid replication latencies.

Patrick, 11-04-2009
Nice and sweet ...
Thanks for this mini howto !
I use it and I have a 3 web node load balancing running as expected :-)
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions