How To Set Up A Loadbalanced High-Availability Apache Cluster Based On Ubuntu 8.04 LTS
Author: Mohamed Ghaleb <Mohamed_Ghaleb [at] msn [dot] com> (English and German only please)
Last edited 06/03/2008
This tutorial shows how to set up a two-node Apache web server cluster that provides high-availability. In front of the Apache cluster we create a load balancer that splits up incoming requests between the two Apache nodes. Because we do not want the load balancer to become another "Single Point Of Failure", we must provide high-availability for the load balancer, too. Therefore our load balancer will in fact consist out of two load balancer nodes that monitor each other using heartbeat, and if one load balancer fails, the other takes over silently.
The advantage of using a load balancer compared to using round robin DNS is that it takes care of the load on the web server nodes and tries to direct requests to the node with less load, and it also takes care of connections/sessions. Many web applications (e.g. forum software, shopping carts, etc.) make use of sessions, and if you are in a session on Apache node 1, you would lose that session if suddenly node 2 served your requests. In addition to that, if one of the Apache nodes goes down, the load balancer realizes that and directs all incoming requests to the remaining node which would not be possible with round robin DNS.
For this setup, we need four nodes (two Apache nodes and two load balancer nodes) and five IP addresses: one for each node and one virtual IP address that will be shared by the load balancer nodes and used for incoming HTTP requests.
I will use the following setup here:
- Apache node 1: webserver1.tm.local (webserver1) - IP address: 192.168.0.103; Apache document root: /var/www
- Apache node 2: webserver2.tm.local (webserver2) - IP address: 192.168.0.104; Apache document root: /var/www
- Load Balancer node 1: loadb1.tm.local (loadb1) - IP address: 192.168.0.101
- Load Balancer node 2: loadb2.tm.local (loadb2) - IP address: 192.168.0.102
- Virtual IP Address: 192.168.0.105 (used for incoming requests)
Have a look at the drawing on http://www.linuxvirtualserver.org/docs/ha/ultramonkey.html to understand how this setup looks like.
In this tutorial I will use Ubuntu 8.04 LTS for all four nodes, just install basic Ubuntu 8.04 LTS on all four nodes.
I want to say first that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!
I also recommend you to have a DNS server in place.
Step 1 to 6 should be done on all four servers.
1 Enable The root Account
sudo passwd root
and give root a password. Afterwards we become root by running
2 Install The SSH Server
If you did not install the OpenSSH server during the system installation, you can do it now:
apt-get install ssh openssh-server
From now on you can use an SSH client such as PuTTY and connect from your workstation to your Ubuntu 8.04 LTS server and follow the remaining steps from this tutorial.
3 Install vim-full
I'll use vi as my text editor in this tutorial. The default vi program has some strange behavior on Ubuntu and Debian; to fix this, we install vim-full:
apt-get install vim-full
(You don't have to do this if you use a different text editor such as joe or nano.)
4 Configure The Network
Because the Ubuntu installer has configured our system to get its network settings via DHCP, we have to change that now because a server should have a static IP address. Edit /etc/network/interfaces and adjust it to your needs (in this example setup I will use the IP address 192.168.0.101):
# This file describes the network interfaces available on your system
Please make sure your network configuration are set correctly, feel free to change that based on your network configuration.
Then restart your network:
Then edit /etc/hosts. Make it look like this:
127.0.0.1 localhost.localdomain localhost
echo loadb1.tm.local > /etc/hostname
Both should show loadb1.tm.local now.
If you have a DNS server in place (recommended) make sure the 4 servers configured to use it, if you don't have a DNS click here