To set up mod_jk load balancing with Apache HTTP and Tomcat/tc Server:
- Create or modify the Apache httpd conf/workers.properties file to define the workers to which the requests are to be load balanced, along with their associated ports and hosts. For example, if there are two Tomcat/tc Server workers running on the localhost listening on AJP ports 8209 and 8309, the workers.properties file appears similar to:
worker.list=loadbalancer
worker.tcruntime8280.port=8209
worker.tcruntime8280.host=localhost
worker.tcruntime8280.type=ajp13
worker.tcruntime8280.lbfactor=1
worker.tcruntime8380.port=8309
worker.tcruntime8380.host=localhost
worker.tcruntime8380.type=ajp13
worker.tcruntime8380.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tcruntime8280,tcruntime8380
Where
- worker.list lists the workers visible external to mod_jk. In this case, only the loadbalancer worker is used in the Apache httpd.conf/ httpsd.conf file.
- worker.tcruntime8380 and worker.tcruntime8380 properties identify two Tomcat/tcServer instances to receive requests forwarded from Apache using mod_jk loadbalancing.
- worker.loadbalancer properties define the worker loadbalancer to be a load balancing (lb) worker and that its set of worker servers consists of internal workers tcruntime8380 and tcruntime8380 .
- In the Apache HTTP Server's conf/httpd.conf or conf/httpsd.conf file, load and configure the mod_jk module. A sample configuration appears similar to:
LoadModule jk_module "/home/ers403/apache2.2/modules/jakarta/mod_jk.so"
<IfModule mod_jk.c>
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkShmFile logs/jk-runtime-status
JkWorkersFile conf/workers.properties
JkMount /TCSession loadbalancer
JkMount /TCSession/* loadbalancer
</IfModule>
- In the Tomcat/tc Server instance(s) server.xml file, ensure that an AJP connector is created with the port matching the value entered in the workers.properties file. For sticky sessions, also ensure that a jvmRoute matching the worker name used in the workers.properties file is set. For example, for the two workers shown in the preceding workers.properties file, you see:
tcruntime8280 worker:
<Engine defaultHost="localhost" name="Catalina" jvmRoute="tcruntime8280">
[...]
</Engine>
<Connector port="8209" protocol="AJP/1.3" ... />
tcruntime8380 worker:
<Engine defaultHost="localhost" name="Catalina" jvmRoute="tcruntime8380">
[...]
</Engine>
<Connector port="8309" protocol="AJP/1.3" ... />
To verify if the configuration is set correctly and the sticky sessions are working:
- Extract the sample Web application, TCSession.war, from the 2010433 _TCSession.zip file attached to this article.
- Save TCSession.war to each Tomcat/tc Server instance's webapps directory, so that it is deployed.
- Access the Web application via Apache HTTP server with a UR, such as http://localhost/TCSession.
- During the first access of a session, you should see a display similar to:
TOMCAT localhost: This is the session id 5B5CF832B4CB62BEEECDABC334043286.tcruntime8380
This is a new session !
- During subsequent requests sent for the same session, you should see a display similar to:
TOMCAT localhost: This is the session id 5B5CF832B4CB62BEEECDABC334043286.tcruntime8380
This is an existing session
Session Value: This is in my session