Hi In this document we will showing you how to setup Apache tomcat load balancing using modjk in Ubuntu Server.

Prerequisites
1. Ubuntu server with root access
2. Install apache2 web server and make it running
3. Install tomcat server. if you don’t know how to setup tomcat server verify this link for the setup.

tomcat 1 location: /opt/tomcat1
tomcat 2 location: /opt/tomcat2

 

wget https://community.jboss.org/servlet/JiveServlet/download/588259-27006/clusterjsp.war

Configuration

Step 1: Make sure that you have achieved all the prerequesties.

Step 2: Create two copies of apache tomcat server because we are testing loadbalancer where both the tomcat servers are in same server.

Step 3: Goto tomcat server 1 and download the following sample application in webapps folder

cd /opt/tomcat1/webapps

wget https://community.jboss.org/servlet/JiveServlet/download/588259-27006/clusterjsp.war

Step 4: Do this tomcat server 2 as well.

cd /opt/tomcat2/webapps
wget https://community.jboss.org/servlet/JiveServlet/download/588259-27006/clusterjsp.war

Step 5: Install apache mod_jk module for ubuntu server by executing the following command.

apt-get install libapache2-mod-jk

Step 6: Change the port number for tomcat1 and tomcat2 otherwise the ports will conflict each other .

For server1( no need to change the port, only enable AJP protocol for loadbalancing.

cd /opt/tomcat1/conf

Open the conf file server.xml

vi server.xml

 

 

<Connector port=”8081″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ />

enable AJP port for load balancing

 

<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”9443″ />

enable jvmroute for loadbbalancing using mod_jk

<Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”ajp13″>

 

 

For server 2

cd /opt/tomcat2/conf

Open the conf file server.xml

vi server.xml

 

<Connector port=”8082″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ />

 

enable AJP port for tomcat2

<Connector port=”8010″ protocol=”AJP/1.3″ redirectPort=”8443″ />

 

enable jvmroute for loadbbalancing using mod_jk

<Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”ajp23″>

 

where ajp13, and ajp23 are works for load balancing

 

Step 7: add the following line worker.properties file

vi /etc/libapache2-mod-jk/workers.properties

 

#
worker.list=loadbalancer,status
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13

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

worker.ajp13.lbfactor=1
worker.ajp23.lbfactor=1

 

worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=ajp13,ajp23
worker.status.type=status

 

The above configuration defines two virtual workers, and two actual workers, which map to my Tomcat servers. The virtual workers “status” and “loadbalancer” are defined in the worker.list property, because I’m going to refer to them later in my apache configuration.

Second, I’ve defined workers for each of my servers, using the values from the AJP connectors in the server.xml from earlier. I’ve also included two optional properties for these workers, “lbfactor”. The higher the number of this property, the more preference mod_jk will give that worker when load balancing. If I had given the servers lbfactors of 1 and 3, I would find that the round-robin load balancing would prefer one server over the other with a 3:1 ratio.

 

step 7: In apache default config/virtual host file, add the following lines

vi /etc/apache2/sites-available/000-default.conf

 

<IfModule mod_jk.c>

JkMount /status status
JkMount /* loadbalancer
</IfModule>

 

Step 8: restart the apache2 service

service apache2 restart

Step 9: start both the tomcats

cd /opt/tomcat1/bin
sh start.sh

 

cd /opt/tomcat2/bin
sh start.sh

step 10: Now if you check the access logs of both the tomcat both the tomcat servers serves the client request

http://localhost/clusterjsp

Step 11: Check loadbalancing status from

http://localhost/status

 

If you refresh the clusterjsp page a few times, you should see the effect in the above section. The important columns are “V” and “Acc”which are showing the worker.[workername].lbvalue and worker.[workername].electedproperties respectively.

The lbvalue (“V”) is the number that the load balancer uses to decide which server to route the next request to. Whichever has the smallest number will get the next request. It’s not a count, although it will look likeone if you’ve set the lbfactor to 1 on all workers. If you increase the lbfactor on (for example) ajp23 to 2 in my case, then you will see that the lbvalue jumps up by two at a time on the ajp13worker. This means that more requests go to ajp23, since its lbvalue is more frequently lower.

 

© 2018, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points
Categories: applicationLinux

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

0 Comments

Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.