TomCat Load Balancing-Windows
TomCat Load Balancing-Windows
TomCat Load Balancing-Windows
By
Sam Mohan (wfj)
Tomcat is a popular application server used to host web applications. Apache
is a popular web server which provides services like https encryption and decryption,
URL rewriting etc. Apache can also be used a load balancer to balance load between
several Tomcat application servers. This article briefly discusses the procedure
followed for load balancing an application server, in the context of a web application.
Please note: The name ‘APACHE_INSTALL’ refers to the httpd install directory.
1. What is a Load-Balancer?
A Load balancer is a virtual worker that does not really communicate with
Tomcat workers; instead it is responsible for the management of several "real"
workers. For example, we can have a web server such as Apache forwarding servlet
requests to a Tomcat process (the worker) running behind it.
Simply, to balance server load, the system distributes requests to different nodes
within the server cluster, with the goal of optimizing system performance. If one
server cannot handle a request, can other servers in the cluster handle it? In a highly
available system, if a single Web server fails, then another server takes over, as
transparently as possible, to process the request. The user requests are processed by
more than one server with all servers sharing the load ‘equally’.
Fig: 1
Load balancing algorithms
1. Apache HTTP server 2.2.11 from The Apache HTTP Server Project. I prefer
the zip over the Windows Installer. You are free to choose the Windows Installer if
you wish, but modify the service instructions below accordingly.
2. Apache Tomcat 5.5.20 from Apache Tomcat downloads
3. Mod JK Tomcat connector from here. Please note: You need to download the
binary- click on JK 1.2 Binary Releases --> win32 --> jk-1.2.28--> mod_jk-apache-
2.2.3.so, and rename it to mod_jk.so.
Apache configuration:
1. Edit the main Apache config file, APACHE_INSTALL/conf/httpd.conf and add the
following immediately after the other LoadModule directives:
• LoadModule jk_module modules/mod_jk.so
Fig: 2
2. You must inform mod_jk the location of your workers.properties file. Use mod_jk's
JkWorkersFile configuration directive. Similarly, you should specify a location where
mod_jk is going to place its log file and a log level to be used. Use the JkLogFile and
JkLogLevel configuration directives.
Add the following just before the end of the APACHE_INSTALL/conf/httpd.conf
file, at the end.
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
Fig: 3
Tomcat worker is a Tomcat instance that is waiting to execute servlets on behalf
of some web server. For example, we can have a web server such as Apache
forwarding servlet requests to a Tomcat process (the worker) running behind it.
Defining workers to the Tomcat web server plug-in can be done using a properties
file, called worker.properties.
Find more details of Tomcat worker.properties please refer
http://tomcat.apache.org/tomcat-3.3-doc/Tomcat-Workers-HowTo.html.
Fig: 4
Fig: 5
5. Copy the Web application (for eg: ematrix) into TOMCAT_HOME\webapps folder
for both apache-tomcat-5.5.20 and apache-tomcat-5.5.20-1, as well as in
APACHE_INSTALL\htdocs
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat2.port=8019
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
# Define the LB worker
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=tomcat1, tomcat2
worker.loadbalancer.method=Busyness
worker.loadbalancer.sticky_sessions=true
Fig: 7
Fig: 8
Fig: 9
8. Modify server.xml file for Server Port, HTTP Connector Port and AJP Connector
Port on \apache-tomcat-5.5.20-1\conf folder.
Fig: 10
Fig: 11
Fig: 12
With the HTTP connector you can access the installed web applications directly.
8. Edit the server.xml for Tomcat and Tomcat's SecondInstance and add a jvmRoute
attribute to the Engine element:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
for the first instance and
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2">
for the second.
9. Open the APACHE_INSTALL/conf/httpd.conf file and comment out the previous
JkMount directives. Replace them with these:
<VirtualHost localhost:80>
#App server Load Balance
JkMount /*.jsp loadbalancer
JkMount /*.inc loadbalancer
JkMount /*.js loadbalancer
JkMount /*.properties loadbalancer
JkMount /*/*.inc loadbalancer
JkMount /*/*.jsp loadbalancer
JkMount /*/*.js loadbalancer
JkMount /*/*.properties loadbalancer
JkMount /*/*/.jsp loadbalancer
JkMount /*/*/.inc loadbalancer
JkMount /*/*/.js loadbalancer
JkMount /*/*/*.properties loadbalancer
JkMount /*/*/*/*.jsp loadbalancer
JkMount /*/*/*/*.inc loadbalancer
JkMount /*/*/*/*.js loadbalancer
JkMount /*/*/*/*.properties loadbalancer
JkMount /*/*/*/*/*.jsp loadbalancer
JkMount /*/*/*/*/*.inc loadbalancer
JkMount /*/*/*/*/*.js loadbalancer
JkMount /*/*/*/*/*.properties loadbalancer
JkMount /WebClient/*.* loadbalancer
JkMount /*/servlet/* loadbalancer
JkMount /*/com/* loadbalancer
JkMount /*/netscape/* loadbalancer
JkMount /workspace/* loadbalancer
JkMount /workspace/*/* loadbalancer
JkMount /services loadbalancer
JkMount /services/* loadbalancer
</VirtualHost>
Here, we've just pointed Apache to a single worker- the loadbalancer. The mod_jk's
JkMount directive is used to assign specific URLs to Tomcat.
10. Add "CATALINA_HOME = <base path>\apache-tomcat-5.5.20",
"CATALINA_HOME = <base path>\apache-tomcat-5.5.20-1", ... to "startup.bat",
"shutdown.bat", "catalina.bat", "digest.bat" and "service.bat". This is done since
Tomcat instances are running on the same server machine.
References:
*********
1. Load Balancing Tomcat with Apache:
http://www.theserverside.com/tt/knowledgecenter/knowledgecenter.tss?
l=LoadBalancingTomcatApache