This is a tutorial to configure Apache Web Server Load Balancing in Linux using the mod_proxy_balancer module. So additional software isn’t required. This tutorial is written for Linux but this can also be applied to windows systems running Apache. This setup makes use of four computers. The first computer is the proxy load balancer, the second and third computers are the normal web servers which will respond to the requests and the fourth computer hosts the web content that is to be served by the web servers. The reason for separating the content is to make sure the same content is served by both the web servers. The fourth computer is optional, you can use tools like rsync to keep the content of both web servers synchronized, but that add up to network traffic. The number of web servers can be increased as per requirement.
The entire setup is shown in the diagram below
———————
Load Balancer
———————
10.0.0.1 (eth1)
|
—————————————————————–
| | |
10.0.0.2 10.0.0.3 10.0.0.4
——————- ——————- ———————
Web Server 1 Web Server 2 Storage Server
——————– ——————- ———————-
The settings for each computer is specified below
Load Balancer
eth0 - 192.168.0.2
eth1 - 10.0.0.1
Web Server 1
eth0 - 10.0.0.2
Web Server 2
eth0 - 10.0.0.3
Storage Server
eth0 - 10.0.0.4
The steps for setting up an Apache Load Balancing Cluster is as follows
- Install Apache HTTP server on load balancer, Web Server 1 and Web Server 2
- Configure network settings on all systems
- Configure mod_proxy_balancer in Load Balancer
- Configure NFS on storage server
- Configure firewall rules and SELinux policies on all systems
- Mount the NFS share in the web servers as the DocumentRoot
Install Apache HTTP Server on load balancer, Web Server 1 and Web Server 2
With repositories properly configured using yum install httpd package on the load balancer and Web Servers
yum -y install httpd
Make sure the service starts at boot time using chkconfig
chkconfig httpd on
Start the httpd service
service httpd start
Check is it works by typing http://localhost in the respective systems. If there is no browser installed use wget
wget http://localhost/
The Apache test page should be downloaded. At this step you cannot access other systems’ web server because firewall rules are not yet configured.
Configure network settings on all systems
The Load Balancer should have two network adapters. Configure the network adapters to use the correct IP address by editing files
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-eth1
The template of the file (for load balancer) is provided below
DEVICE=eth0
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
ONBOOT=yes
BOOTPROTO=none
For other three computers set the gateway as 10.0.0.1
DEVICE=eth0
IPADDR=10.0.0.2
NETMASK=255.0.0.0
GATEWAY=10.0.0.1
ONBOOT=yes
BOOTPROTO=none
Configure proper IP address settings on all the systems. Except the load balancer other systems have only eth0. After saving the files restart the network service.
service network restart
ifconfig
the ifconfig command will display the IP address of each adapter. After configuration ping each other systems’ 10.0.0.X (replace X with the appropriate number) IP address to see whether they are accessible.
Configure mod_proxy_balancer in Load Balancer
On the load balancer system open the Apache configuration file
/etc/httpd/conf/httpd.conf
Make sure the following lines are uncommented so that the proper modules load
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_balancer_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Add the following lines to the end of the httpd.conf file
echo "ProxyRequests Off" >> /etc/httpd/conf/httpd.conf
echo "<Proxy balancer://mycluster>" >> /etc/httpd/conf/httpd.conf
echo "BalancerMember http://10.0.0.2:80" >> /etc/httpd/conf/httpd.conf
echo "BalancerMember http://10.0.0.3:80" >> /etc/httpd/conf/httpd.conf
echo "</Proxy>" >> /etc/httpd/conf/httpd.conf
Save the file and run a configtest to check if there are any syntax errors
service httpd configtest
If there you see Syntax OK restart the service
service httpd restart
Configure NFS on storage server
By default NFS is installed while installing Linux. So you just have to configure it. Create a directory that will host the web content.
mkdir /docroot
Configure the /etc/exports file to share the docroot folder. Add the following line in the /etc/exports file
/docroot 10.0.0.*(rw,sync)
You can choose whether to allow other systems to have write access on this folder. If you the root user of the other systems have to write to this location use the option no_root_squash.
Configure firewall rules and SELinux policies on all systems
If you’re not comfortable editing iptables through the command line use
system-config-firewall-tui
to allow port 80 on the load balancer and web servers. Others use the following command for load balancer
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
Web servers
iptables -I INPUT -p tcp --dport 80 -i eth0 -j ACCEPT
Storage Server
iptables -I INPUT -p tcp --dport 2049 -i eth0 -j ACCEPT
All systems
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -j REJECT
Save the rules on all systems
service iptables save
On the Web Servers (NOT load balancer) configure SELinux booleans
setsebool -P httpd_use_nfs 1
This will allow Apache to read from the NFS directories
Mount the NFS share in the web servers as the DocumentRoot
On the web servers add the following line in the /etc/fstab file to mount NFS share
10.0.0.4:/docroot /var/www nfs soft,nfsvers=4 0 0
Here 10.0.0.4 is the IP address of the NFS server. After saving the file remount all mount points
mount -a
Now in the NFS server create the folders www and cgi-bin
mkdir /docroot/{www,cgi-bin}
Create a welcome screen inside the www folder
echo "<h1>Welcome to our cluster</h1>" > /docroot/www/index.html
From a computer connected to the same network as the load balancer (192.168.0.0/24) open the browser and type the IP address of the load balancer
http://192.168.0.2/
If everything was successfully configured you should see the welcome page. Now to check from which web server is serving your requests you need to create a CGI script which displays the hostname of the server. On the NFS server create a file in the /docroot/cgi-bin directory and add the content
touch /docroot/cgi-bin/script.cgi
chmod o+x /docroot/cgi-bin/script.cgi
#!/bin/bash
echo -e "Content-Type: text/plain\n"
hostname
Save the file and point the browser of the computer which you used to test earlier
http://192.168.0.2/cgi-bin/script.cgi
When you keep refreshing this page you should see different hostnames being displayed. Take a look at the screenshot below
Now its time to check Failover clustering. To test this go to either of the web servers and bring the network interface down
ifdown eth0
Go to the browser and refresh it it will take a few seconds then it will display the hostname of only the server whose network interface is up. All this while did you notice that we keep seeing the hostnames of the web servers and not the load balancer or the storage server. This is because the load balancer is merely a proxy forwarding requests to the web servers and the storage server just hosts the CGI script so the actual execution of the secript takes place on the web servers.
Some things to take note when using clustering
If you plan to use a server side scripting language such as PHP the session variables are stored in the web server itself so this poses a problem as a session variable might not be same on all pages. To prevent this you can place the session save path on the storage server it self and mount it on /var/lib/php/sessions/
Another problem you might face is with the upload temp dir. The default is usually the /tmp directory. By editing the /etc/php.ini file you can change it to something else and mount that directory to the storage server.
The Apache load balancer has three algorithms, to know more about these refer to the Apache documentation at http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html. To use a specific algorithm open the /etc/httpd/conf/httpd.conf file in the Load Balancer and add the following line in between <Proxy> directive
ProxySet lblmethod=method-name
example
ProxySet lblmethod=bytraffic
Nilesh says
Nice Document. It help me to configured Apache Load Balance
h says
Can You post for Configure Apache Web Server Load Balancing in Windows