Wednesday, April 15, 2009

Tomcat Clustering and Load-balancing with Apache

It is quite surprising that I was unable to find a single article for Tomcat 6.0 clustering and Load-balancing with the help of Apache 2.2. I needed it in one of my project to check the performance of the web application. Of course, I got a big help from one of the article on this link.

My goal was to put 3 Tomcat servers on cluster with load-balancing by Apache 2.2. So, these are the steps to achieve this goal:
A. Download Apache Tomcat 6.0.18 from Apache Tomcat Download Page. Download binary zipped format.
B. Download Apache 2.2 HTTP server from Apache Download Center.
C.Download Tomcat Connectors. Click on "JK" --> Binaries --> Win32 --> jk-1.2.21 --> mod_jk-apache-2.2.4.so
D. We will test 2 Tomcat server instances in cluster and 1 Apache HTTP server for load balancing. Assume we have 3 machines on network with IP 172.16.xx.xxx, 172.16.yy.yyy and 172.16.zz.zzz respectively. Let us set-up Apache HTTP Server first on 172.16.zz.zzz.

1. Install Apache HTTP Server from the setup file you downloaded from Apache Download Center.
2. Start it and test it once. Go to "http://localhost/" in your favorite browser. You will get something like "It works" as result. Stop it and let us setup Connector.
3. Copy the mod_jk-apache-2.2.4.so to the modules directory in modules directory in your Apache installation.
4. Rename it to mod_jk.so
5. Open up httpd.conf in the conf directory of your Apache installation in a text edit, and add the following line at the end of the set of LoadModule statements:
LoadModule jk_module modules/mod_jk.so
6. Create a file called workers.properties in the conf directory. Add these lines to it:

* workers.java_home=$JAVA_HOME
* worker.list=balancer
* worker.worker1.port=8009
* worker.worker1.host=172.16.xx.xxx
* worker.worker1.type=ajp13
* worker.worker1.lbfactor=1
* worker.worker2.port=8009
* worker.worker2.host=172.16.yy.yyy
* worker.worker2.type=ajp13
* worker.worker2.lbfactor=1
* worker.balancer.type=lb
* worker.balancer.balance_workers=worker1,worker2
* worker.balancer.method=B

Put your java home directory instead of $JAVA_HOME.
7. Specify the worker properties in httpd.conf:

Add these lines just after the LoadModule definitions-

# Path to workers.properties
JkWorkersFile c:/apache2.2/conf/workers.properties

# Path to jk logs
JkLogFile c:/apache2.2/mod_jk.log

# Jk log level [debug/error/info]
JkLogLevel info

# Jk log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions for forwarding
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"


JkMount /MyApp balancer
JkMount /MyApp/* balancer


"MyApp" is your web-application name. And, I installed Apache2.2 directly under C: drive.


E. Put one Tomcat instance on 172.16.xx.xxx and another on 172.16.yy.yyy. Edit the server.xml for Tomcat and Tomcat's SecondInstance and add a jvmRoute attribute to the Engine element:
(engine name="Catalina" defaulthost="172.16.xx.xxx" jvmroute="worker1")
for 172.16.xx.xxx and
(engine name="Catalina" defaulthost="172.16.yy.yyy" jvmroute="worker2")
for 172.16.yy.yyy.
Also, replace "localhost" words in "server.xml" with "IP" of the machine.

***Please check you do not have space between the directory names. Ex: "C:\Tomcat Server". There is a space between "Tomcat" and "Server". Your Apache server will not start if you have directory names like this in "httpd.conf" and "worker.properties" files. You better make it "C:\TomcatServer". Tell me if you find a way to rectify this issue.***

Start Tomcat servers and then start Apache HTTP Server. Now, Your Tomcat servers are in cluster and Apache HTTP server is managing load-balancing.


Lexmark Ink Cartridges

1 comments:

  1. Dear Sumved Shami,

    Ur post is cool and useful could you tell me a way to test this load balancing scenario or can u direct me to a place where I can get the testing procedure. plz me to my account gerald.amalraj@gmail.com

    Thanks

    ReplyDelete