Apache2.* + Tomcat 4.* + Load Balance
Updated: 2003-11-15
Edition: 1.0
================================================================================
Introduction
The Tomcat servlet container will crashed in some strange situation recently in my office.
I must restart the server to supply the service as soon as possible. So, I got no time to
find the bug. I want to build a load balancing system to supply the service of 7*24.
That is mean, when one instance of tomcat server crashed another instance of tomcat server
should take over the responsibility to service the client's requests.
I don't want to reconstruct all the applications after setup the load balance sytem, so, I
choose the sticky session solution of load balance. That's mean when a request of one user
is responsed by tomcat1 server, then the following request should be responsed by the same
tomcat server, tomcat1. Anyway, if the tomcat1 server has crashed by some reason, the followed
request will be delegated to the second server, tomcat2.
I use linux box as server in office. At home I used windows as work-station. The following
steps are tested on windows box.
Here is the steps how do I setup the load balance system.
1. Download the required software
Apache2.x -- http://httpd.apache.org
Tomcat4.x -- http://jakarta.apache.org/tomcat/
The JK Module
2. Install and configure the Apache
2.1 Install Apache
Click downloaded install archive, for widows this should be *.msi. Follow the wizard to install.
2.2 Configure the JK2 Module in httpd.conf
Add following lines below the Dynamic Shared Object (DSO) support secion.
#
# Load JK Module
#
LoadModule jk2_module modules/mod_jk2-2.0.43.dll
2.3 Create the workers2.properties file under ${ServerRoot}/conf, which ${ServerRoot} should be
the directory that apache web server installed at.
# only at beginnin. In production uncomment it out
[logger.apache2]
level=DEBUG
[shm]
file=c:/jk2.shm
size=1048576
# Example socket channel, override port and host.
[channel.socket:localhost:12009]
tomcatId=tomcat2
group=lb:lb0
[channel.socket:localhost:11009]
tomcatId=tomcat1
group=lb:lb0
#
# defined workers
#
[status:me]
[lb:lb0]
stickySession=0
# Uri mapping
[uri:/examples/*]
group=lb:lb0
[uri:/*.jsp]
group=lb:lb0
[uri:/jkstatus]
group=status:me
3. Install and configure the Tomcat
Unzip the downloaded tomcat archive.
unzip tomcat-4.*.zip tomcat1
copy tomcat1 tomcat2
In both tomcat1 and tomcat2, the same files will be modified. Here, I present the modifications
to the files in tomcat1. You should apply the same changes to the corresponding files located
under tomcat2.
3.1 Modify the catalina.bat to add following lines:
JAVA_HOME=c:/j2sdk1.4
CATALINA_HOME=c:/tomcat1
3.2 Modify conf/server.xml
3.2.1 Add unique jvmRoute to Tomcat Engine
Replace following line
<Engine name="Standalone" defaultHost="localhost" debug="0">
with:
<Engine jvmRoute="tomcat1" name="Standalone" defaultHost="localhost" debug="0">
for tomcat2, set jvmRoute="tomcat2"
3.2.2 Modify the control port
replace:
<Server port="8005"
with:
<Server port="11005"
For the tomcat2 server, replace port 8005 with 12005. This will prevent the two servers from conflicting.
3.2.3 Change the AJP13 port
In the AJP 13 connector definition, replace:
port="8009"
with:
port="11009"
For the tomcat2 server, replace port 8009 with 12009.
3.2.4 Disable the standalone HTTP port
I don't want the server to respond the HTTP request directly. So, I comment out the HttpConnector section.
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<!--
<Connector className="org.apache.catalina.connector.http.HttpConnector"
port="8080" minProcessors="5" maxProcessors="75"
enableLookups="true" redirectPort="8443"
acceptCount="10" debug="0" connectionTimeout="60000"/>
-->
3.3 Modify conf/jk2.properties
3.3.1 Replace jk2.properties under c:\tomcat1\conf
# Set the desired handler list
handler.list=channelSocket,request
#
# Override the default port for the socketChannel
channelSocket.port=11009
# Default:
shm.file=c:/jk2.shm
For the tomcat2 server, make channelSocket.port to 12009
3.4 Create test jsp file (index.jsp)
3.4.1 Create a file named index.jsp and put it under c:\tomcat1\webapps\ROOT
<html>
<body bgcolor="red">
<center>
<%= request.getSession().getId() %>
<h1>Tomcat 1</h1>
</body>
</html>
3.4.2 Create a file named index.jsp and put it under c:\tomcat2\webapps\ROOT
<html>
<body bgcolor="blue">
<center>
<%= request.getSession().getId() %>
<h1>Tomcat 2</h1>
</body>
</html>
4. Start Tomcat1, Tomcat2, Apache
c:\tomcat1\bin\startup.bat
c:\tomcat2\bin\startup.bat
c:\apache2\Apache.exe
5. Test your Installation
Now is the time to test your setup. First, verify that Apache serves static content.
Click on: http://localhost/. You should see the default Apache index.html page.
Now test that tomcat (either Tomcat 1 or Tomcat 2) is serving Java Server Pages.
Click on: http://localhost/index.jsp
Click on: http://localhsot/jkstatus to show the status of jk2 module in Apache.