Apache Httpd Load Balancer Configuration using mod_jk

Why wew need Apache Load Balancer

We can use Apache Load Balancer module (mod_jk) to optimizes resource use, maximizes throughput, minimizes response time, and avoids overload as well as for auto failover.

How it can be utilized

Let us assume that you have two tomcat web applications running on two different servers. Now you want to make your application highly available and also want to distribute traffic across both tomcat application servers. So here we can configure one web server(apache) with mod_jk module, which will be act as a frontend server and two tomcat application servers will act as backend server.

Client request for your application will come to a Web server(apache). Based on mod_jk configuration, Apache will send the request to both tomcat applications. Cool!!!!

How to configure mod_jk on Apache Web server.

1. First download mod_jk source from below mentioned link. Please choose package according to your server architecture(32bit or x64).

For Linux:

For Windows:

 If you are using windows then you need to simply copy mod_jk.so file into your apache’s module directory.
If you are using linux then you will have to make mod_jk.so file using below steps.
Extract tomcat-connectors-1.2.37-src.tar.gz.
1.1  #”tar -zxvf tomcat-connectors-1.2.37-src.tar.gz”

1.2 Now configure it using below command.
#./configure –with-apxs=/usr/sbin/apxs

1.3 Now run make and then make install.
“make && make install”
Note: If you are getting any error, then please check if “httpd-devel” package is installed or not.

1.4 If above 3 commands run successfully, then it would have created mod_jk.so file into /etc/httpd/modules/ directory.

2. Now Load that module in apache’s httpd.conf file using string. You can copy that string at bottom of httpd.conf file.
# Load mod_jk
LoadModule jk_module modules/mod_jk.so

3. Now you need to specify workers.properties file path in httpd.conf file, so that apache can read the configuration of both tomcat applications.

JkWorkersFile conf/workers.properties

4. you can specify log file location, log level, log format too using string in httpd.conf file.

JkLogFile logs/mod_jk.log
JkLogLevel warn
JkLogStampFormat “[%a %b %d %H:%M:%S %Y]”

5. Now create workers.properties file using the content.

– worker.jvm1.port is ajp port of tomcat application server configured in server.xml file,
– worker.jvm1.host is IP address of tomcat application server,
– worker.jvm1.type is the ajp protocol version,
– worker.jvm1.lbfactor is to assign weight to tomcat application,
– worker.jvm1.max_packet_size is to specify maximum packet size.
Please note that here we have used jvm1 and jvm2. For tomcat server 1, we have used jvm1 and for another tomcat server, we have used jvm2.worker.loadbalancer.balance_workers is used to mention the name of tomcat application server’s worker name.
worker.loadbalancer.sticky_session – This will enable sticky session.
worker.loadbalancer.method – This will set load balancing method.

6. Now you need to mount Load Balancer in httpd.conf file. You can use below string in httpd.conf file to mount load balancer.

JkMount /* loadbalancer

If you want to exclude any directory then it can be specified before JkMount tag as shown below.

JkUnMount /balancer-manager loadbalancer
JkMount /* loadbalancer

7. Once all above-mentioned configuration is done, you can restart apache web server and test.

For Sticky Session testing:

If your application works on the session, then you may need to configure sticky session at Apache Load Balancer. In workers.properties we have already set worker.loadbalancer.sticky_session to 1. But we need configuration at tomcat too. In both application server edit tomcat/conf/server.xml file and change property as shown below.
<Engine name=”Catalina” defaultHost=”localhost” >
<Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”jvm1″>
In tomcat app server 1 you can use jvm1 and in second tomcat you can use jvm2.