Load Balance Apache Web Servers With Ubuntu 12.10

Configuring one or more Ubuntu servers to utilize load balancing with the mod_proxy_balancer module is a piece of cake.  I’ve setup two freshly imaged virtual machines with static IP addresses running Ubuntu 12.10 for this tutorial, and will be walking you through the process of configuring load balancing with Apache.  This is an easy and inexpensive way to scale your existing web infrastructure out horizontally, using little more than software to accomplish higher levels  of performance and redundancy.  There are highly specialized hardware solutions out there that can achieve this, but they tend to be expensive and difficult to implement.  Here’s how you setup load balanced web servers on Ubuntu 12.10 :

I’ve already taken the liberty of installing Apache ahead of time, but if you are curious on how to do that you can check this article out here or here.  Before we start there are a couple of concepts that we need to clear up to have a better understanding of load balancing with the mod_proxy_balancer module.  Though we are starting with two servers, we could theoretically have any number of servers in this distributed pool – but the key is that only one of the servers will serve as the load balancer.  This is the server that ensures the high availability and provides the load balancing.  It helps if you think of it as one server taking all of the web requests, then daisy chaining them out to the other servers in whatever order you specify in the config files.  Let’s say you have 2 servers,  and 1 has 3X the resources of the other, then you would likely want to split the load accordingly by giving the more capable machine every 3 out of 4 GET/WGET requests.

That being said, let’s jump into it.  Make sure you have two freshly imaged servers that are fully up to date.

sudo apt-get update

sudo apt-get upgrade -y

Now enable the proxy, proxy_balancer, and proxy_http apache modules and restart the apache service.  Do this on the designated load balancer server only.

sudo a2enmod proxy_balancer
Considering dependency proxy for proxy_balancer:
Module proxy already enabled
Enabling module proxy_balancer.
To activate the new configuration, you need to run:
service apache2 restart
sudo a2enmod proxy_http
Considering dependency proxy for proxy_http:
Module proxy already enabled
Enabling module proxy_http.
To activate the new configuration, you need to run:
service apache2 restart

sudo service apache2 restart
* Restarting web server apache2

Next up we’ll create the /etc/apache2/conf.d/proxy-balancer file and start designating our member servers.

techstatycluster>
BalancerMember http://192.168.1.105
BalancerMember http://192.168.1.105
</Proxy>
ProxyPass / balancer://techstatycluster

So we’ve designated two servers to distribute web requests, LB-APACHE1 and LB-APACHE2.  Next we need to configure the proxy we just created to adjust the host access directive.

ProxyRequests On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
#Allow from .example.com
</Proxy>

Notice that we didn’t uncomment the #Allow from .example.com line – for the purposes of this tutorial, we don’t need this.  This would be a good way to serve internal get requests in an intranet though.

Restart the apache service once more :

sudo service apache2 restart
* Restarting web server apache2

And that’s pretty much it – if you want to see the load balancing in action, you can run a simple netstat on your computer requesting web services on the load balanced cluster, and you would be able to see the proxy handing off requests.

Advertisements
Leave a comment

Leave your opinion

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: