Skip to main content

Tomcat Clustering: Loadbalancing with mod_jk and Apache


Tomcat

We already see how to run multiple tomcat instance in a single machine in my previous blog (http://kmlsarwar.blogspot.com/2014/08/multiple-tomcat-instances-on-single.html).

In this post we will see how to configure load balancing with mod_jk and Apache on Ubuntu.

Suppose we have two hosts, node1 and node2. Node1 runs an Apache and a Tomcat instance. On node2 we’ve got another Tomcat.

Loadbalancing with apache mod_jk




A browser will connect to the host that’s running the Apache. Since the load on a single server running a web application can be pretty severe, we’re going to share the burden of serving servlets with multiple hosts (in our case two hosts). And we’re going to make mod_jk to do that for us.



Steps to Implement Virtual Host/Loadbalancer Concept in this Scenario:
  1. Install Apache httpd Web Server 
  2. Install mod_jk connector 
  3. Configure JK Connector 
  4. Configure Apache httpd server, apply loadbalancer concepts

Step One—Install Apache

We can install Apache web server in two ways:
  1. Binary module
  2. From Source
We can install Apache httpd server from distribution package manager (using apt-get). Or we can download the source code and then compile and install.
We use the second option. First download the httpd server source code from here . then extract it and install

$ wget http://mirrors.ispros.com.bd/apache//httpd/httpd-2.4.10.tar.gz

After the download completes, untar the file.
tar xvzf httpd-2.4.10.tar.gz
 
And install with this command
 
$ ./configure --prefix=/usr/local/apache --enable-rewrite=shared 
--enable-proxy=shared 
$ make
$ sudo make install 
 
Here –prefix option to mention where the location we going to install Apache httpd server.

–enable-rewrite and –enable-proxy options to enable these module in shared mode. 

These modules are not needed now. but we used in future for rewrite the URL before handover to next chain of servers and load-balancing support.

Note: you may need download and install the some additional pakages before install apache httpd like apr, apr-utill, pcre.

So, now apache installed in /usr/local/apache folder.


Step Two—Install mod_jk connector


Now Apache httpd server is ready. we need to add ajp support to server.
download the mod_jk connector module from here.

Extract it and install it
 $ wget http://mirrors.ispros.com.bd/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.40- src.tar.gz
 
After the download completes, untar the file.
$ tar xvzf tomcat-connectors-1.2.40-src.tar.gz
 
And install with this command 

$ cd native
$ ./configure    --with-apxs=/usr/local/apache/bin/apxs

$ make
$ sudo make install

Here  --with-apxs option to specify where apxs module is located. so we need to give Apache httpd server location.
Now mod_jk.so files is created on modules directory in apache installed location (/usr/local/apache/modules).


Step Three—Install Configure Jk connector


This step have 2 sub step
  •   Create workers.properties file
  •    Load and configure the JK connector module in apache httpd.conf file

Create workers.properties file 
mod_jk connector is ready. but this connector is works based on configuration file. so we need to create configuration file called workers.properties file
this file syntax is

key=value pair,
Here we define the workers. i.e all department tomcat hosts IP address and ajp port for corresponding tomcat.
Here entry format is look like
worker.<name>.property=<value>
for example 
worker.<name>.type=ajp13
worker.<name>.port=<ajp port>
worker.<name>.host=<tomcat ip addr>
worker.list=<name>


Here, worker.list key have all workers name separated by comma.
type = here type of the worker. we use ajp13 version 
port= we specify the ajp port (not http port ) of that server
host= IP address or host name of tomcat server


Step Four—Setup Load balancer

For simplicity purpose i going to run 3 tomcat instances in single machine(we can run on dedicated machine also) with Apache httpd web server. and single web application is deployed in all tomcat instances.


Here we use mod_jk module as the load balancer. by default its use the round robin algorithm to distribute the requests. now we need to configure the workers.properties file like
worker.list=tomcat1,tomcat2,tomcat3

worker.tomcat1.type=ajp13
worker.tomcat1.port=8009  
worker.tomcat1.host=localhost

worker.tomcat2.type=ajp13
worker.tomcat2.port=8010  
worker.tomcat2.host=localhost

worker.tomcat3.type=ajp13  
worker.tomcat3.port=8011  
worker.tomcat3.host=localhost
Here I configure the 3 tomcat instances in workers.properties file. here type is ajp13 and port is ajp port (not http connector port) and host is IP address of tomcat instance machine.

There are couple of special workers we need add into workers.properties file.
First one is add load balancer worker, here the name is  balancer (You can put any name).

worker.balancer.type=lb worker.balancer.balance_workers=tomcat1,tomcat2,tomcat3 
here this worker type is lb, ie load balancer. its special type provide by load balancer. and another property is balance_workers to specify all tomcat instances like tomcat1,tomcat2,tomcat3 (comma separated)

Second one, add the status worker, Its optional. but from this worker we can get statistical of load balancer.


worker.stat.type=status

Here we use special type status. Now we modify the worker.list property.


worker.list=balancer,stat
So from outside there are 2 workers are visible (balancer and stat). so all request comes to balancer. then balancer worker manage all tomcat instances.

Complete workers.properties file
worker.list=balancer,stat

worker.tomcat1.type=ajp13
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.lbfactor=10

worker.tomcat2.type=ajp13
worker.tomcat2.port=8010
worker.tomcat2.host=localhost
worker.tomcat2.lbfactor=10

worker.tomcat3.type=ajp13
worker.tomcat3.port=8011
worker.tomcat3.host=localhost
worker.tomcat3.lbfactor=10


worker.balancer.type=lb
worker.balancer.balance_workers=tomcat1,tomcat2,tomcat3

worker.stat.type=status
Now workers.properties confiuration is finished. now we need to send the all request to balancer worker. So modify the httpd.conf file of Apache httpd server. Add this folowing line end of the httpd.conf file.

LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties

JkLogFile     logs/mod_jk.log
JkLogLevel    emerg
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions     +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat  "%w %V %T %p %q %r %v %U"
  
JkMount  /*  balancer
JkMount  /status  stat

The above code is just boiler plate code. 1st line load the mod_jk module, 2nd line to specified the worker file (workers.properties file). all others are just logging purpose. 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. 


Step Five—Result

Apache is now installed and configured. However, it is not yet activated.
First start your tomcat instances using start script.
The final step is to activate Apache by running its startup script:

$ ./apachectl start
Once that runs, apache is up and ready on port 80.
You can visually verify that Tomcat is working by accessing your server page at your_IP_address/localhost.

If you shutdown the tomcat instance 1, load balance will connect to tomcat instance 2. Enjoy tomcat clustering!!

Comments

Popular posts from this blog

How to install play framework

Prerequisites To run the Play framework, you need JDK 6 or later . If you are using MacOS, Java is built-in. If you are using Linux, make sure to use either the Sun JDK or OpenJDK (and not gcj, which is the default Java command on many Linux distros). If you are using Windows, just download and install the latest JDK package. Note, Java 7 pre update 9 on MacOS has a bug that causes problems with futures and iteratees, including making large file uploads hang. If using Java 7 on MacOS, make sure you are using the latest version. Be sure to have the java and javac commands in the current path (you can check this by typing java -version and javac -version at the shell prompt). Download the binary package Download the latest Play standalone distribution and extract the archive to a location where you have both read and write access. (Running play writes some files to directories within the archive, so don’t install to /opt , /usr/local or anywhere else you’d need s...

Java HotSpot VM Settings

The following JVM parameters are recommended: -Djava.awt.headless=true  -Dfile.encoding=UTF-8  -server  -Xms512m  -Xmx1024m  -XX:NewSize=256m  -XX:MaxNewSize=256m  -XX:PermSize=256m  -XX:MaxPermSize=256m  -XX:+DisableExplicitGC -Djava.awt.headless=true Headless mode is a system configuration in which the display device, keyboard, or mouse is lacking. Sounds unexpected, but actually you can perform different operations in this mode, even with graphic data. -server The JDK includes two flavors of the VM -- a client-side offering, and a VM tuned for server applications. These two solutions share the Java HotSpot runtime environment code base, but use different compilers that are suited to the distinctly unique performance characteristics of clients and servers. These differences include the compilation inlining policy and heap defaults. Although the Server and the Client VMs are similar, the Serv...

How to install Oracle JDK on Ubuntu from repository

This package provides Oracle Java JDK 7 (which includes Java JDK, JRE and the Java browser plugin). However, you can't only install Oracle JRE - the PPA only provides the full Oracle JDK7 package. To install: sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-jdk7-installer And to uninstall: sudo apt-get remove oracle-jdk7-installer And to verify your installation: java -version java version "1.7.0_51" Java(TM) SE Runtime Environment (build 1.7.0_51-b13) Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode) And to verify the web browser installation: http://java.com/en/download/installed.jsp Verified Java Version Congratulations! You have the recommended Java installed (1.7.0_51). And to update: If for some reason, the Java version in use is not 1.7.0, you can try to run the following command: sudo update-java-alternatives -s java-7-oracle To Install and Set in environment variable: To...