Tomcat Load Balance using Apache and mod_jk.

What is load balance?
Load balance means dispatching the request between two or more tomcats.
Here all the tomcats which are wants to participante in the load balance will be grouped.
The Group of tomcat is called as tomcat clusters.
What is the need of load balance and tomcat cluster?
Mostly servers will run in single machine. There is a posibility anytime that machine can go down due to any os correption or harderware failure. So if it happens you cant access the server anymore.
So your site will be completely down. To resolve this we need tomcat cluster with load balance.
If any server goes down, the whole network will not go down. That is, if Load balancer receives any error message form a server it will redirect the request to the any of the remaining server. The servers in clusters will be selected using round robin method.
How to resolve the communication problem?
There is a way, By implementing load balancer you can resolve this issue. This load balancer will be added in from of all tomcat clusters. Here all the request and response will go through the load balancer.
So load balance will take care about dispatching the requests between tomcats which are in cluster.
There are Two effective load balancers are available.
  1. Apache http server
  2. Nginx
In this tutorial we are going to see Load balance using Apache Http Server.
What are the Software’s are required for Apache load balance?
  1. Apache Http server
  2. mod_jk
  3. tomcat cluster (many instances).
First install all the required softwares. In two different way you can install these, by using source code or apt-get. Using source code is always good because you can install only required things and at user specified place.
Here we are going to see how to build and install using source code.
How to install Apache Http Server?
First download the httpd server source code from then extract it and install.
You cant directly install using source code, you have to build to make Make file to install.
Steps to build:-
./configure –prefix=/usr/local/apache –enable-rewrite=shared–enable-proxy=shared
sudo make install

–prefix is the location where it is going to install in the machine.
–enable-rewrite and –enable-proxy options to enable these module in shared mode. These modules are not needed now. but we need in future for rewrite theURLbefore handover to next chain of servers and load-balancing support.
How to install mod_jk?
By using Apache http server alone we cant do load balance. Because by default Apache wont support AJP connector (ajp protocall).
So we need a linker to connect Apache web server and Ajp protocall. So here mod_jk going to be only a linker between appach and ajp protocall.
download the mod_jk connector module from it and follow the given steps to build.
cd tomcat-connectors-1.2.37-src/native
./configure –with-apxs=/usr/local/apache/bin/apxs
sudo make install
here–with-apxsoption to specify where apxs module is located. so weneedto giveApachehttpd server location.
nowmod_jk.sofiles is created on modules directory in apache installed location (/usr/local/apache/modules)
How to configure mod_jk connector?
You have to do two thinks to configure mod_jk.
  1. Creating file
  2. Configure mod_jk connector in apache httpd.conf file.
How to create file?
Get into apache/conf folder and create file.
Its a configuration file to make communicate between tomcat and ajp protocal using mod_jk connector.
sample :-




type is ajp13, port is ajp port and host is tomcat instance.
Here tomcat name and balancer name is user defined names. So you can give any name whatever you want.
balancer type is lb. Its a special type for load balance.

Status property is optional. It is used to get the load balance status and logs.
All requests are received by balancer and those are handled and dispached by balance_workers.
Because balance_workers have all tomcat instances names which are participating in load balance.
Actually to forward the requests to the balance_workers we need to configure httpd.conf file.
Do refer the following line if you need more details abt file.
How to configure httpd.conf file?
File path of httpd.conf is apache/conf/httpd.conf
Just add these following lines at the end of httpd.conf file.
JkWorkersFile conf/
JkLogFile logs/mod_jk.log
JkLogLevel emerg
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat “%w %V %T”

JkMount / balancer
JkMount /status stat

* JkWorkersFile will represent the location of the file.
* LoadModule is used to load module.
The last 2 lines are important.
JkMount /status stat means any request to match the /status then that request forward to stat worker. Its status type worker. so its shows status of load balancer.
JkMount /* balancer this line matches all the request, so all request is forward to balancer worker. In balancer worker is uses the round robin algorithm to distribute the request to other tomcat instances.
What about tomcat instance failover?
There is a default property called worker.maintain.
Every 60 seconds worker.maintain will send request to update the status of the all tomcat instances. If worker.maintain didnt not receive respone from any instance it will update the status as fail.
This process will be continued. If it receives any response from instance’s it will update the status to ok.
Thats it. Try it.. if u get any doubt please do let me know.