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

JavaMelody: Monitoring the Performance of Tomcat Application Server

Javamelody is an opensource (LGPL) application to monitor Java or Java EE application servers in QA and production environments. JavaMelody is mainly based on statistics of requests and on evolution charts. (Extract from the Javamelody home page) It allows to improve applications in QA and production Give facts about the average response times and number of executions Make decisions when trends are bad, before problems become too serious Optimize based on the more limiting response times Find the root causes of response times Verify the real improvement after optimization It includes summary charts showing the evolution over time of the following indicators: Number of executions, mean execution times and percentage of errors of http requests, sql requests, jsp pages or methods of business façades (if EJB3, Spring or Guice) Java memory Java CPU Number of user sessions Number of jdbc connections These charts can be viewed on the current day, week, month, year or cu

Configuring URL Encoding on Tomcat Application Server

Application servers may have different settings for character encodings. We strongly recommend UTF-8 where possible. By default, Tomcat uses ISO-8859-1 character encoding when decoding URLs received from a browser. This can cause problems when Confluence's encoding is UTF-8, and you are using international characters in the names of attachments or pages. To configure the URL encoding in Tomcat: Step 1 - Configure connector: Edit conf/server.xml and find the line where the Coyote HTTP Connector is defined. It will look something like this, possibly with more parameters: < Connector port = "8080" /> Add a URIEncoding="UTF-8" property to the connector: < Connector port = "8080" protocol = "HTTP/1.1"      connectionTimeout = "20000"      redirectPort = "8443"      URIEncoding = "UTF-8" /> If you are using mod_jk you should appl

Set Up Nginx Load Balancing

About Load Balancing Loadbalancing is a useful mechanism to distribute incoming traffic around several capable Virtual Private servers. By apportioning the processing mechanism to several machines, redundancy is provided to the application -- ensuring fault tolerance and heightened stability. The Round Robin algorithm for load balancing sends visitors to one of a set of IPs. At its most basic level Round Robin, which is fairly easy to implement, distributes server load without implementing considering more nuanced factors like server response time and the visitors’ geographic region. Setup The steps in this tutorial require the user to have root privileges on your VPS. Prior to setting up nginx loadbalancing, you should have nginx installed on your VPS. You can install it quickly with apt-get: sudo apt-get install nginx Upstream Module In order to set up a round robin load balancer, we will need to use the nginx upstream module. We will incorporate the conf