In my previous post I’ve talked about setting up 2 instances of tomcat on the same machine. This post is the second one of my series about load balancing instances of tomcats on a single machine as it’s a continuation of the first one. it talks essentially about how the real load balancing can be setup using mod_jk. Again in this post we are going to referrence our tomcat servers timo and pumba that we setup during the first post.
Apache mod_jk is a module for apache but maintained separately. In case you don’t have apache installed already, then install it with the following command:
$ sudo apt-get install apache2
We now need to install the module mod_jk .Unlike mod_proxy which is newer and comes bundled and maintained with apache2, mod_jk is installed differently. It’s module name can differ from distribution to distribution but, on Ubuntu we use the following to install it:
$ sudo apt-get install libapache2-mod-jk
Let’s pause here. Most of the time, modules come bundled with apache2 but not enabled. In Ubuntu they are found under the folder
They are enabled with a command a2enmod. Its usage is simple:
$ sudo a2enmod name-of-the-module
Once the command is successful it creates s symbolic link of the
/etc/apache2/mods-available/module-name.conf # /etc/apache2/mods-available/module-name.load
to the folder
That’s generally what happens but in our case , we won’t need to call the a2enmod command because as soon as libapache2-mod-jk is installed it’s enabled as well so it has both jk.conf and jk.load in the available as well as the enabled folder(but as a symbolic link)
Let’s edit jk.conf in the /etc/apache2/mods-available folder.
$ nano /etc/apache2/mods-available
Find and change this line
to look like the line below
Comment out the
<Location /jk-manager> JkMount jk-status Order deny,allow Deny from all Allow from 127.0.0.1 # you can add other ips separated by a space </Location>
In the location tag, we are instructing apache2 to do use the worder jk-status when the url /jk-manager white list of IPs where connections should be allowed from.
Let’s create our own workers.properties file, a much simpler one. We will put it inside apache2 folder: /etc/apache2
$ sudo nano /etc/apache2/workers.properties
worker.list=loadbalancer,jk-status # configure list of workers to run #setup worker.timo.port=8009 # ajp port on timo server worker.timo.host=localhost worker.timo.type=ajp13 worker.timo.lbfactor=2 #setup pumba worker.pumba.port=8010 # ajp port on pumba server worker.pumba.host=localhost worker.pumba.type=ajp13 worker.pumba.lbfactor=1 #setup the load-balancer worker.loadbalancer.type=lb worker.loadbalancer.balance_workers=timo,pumba worker.loadbalancer.sticky_session=True #worker.loadbalancer.sticky_session_force=True # Status worker for managing load balancer worker.jk-status.type=status # setting type of jk-status' worker.
To be able to let the 2 servers timo and pumba look like a distributed system where users would not have to call them using 2 differents ports etc, we will setup a virtualhost on apache2 so that it will interface them, and then load balance them.
Let’s configure the virtualhost. We are going to create it Ubuntu’s way as in the /etc/apache2/sites-available folder.The file name is ourtimoandpumba.org it’s also the name of the virtualhost that we will have to enable later
$ nano /etc/apache2/sites-available/ourtimoandpumba.org
#/etc/apache2/sites-available/ourtimoandpumba.org <VirtualHost *:80> ServerAdmin [email protected] ServerAlias live.ourtimoandpumba.org ServerName ourtimoandpumba.org JkMount /* loadbalancer JkMount /jk-manager/* jk-status Loglevel warn ErrorLog /var/log/apache2/ourtimoandpumba_error_log CustomLog /var/log/apache2/ourtimoandpumba_access_log combined </VirtualHost>
After saving with crtl +X combination we need to enable the virtualhost unless it won’t be available. This supposed there is a domain name of the name ourtimoandpumba.org which is already pointed to the IP of the our physical server. As opposed to a2enmod to enable a module, we use a2ensite for virtualhost enabling.
$ sudo a2ensite ourtimoandpumba.org
$ sudo service apache2 restart # a reload as in service apache2 reload should be enough though
Voila! you have a load balancer managing your “cluster” of tomcat instances