Setting Up A High-Availability Load Balancer (With Failover and Session Support) With Perlbal/Heartbeat On Debian Etch
Version 1.0
Author: Falko Timme
This article explains how to set up a two-node load balancer in an active/passive configuration with Perlbal and heartbeat on Debian Etch. The load balancer sits between the user and two (or more) backend Apache web servers that hold the same content. Not only does the load balancer distribute the requests to the two backend Apache servers, it also checks the health of the backend servers. If one of them is down, all requests will automatically be redirected to the remaining backend server. In addition to that, the two load balancer nodes monitor each other using heartbeat, and if the master fails, the slave becomes the master, which means the users will not notice any disruption of the service. Perlbal is session-aware, which means you can use it with any web application that makes use of sessions (such as forums, shopping carts, etc.).
I do not issue any guarantee that this will work for you!
1 Preliminary Note
In this tutorial I will use the following hosts:
- Load Balancer 1: lb1.example.com, IP address: 192.168.0.100
- Load Balancer 2: lb2.example.com, IP address: 192.168.0.101
- Web Server 1: http1.example.com, IP address: 192.168.0.102
- Web Server 2: http2.example.com, IP address: 192.168.0.103
- We also need a virtual IP address that floats between lb1 and lb2: 192.168.0.99
Here's a little diagram that shows our setup:
shared IP=192.168.0.99
192.168.0.100 192.168.0.101 192.168.0.102 192.168.0.103
-------+------------+--------------+-----------+----------
| | | |
+--+--+ +--+--+ +----+----+ +----+----+
| lb1 | | lb2 | | http1 | | http2 |
+-----+ +-----+ +---------+ +---------+
Perlbal Perlbal 2 web servers (Apache)
heartbeat heartbeat
The shared (virtual) IP address is no problem as long as you're in your own LAN where you can assign IP addresses as you like. However, if you want to use this setup with public IP addresses, you need to find a hoster where you can rent two servers (the load balancer nodes) in the same subnet; you can then use a free IP address in this subnet for the virtual IP address.
http1 and http2 are standard Debian Etch Apache setups with the document root /var/www (the configuration of this default vhost is stored in /etc/apache2/sites-available/default). If your document root differs, you might have to adjust this guide a bit.
2 Preparing The Backend Web Servers
We will configure Perlbal as a transparent proxy, i.e., it will pass on the original user's IP address in a field called X-Forwarded-For to the backend web servers. Of course, the backend web servers should log the original user's IP address in their access logs instead of the IP addresses of our load balancers. Therefore we must modify the LogFormat line in /etc/apache2/apache2.conf and replace %h with %{X-Forwarded-For}i:
http1/http2:
vi /etc/apache2/apache2.conf
[...] #LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined [...] |
Afterwards we restart Apache:
/etc/init.d/apache2 restart
We are finished already with the backend servers; the rest of the configuration happens on the two load balancer nodes.
3 Installing Perlbal
Perlbal is not available as a package for Debian Etch, but we can install it through the Perl shell. Before we do this, we install a few prerequisites:
lb1/lb2:
apt-get install build-essential unzip lynx ncftp perl
Afterwards we invoke the Perl shell as follows:
perl -MCPAN -e shell
On the Perl shell, we run the following three commands to install Perlbal:
force install HTTP::Date
install IO::AIO
force install Perlbal
Type
q
to leave the Perl shell.
4 Configuring The Load Balancers
Perlbal expects its configuration in the file /etc/perlbal/perlbal.conf which we create as follows:
lb1/lb2:
mkdir /etc/perlbal
vi /etc/perlbal/perlbal.conf
CREATE POOL webfarm POOL webfarm ADD 192.168.0.102:80 POOL webfarm ADD 192.168.0.103:80 CREATE SERVICE balancer SET listen = 192.168.0.99:80 SET role = reverse_proxy SET pool = webfarm SET persist_client = on SET persist_backend = on SET verify_backend = on ENABLE balancer |
You won't find much documentation about the Perlbal configuration on the Internet, so the best way to learn about the Perlbal configuration options is to download the latest Perlbal release from http://code.google.com/p/perlbal/downloads/list (e.g. http://perlbal.googlecode.com/files/Perlbal-1.70.tar.gz). Uncompress it and then take a look at the files in the conf/ and doc/ subdirectories. You will find some configuration examples and a list of configuration options there.