Apache, Tomcat cluster, load balancing using mod_jk
[print_link]
Introduction

This article discusses how to install and configure Apache 2.0.x and Tomcat (or JBoss) to communicate with each other via mod_jk. This article also discusses how to setup a Tomcat cluster using mod_jk to balance work load.
Install JDK
-
Download JDK rpm and launch the executable by running the following commands:
chmod a+x j2sdk-1_4_2_<version number>-linux-i586-rpm.bin ./j2sdk-1_4_2_<version number>-linux-i586-rpm.bin
- Type yes and agree to the binary license agreement.
- Become root by running su and entering root password
-
Install the rpm. This will install JDK at
/usr/java/j2sdk-1.4.2:rpm -iv jdk-1.4.2_<version number>.i386.rpm
-
Edit PATH environment variable to include bin directory of JDK by adding the following lines to
/etc/profile:JAVA_HOME=/usr/java/j2sdk-1.4.2_<version number> export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH
Avoid Firewall and Routing issues
-
Disable the default firewall (unless you can configure it to allow multicast packets)
/etc/init.d/iptables stop /etc/init.d/ip6tables stop
-
Add this multicast route:
/sbin/route add -net 224.0.0.0 netmask 240.0.0.0 dev eth<id>
Install Apache
-
Download
httpd-2.0.50-i686-pc-linux-gnu.tar.gzinto a temporary location and run the following commands to install Apache at/usr/local/share:unzip < httpd-2.0.50-i686-pc-linux-gnu.tar.gz | tar xvf - cd httpd-2.0.50 ./install-bindist.sh /usr/local/share/apache-2.0.50
-
Edit PATH environment variable to include bin directory of Apache by adding the following lines to
/etc/profile:APACHE_HOME=/usr/local/share/apache-2.0.50 export APACHE_HOME PATH=$APACHE_HOME/bin:$PATH export PATH
Install Tomcat
-
Move
apache-tomcat-x.y.z.zip.zipto/usr/local/share(I will refer to the installation folder by this path) and unzip it:unzip apache-tomcat-x.y.z.zip -
Edit PATH environment variable to include bin directory of Apache by adding the following lines to
/etc/profile:TOMCAT_HOME=/usr/local/share/apache-tomcat-x.y.z export TOMCAT_HOME PATH=$TOMCAT_HOME/bin:$PATH export PATH
Configure mod_jk
-
Copy the mod_jk binary to
$APACHE_HOME/modulesdirectory and rename it asmod_jk.so -
Add the following lines at the very bottom in
$APACHE_HOME/conf/httpd.conf
#Include mod_jk configuration file Include conf/mod-jk.conf
-
Under
$APACHE_HOME/confcreatemod-jk.confand write the following lines in it:
# Load mod_jk module # Specify the filename of the mod_jk lib LoadModule jk_module modules/mod_jk.so # Where to find workers.properties JkWorkersFile conf/workers.properties # Where to put jk logs JkLogFile logs/mod_jk.log # Set the jk log level [debug/error/info] JkLogLevel info # Select the log format JkLogStampFormat "[%a %b %d %H:%M:%S %Y]" # JkOptions indicates to send SSK KEY SIZE JkOptions +ForwardKeySize +ForwardURICompat - ForwardDirectories # JkRequestLogFormat JkRequestLogFormat "%w %V %T" JkMount /Context/* loadbalancer
Where Context is the web-app context name. -
Under
$APACHE_HOME/confcreateworkers.propertiesand populate it as follows:
# Worker list worker.list=loadbalancer # Define Node1 worker.node1.port=8009 worker.node1.host=node1.ip.address worker.node1.type=ajp13 worker.node1.lbfactor=1 #worker.node1.local_worker=1 (1) worker.node1.cachesize=10 # Define Node2 worker.node2.port=8009 worker.node2.host=node2.ip.address worker.node2.type=ajp13 worker.node2.lbfactor=1 #worker.node2.local_worker=1 (1) worker.node2.cachesize=10 # Load-balancer worker.loadbalancer.type=lb worker.loadbalancer.balanced_workers=node1, node2 worker.loadbalancer.sticky_session=1 worker.loadbalancer.local_worker_only=1
Add one worker per node and name workers as node1, node2, node3 … nodeN. Don’t forget to define all these as load balancing workers:
worker.loadbalancer.balanced_workers=node1, node2, node3, ... nodeN
Replace
node1.ip.addresswith IPAddress of Tomcat(or JBoss) instance1 and so on. The attributesticky_session=1tells mod_jk that SESSION ID’s should be routed back to the same Tomcat worker. Thelbfactordetermines the number of requests a worker receives. The higher thelbfactorthe more requests a worker receives. -
Each node should be named according to the names specified in
workers.properties. On each Tomcat instance, edit$TOMCAT_HOME/conf/server.xml. Locate the <Engine> element and add an attribute called jvmRoute:<Engine name="Catalina" defaultHost="localhost" jvmRoute="node1"> … </Engine>
NOTE: THE jvmRoute ATTRIBUTE MUST MATCH THE NAME SPECIFIED IN workers.properties.
Start Apache and Tomcat
-
Start Apache:
$APACHE_HOME/bin/apachectl -k start
-
To stop use:
$APACHE_HOME/bin/apachectl -k stop
-
Start Tomcat:
$TOMCAT_HOME/bin/startup.sh
-
Stop Tomcat:
$TOMCAT_HOME/bin/shutdown.sh









Nice article. I had no idea of /etc/profile file before reading this entry. I was only aware of the .bash_profile in the user’s directory. So I had no idea how to have a single Java installation and the environment entries at a single place (/etc/profile) for all the users.
Hi,
Very good article. It is in simpler format to set up mod-jk.
Please tell me how to integrate SSL with the Apache mod-jk & Jboss.
I am unable to set up SSL with Jboss apache
Please Help.
Regards,
Dinesh
Leave your response!
Pages
Categories
Calendar
Tags
About Kandy Software Inc.