Adding a Front-end Apache Cloud Load Balancer


The next step in our fully redundant, high-availability LAMP stack is adding load balancing.

This means setting up a new server in front of our two web servers.  This load balancing server will monitor both web servers, and switch traffic between them intelligently.

Load balancing setup on Crosspeer has special requirements, because our system does not allow floating Mac addresses.  This makes using VRRP (Virtual Router Redundancy Protocol), which is the basis for many load balancers, problematic.

We use the Apache module mod_proxy_balancer.  This turns Apache into a load balancer, retrieving requested pages from two or more backend web servers and delivering them to the user's computer.

If required, mod_proxy_balancer can keep track of sessions, so a single user always deals with the same backend webserver.

The steps are as follows:

Step 1:  Set up a new server
Step 2:  Install Apache and mod_proxy_balancer
Step 3:  Configure mod_proxy_balancer




Step 1:  Set up a New Server

First we create a fifth Ubuntu 10.04 server, which will be our load balancer. We'll call this new server herring.

Then set herring up to share a private VLAN with our web and database servers, as described in our VLAN tutorial. In this tutorial, we've given herring the IP address 10.0.0.5.

In addition, we will need an external static IP: add this as described in our static IP tutorial. In this tutorial, we've given herring the IP address 46.20.121.121.

Note: In the next tutorial, you'll be setting up a second load balancer with another static IP address. These should be within the same class C range, so you may find it helpful to set up two static IP addresses now, and check that they are within the same A.B.C.* range.

Just to recap, we currently have five servers in total:

     seagull:  Web server 1, private IP address 10.0.0.1.
     pelican:  Web server 2, private IP address 10.0.0.4.
     beagle:  Database server 1, private IP address 10.0.0.2.
     crocodile:  Database server 2, private IP address 10.0.0.3.
     herring:  Load balancer 1, private IP address 10.0.0.5 and static IP address 46.20.121.121.

In a real system, at this point you might want to configure your DNS servers to give each of your servers with a subdomain
(e.g. db1.yourdomain.com, web1.yourdomain.com, loadb1.yourdomain.com etc) in order to check their status easily.



$ apt-get update
$ apt-get install apache2
$ a2enmod proxy
$ a2enmod proxy_balancer
$ a2enmod proxy_http
$ /etc/init.d/apache2 force-reload
Step 3:  Configure mod_proxy_balancer

Now, we need to configure our cluster, in a new file in conf.d:
$ vi /etc/apache2/conf.d/proxy-balancer
<Proxy balancer://mycluster>
BalancerMember http://10.0.0.1
BalancerMember http://10.0.0.4
</Proxy>
ProxyPass / balancer://mycluster
Then we need to configure our proxy to allow access from all hosts:
$ vi /etc/apache2/mods-enabled/proxy.conf
Change the line "Deny from all" in this file to "Allow from all". Then restart Apache:
$ /etc/init.d/apache2 restart
Now, when you visit http://46.20.121.121 in a web browser, you should see our site. You should also be able to stop either of the web servers, or either of the database servers, and the site should continue to run.

You can find more information on configuring mod_proxy_balancer - including balancing methods, sticky sessions and dynamic updates to load balancing at the Apache documentation.
Step 2:  Install Apache and mod_proxy_balancer

First, on our new load balancing server, we need to install Apache and enable the proxy modules:
Copyright © 2012 - 2014 Crosspeer, Inc.
               All Rights Reserved