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.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:
- Install Apache httpd Web Server
- Install mod_jk connector
- Configure JK Connector
- Configure Apache httpd server, apply loadbalancer concepts
Step One—Install Apache
We can install Apache web server in two ways:- Binary module
- From Source
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.
$ 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,
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
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.
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
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
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
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"
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
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.
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!!
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
Post a Comment