Monday, September 13, 2010

Load Balancing Web Servers using ldirectord

Web Server Load Balancing


Using ldirectord to set up load balancing in apache, using debian linux as my distro, should also work on ubuntu or any other distro for that matter (bsd, red hat, etc). See below for links to the documentation. This setup is done using ldirectord's masq mode, which is basically ip masquerading function using iptables.

Let's Begin

Im just going to take a few mins to explain my load balancing set up. What we have is 1 router, the default gateway, connected to the internet, and also connecting to an internal network. Standard set up really.

What we want to do is set up load balancing with 2 (or more, our case we have 4) apache servers.

First step - download ldirectord (ldirectord) is a daemon that will route traffic to a certain host based on whatever load balancing algorithm you choose. Some examples of algorithms are round robin, where it selects the next host every time, or least connections, route to the host with the least number of open connections. There are a few other options that are covered in the ldirectord documentation (man page) and specifically for the algorithms is in the IPVSADM man page under -s (scheduler) located here (

Note that ldirectord is now a sub project of linux HA project called Heartbeat. It is available as a part of that package available here

The reason for this is ldirectord is used as a part of that package. It is also useable on its own as I will show you.

Ldirectord set up

What we have to do next is set up ldirectord after we manage to get it compiled. Here is an example of a /etc/ha.d/

# Global Directives

# Virtual Server for HTTP
real= masq
real= masq
real= masq
real= masq
receive="Test Page"

As you can see, we have set this ldirectord program up on the host located at which is a physical machine. Using Heartbeat you can set up a virtual IP address and have 2 or more machines take over that address when the main one goes down, but that is for another article. We are strictly focusing on setting up ldirectord here.

Setting up the load balanced machines

On each of the load balanced machines (, 22, 23, and 12) we need to set the default gateway to (the ip address of the load balancer) Please adjust accordingly to your network. This can be done using the following command on debian linux and most other types

route add default gw

Next we have set up our ldirectord to check each machine by loading the page "ldirectord.html" shown above. So really it will be trying to access the page We have set up a regular expression as well above saying "Test Page". As long as this url returns this string somewhere, the host will be considered "up" by ldirectord. After we have set everything up, it should be good to go.

Running ldirectord

Please make sure your config file is in /etc/ha.d/ because the script will be searching there for it. Next run ldirectord by typing

/usr/bin/perl /usr/sbin/ldirectord start

You should make a init.d script for this. After you have run ldirectord, typing

ipvsadm -L

should give you some information on its status.

s5:/etc/ha.d# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP wlc
-> Masq 1 6 2245
-> Masq 1 5 2275
-> Masq 1 4 2563
-> Masq 1 6 2074

Here it shows that all hosts are up (their "Weight" is each 1) and have active connections running to them.

Setting up external routing

Now just have your router point all web traffic to and you should have full load balancing. Note there are other problems such as how are you going to make sure they are all synchronized. You can use tools such as rsync, or use NFS to mount a share that contains all your files on each of the slave servers (our approach). The NFS approach seems to work nicely, however if you are hosting webservers for file transfers it may become a bottleneck so make sure your internet network is fast (1000 mbit) and tweaking NFS settings to your liking.

Thank you, your comments are welcome!

1 comment: