<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kandy Software, Inc. &#187; load balance</title>
	<atom:link href="http://www.kandysoftwareinc.com/tech-articles/tag/load-balance/feed" rel="self" type="application/rss+xml" />
	<link>http://www.kandysoftwareinc.com</link>
	<description></description>
	<lastBuildDate>Wed, 17 Dec 2008 06:47:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Apache, Tomcat cluster, load balancing using mod_jk</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/apache-tomcat-cluster-load-balancing-using-mod_jk</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/apache-tomcat-cluster-load-balancing-using-mod_jk#comments</comments>
		<pubDate>Thu, 14 Feb 2008 21:46:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[cluster]]></category>
		<category><![CDATA[httpd.conf]]></category>
		<category><![CDATA[load balance]]></category>
		<category><![CDATA[mod_jk]]></category>
		<category><![CDATA[multicast]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[worker]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=64</guid>
		<description><![CDATA[This article discusses how to configure multiple Tomcat instances to balance work load using mod_jk Apache connector.]]></description>
			<content:encoded><![CDATA[<p>[print_link]</p>
<h3><strong>Introduction</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/tomcat.gif"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/tomcat.gif" alt="" title="tomcat" width="130" height="92" class="alignleft size-thumbnail wp-image-68" /></a><a href="http://www.kandysoftwareinc.com/articles/mod_jk.pdf" target="_blank"><img src="http://www.kandysoftwareinc.com/images/PDF_icon.jpg" align="right" height="50" width="50" class="alignleft" border="0"/></a>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.<br />
<br/></p>
<h3><strong>Install JDK</strong></h3>
<ol>
<li>
Download JDK rpm and launch the executable by running the following commands:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> a+x j2sdk-<span style="color: #000000;">1</span>_4_2_<span style="color: #000000; font-weight: bold;">&lt;</span>version number<span style="color: #000000; font-weight: bold;">&gt;</span>-linux-i586-rpm.bin
.<span style="color: #000000; font-weight: bold;">/</span>j2sdk-<span style="color: #000000;">1</span>_4_2_<span style="color: #000000; font-weight: bold;">&lt;</span>version number<span style="color: #000000; font-weight: bold;">&gt;</span>-linux-i586-rpm.bin</pre></div></div>

</li>
<li>
Type <strong>yes</strong> and agree to the binary license agreement.
</li>
<li>
Become root by running <strong>su</strong> and entering root password
</li>
<li>
Install the rpm. This will install JDK at <code>/usr/java/j2sdk-1.4.2</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rpm <span style="color: #660033;">-iv</span> jdk-1.4.2_<span style="color: #000000; font-weight: bold;">&lt;</span>version number<span style="color: #000000; font-weight: bold;">&gt;</span>.i386.rpm</pre></div></div>

</li>
<li>
Edit PATH environment variable to include bin directory of JDK by adding the following lines to <code>/etc/profile</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>java<span style="color: #000000; font-weight: bold;">/</span>j2sdk-1.4.2_<span style="color: #000000; font-weight: bold;">&lt;</span>version number<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> JAVA_HOME
<span style="color: #007800;">PATH</span>=<span style="color: #007800;">$JAVA_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #007800;">$PATH</span></pre></div></div>

</li>
</ol>
<h3><strong>Avoid Firewall and Routing issues</strong></h3>
<ol>
<li>
Disable the default firewall (unless you can configure it to allow multicast packets)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>iptables stop
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>ip6tables stop</pre></div></div>

</li>
<li>
Add this multicast route:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>route add <span style="color: #660033;">-net</span> 224.0.0.0 netmask 240.0.0.0 dev eth<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #c20cb9; font-weight: bold;">id</span><span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

</li>
</ol>
<h3><strong>Install Apache</strong></h3>
<ol>
<li>
Download <code>httpd-2.0.50-i686-pc-linux-gnu.tar.gz</code> into a temporary location and run the following commands to install Apache at <code>/usr/local/share</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">unzip</span> <span style="color: #000000; font-weight: bold;">&lt;</span> httpd-2.0.50-i686-pc-linux-gnu.tar.gz <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> xvf -
<span style="color: #7a0874; font-weight: bold;">cd</span> httpd-2.0.50
.<span style="color: #000000; font-weight: bold;">/</span>install-bindist.sh <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apache-2.0.50</pre></div></div>

</li>
<li>
Edit PATH environment variable to include bin directory of Apache by adding the following lines to <code>/etc/profile</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">APACHE_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apache-2.0.50
<span style="color: #7a0874; font-weight: bold;">export</span> APACHE_HOME
<span style="color: #007800;">PATH</span>=<span style="color: #007800;">$APACHE_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #007800;">$PATH</span>
<span style="color: #7a0874; font-weight: bold;">export</span> PATH</pre></div></div>

</li>
</ol>
<h3><strong>Install Tomcat</strong></h3>
<ol>
<li>
Move <code>apache-tomcat-x.y.z.zip.zip</code> to <code>/usr/local/share</code> (I will refer to the installation folder by this path) and unzip it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">unzip</span> apache-tomcat-x.y.z.zip</pre></div></div>

</li>
<li>
Edit PATH environment variable to include bin directory of Apache by adding the following lines to <code>/etc/profile</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">TOMCAT_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apache-tomcat-x.y.z
<span style="color: #7a0874; font-weight: bold;">export</span> TOMCAT_HOME
<span style="color: #007800;">PATH</span>=<span style="color: #007800;">$TOMCAT_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #007800;">$PATH</span>
<span style="color: #7a0874; font-weight: bold;">export</span> PATH</pre></div></div>

</li>
</ol>
<h3><strong>Configure mod_jk</strong></h3>
<ol>
<li>
Copy the mod_jk binary to <code>$APACHE_HOME/modules</code> directory and rename it as <code>mod_jk.so</code>
</li>
<li>
Add the following lines at the very bottom in <code>$APACHE_HOME/conf/httpd.conf</code><br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;">#Include mod_jk configuration file</span>
<span style="color: #00007f;">Include</span> conf/mod-jk.conf</pre></div></div>

</li>
<li>
Under <code>$APACHE_HOME/conf</code> create <code>mod-jk.conf</code> and write the following lines in it:<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;"># Load mod_jk module</span>
<span style="color: #adadad; font-style: italic;"># Specify the filename of the mod_jk lib</span>
<span style="color: #00007f;">LoadModule</span> jk_module modules/mod_jk.so
<span style="color: #adadad; font-style: italic;"># Where to find workers.properties</span>
JkWorkersFile conf/workers.properties
<span style="color: #adadad; font-style: italic;"># Where to put jk logs</span>
JkLogFile logs/mod_jk.log
<span style="color: #adadad; font-style: italic;"># Set the jk log level [debug/error/info]</span>
JkLogLevel info
<span style="color: #adadad; font-style: italic;"># Select the log format</span>
JkLogStampFormat <span style="color: #7f007f;">&quot;[%a %b %d %H:%M:%S %Y]&quot;</span>
<span style="color: #adadad; font-style: italic;"># JkOptions indicates to send SSK KEY SIZE</span>
JkOptions +ForwardKeySize +ForwardURICompat -
ForwardDirectories
<span style="color: #adadad; font-style: italic;"># JkRequestLogFormat</span>
JkRequestLogFormat <span style="color: #7f007f;">&quot;%w %V %T&quot;</span>
JkMount /Context/* loadbalancer</pre></div></div>

<p><br/><br />
Where <font color="#ff0000"><strong>Context</strong></font> is the web-app context name.
</li>
<li>
Under <code>$APACHE_HOME/conf</code> create <code>workers.properties</code> and populate it as follows:<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;"># Worker list</span>
worker.list=loadbalancer
<span style="color: #adadad; font-style: italic;"># Define Node1</span>
worker.node1.<span style="color: #00007f;">port</span>=<span style="color: #ff0000;">8009</span>
worker.node1.host=node1.ip.address
worker.node1.type=ajp13
worker.node1.lbfactor=<span style="color: #ff0000;">1</span>
<span style="color: #adadad; font-style: italic;">#worker.node1.local_worker=1 (1)</span>
worker.node1.<span style="color: #00007f;">cachesize</span>=<span style="color: #ff0000;">10</span>
<span style="color: #adadad; font-style: italic;"># Define Node2</span>
worker.node2.<span style="color: #00007f;">port</span>=<span style="color: #ff0000;">8009</span>
worker.node2.host=node2.ip.address
worker.node2.type=ajp13
worker.node2.lbfactor=<span style="color: #ff0000;">1</span>
<span style="color: #adadad; font-style: italic;">#worker.node2.local_worker=1 (1)</span>
worker.node2.<span style="color: #00007f;">cachesize</span>=<span style="color: #ff0000;">10</span>
<span style="color: #adadad; font-style: italic;"># Load-balancer</span>
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=node1, node2
worker.loadbalancer.sticky_session=<span style="color: #ff0000;">1</span>
worker.loadbalancer.local_worker_only=<span style="color: #ff0000;">1</span></pre></div></div>

<p>Add one worker per node and name workers as node1, node2, node3 … nodeN. Don’t forget to define all these as load balancing workers:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">worker.loadbalancer.balanced_workers=node1, node2, node3, ... nodeN</pre></div></div>

<p>Replace <code>node1.ip.address</code> with IPAddress of Tomcat(or JBoss) instance1 and so on. The attribute <code>sticky_session=1</code> tells mod_jk that SESSION ID&#8217;s should be routed back to the same Tomcat worker. The <code>lbfactor</code> determines the number of requests a worker receives. The higher the <code>lbfactor</code> the more requests a worker receives.
</li>
<li>
Each node should be named according to the names specified in <code>workers.properties</code>. On each Tomcat instance, edit <code>$TOMCAT_HOME/conf/server.xml</code>. Locate the &lt;Engine&gt; element and add an attribute called jvmRoute:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Engine</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Catalina&quot;</span> <span style="color: #000066;">defaultHost</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span> <span style="color: #000066;">jvmRoute</span>=<span style="color: #ff0000;">&quot;node1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
…
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Engine<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</li>
</ol>
<p><font color="#ff0000"><strong>NOTE: THE jvmRoute ATTRIBUTE MUST MATCH THE NAME SPECIFIED IN workers.properties.</strong></font></p>
<h3><strong>Start Apache and Tomcat</strong></h3>
<ol>
<li>
Start Apache:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$APACHE_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>apachectl <span style="color: #660033;">-k</span> start</pre></div></div>

</li>
<li>
To stop use:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$APACHE_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>apachectl <span style="color: #660033;">-k</span> stop</pre></div></div>

</li>
<li>
Start Tomcat:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$TOMCAT_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>startup.sh</pre></div></div>

</li>
<li>
Stop Tomcat:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$TOMCAT_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>shutdown.sh</pre></div></div>

</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/apache-tomcat-cluster-load-balancing-using-mod_jk/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
