Setting Up A High-Availability Load Balancer (With Failover and Session Support) With Perlbal/Heartbeat On Debian Etch

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Sun, 2009-01-11 19:32. :: Debian | High-Availability

Setting Up A High-Availability Load Balancer (With Failover and Session Support) With Perlbal/Heartbeat On Debian Etch

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 12/29/2008

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.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Wed, 2009-06-17 04:05.
shared IP=192.168.0.99 Load Balancer = 192.168.0.100 192.168.0.101 I have only 1 public IP address. How can I set shared IP in different network address of Load Balancer's IP ?
Submitted by Anonymous (not registered) on Sun, 2009-01-11 21:27.
What happens when the load balancer fails?  Then you have no service at all...
Submitted by admin (registered user) on Mon, 2009-01-12 00:03.
That's why I'm using two load balancers in a failover configuration...
Submitted by Todd (not registered) on Wed, 2009-02-25 19:49.

For a full setup instructions for Kemp's load balancer, check this "manual" out:

http://kemptechnologies.com/pr/reviews/08-20-08-lbdigest.com.shtml?utm_source=howtoforge&utm_medium=pv&utm_campaign=setup