2013. november 17., vasárnap

Tomcat 7 cluster with load balancer part 1.

It has been a long time since my last post. I decided to write my future posts in English to get a bigger audience. (sorry for the language mistakes :))

In this post I'm going to explain how to configure tomcat clusters with Apache httpd loadbalancer. I couldn't really find any good material about the topic that's why I have decided to write this post. I'm not going to explain the load balancing concepts deeply, this post is going to be useful for lazy people like me who need a quick configuration to test the applications in clustered environment.

For the configuration we will need:
Tomcat 7
Apache HTTP server
mod_jk connector

First of all we should configure the Apache HTTP server with mod_jk modul. Just simply download the appropriate mod_jk version (my version is 2.4) unzip it and copy the content to the modules folder.
In the apache conf/ folder we will need two extra configuration files mod-jk.conf and workers.properties.

The mod-jk.conf should contain:

 
LoadModule    jk_module  modules/mod_jk.so 
 
JkWorkersFile conf/workers.properties 
 
JkLogFile     logs/mod_jk.log 
JkLogLevel    INFO 
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " 
JkOptions     +ForwardKeySize +ForwardURICompat -ForwardDirectories 
JkRequestLogFormat     "%w %V %T" 
 
The first line is loading the mod_jk module, the second is loading the workers from the workers.properties configuration file. The rest is just logging.

The next config file is the workers.properties. In this file we need the following configuration:
 
worker.list=balancer,status  

worker.tomcat1.type=ajp13 

worker.tomcat1.port=8009 

worker.tomcat1.host=192.168.1.15 


worker.tomcat2.type=ajp13 

worker.tomcat2.port=8009 

worker.tomcat2.host=192.168.1.9 

worker.balancer.type=lb 

worker.balancer.balance_workers=tomcat1,tomcat2 

worker.status.type=status 

As you can see I configured 2 tomcat workers, a status worker and the loadbalancer worker. You can add more tomcat workers if you want. To make it work on your computer you should use your IP's and ports. Don't forget that the 8009 is the ajp port and not the HTTP.

The last part of the apache configuration is to inject the mod-jk configuration to the httpd.conf configuration file. You can do it by adding the following lines to the httpd.conf file.

Add a new virtual host to the virtual hosts section:

<VirtualHost *:80>
    JkMount  /jkstatus  status
    JkMount  /*  balancer
</VirtualHost>

and insert the following line to the end of the httpd.conf file
 
Include conf/mod-jk.conf

To test the configuration open the localhost/jkstatus page.