Why 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
Lets 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 Web server(apache). Based on mod_jk configuration, apache will send 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:
http://apache.petsads.us//tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz
For Windows:
http://apache.petsads.us//tomcat/tomcat-connectors/jk/binaries/windows/
2. 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.
1.1 Extract tomcat-connectors-1.2.37-src.tar.gz.
"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.
3. Now Load that module in apache's httpd.conf file using below string. You can copy that string at bottom of httpd.conf file.
#
# Load mod_jk
#
LoadModule jk_module modules/mod_jk.so
4. Now you need to specify workers.properties file path in httpd.conf file, so that apache can read configuration of both tomcat applications.
JkWorkersFile conf/workers.properties
5. you can specify log file location, log level, log format too using below string in httpd.conf file.
JkLogFile logs/mod_jk.log
JkLogLevel warn
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
6. Now create workers.properties file using below content.
Here
- 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 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.
7. 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
8. Once all above mentioned configuration done, you can restart apache web server and test.
For Sticky Session testing:
If your application works on 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.
Before:
<Engine name="Catalina" defaultHost="localhost" >
After:
<Engine name="Catalina" defaultHost="localhost"
jvmRoute="jvm1">
In tomcat app server 1 you can use jvm1 and in second tomcat you can use jvm2.