Apache load balancer server backup

apache-2.2load balancing

I want to use apache load balancer so that one tomcat server is primary, and the secondary server is online only if the first server is down. how can this be done?

Best Answer

If you're only going to do load balancing, I would recommend something more lightweight than Apache, such as HAproxy, but lets assume that you're going to go with apache.

There are several methods for load balancing, so you should spend a little bit of time getting acquainted with them. There is an excellent article available by Willy Tarreau called Making applications scalable with Load Balancing that is worth your time to read.

There are a couple of tools for making Apache a load balancer, and maybe the easiest (that I know of) is by using mod_proxy_balancer. The only problem with this is that it doesn't appear to do failover load balancing (which is what you want: high availability, rather than true load balancing).

Since you're using Tomcat, why not use mod_jk, the Apache tomcat connectors?

Assume for a second that we're on a Redhat-based system (I'm using CentOS 5.5) that is configured to compile things (for instance, has gcc and the appropriate libraries (this isn't a good idea on production systems. This is not the time or place to discuss RPM packaging, though)

 yum install httpd httpd-devel

This will install Apache along with the development stuff for making modules, and the configuration directory will be /etc/httpd/

The current (20110130) Tomcat Connectors are 1.2.31 (but you can check for the latest here), so download them:

 wget http://mirror.cc.columbia.edu/pub/software/apache//tomcat/tomcat-connectors/jk/source/jk-1.2.31/tomcat-connectors-1.2.31-src.tar.gz

Extract, compile, and install them:

 tar zxvf tomcat-connectors-1.2.31-src.tar.gz 
 cd tomcat-connectors-1.2.31-src/native
 ./configure --with-apxs=/usr/sbin/apxs 
 sudo make install 

Verify that mod_jk.so was installed:

 ls -al /etc/httpd/modules/mod_jk.so 
 -rwxr-xr-x 1 root root 903072 Jan 30 15:21 /etc/httpd/modules/mod_jk.so

Now, we get to actually configure it.

Edit /etc/httpd/conf.d/jk.conf:

 # This actually tells apache to load the module we built
 LoadModule jk_module modules/mod_jk.so 

 # This file holds the instructions for which servers the proxy will be talking to
 JKWorkersFile /etc/httpd/conf.d/workers.properties

 # This is (obviously) the logfile that it will write to. Change to whatever you want
 JKLogFile /var/log/httpd/mod_jk.log 

 # Simplistically, we'll set up a virtualhost that listens on all IPs on port 80. 
 # HTTPS is left as an exercise for the reader
 NameVirtualHost *:80
 <VirtualHost *:80>
     ServerAdmin you@yourdomain.com 
     ServerName thedomainnameoftheproxy.com

     # The following line says "send anything to the JK Worker named MyProxyCluster"
     JKMount /* MyProxyCluster


Alright, that tells Apache that we want to use mod_jk, that we want to use a file called /etc/httpd/conf.d/workers.properties, and that we have one virtualhost where everything is sent to the workers. Great, now lets configure the workers. Edit /etc/httpd/conf.d/workers.properties:

 # We're telling mod_jk which workers it needs to look for. We're going to define 
 # a total of 3 workers: MyProxyCluster (the load balancer job), worker1, and worker2 
 # (the two backend servers) 


 # ajp13 means Apache JServ Protocol version 1.3, and is apparently universal


 # When worker1 dies, we're going to want it to go to worker2

 ### End of worker1 config

 # repeat the same things for worker2
 # Disable worker2 except when it fails over

 # make the actual load balancer worker that we referenced in the beginning
 # EOF

Now, we have two actual workers, and one virtual load balancer for them. You can now start Apache:

 service httpd start 

Test it out by browsing to the machine, and it should go to the first tomcat server. You should be able to verify this by watching /var/log/httpd/mod_jk.log (or wherever you pointed it).

Good luck!

Related Topic