Using Apache as Software Load Balancer
Here, I will be explaining on how 'mod_jk + Apache' can be used together to work as a software Load Balancer.
Note: I am assuming that whoever is reading this has basic knowledge of Apache and its use and implementation.
- mod_jk.conf
- ssl.conf [For https content]
- workers.properties
I am excluding "httpd.conf " file here. You can leave it with original settings . I am also excluding "uriworkermap.properties" file because instead of defining the context path or the name of your war file here, you can simply run you application as ROOT.war. Meaning, rename your "xyz.war" to "ROOT.war" and run your tomcat server. After renaming your application as ROOT.war ,you don't have to write the context path while accessing your application. And you have excluded one file ("uriworkermap.properties") while setting up Apache as Software Load Balancer. Isn't this a good deal :).
E.g:
How to access your application if the name of your war file is:
xyz.war --> http://localhost:8080/xyz
ROOT.war --> http://localhost:8080 [No need to explicitly mention the context path ; port 8080 is the port on which your tomcat server is running]
E.g:
How to access your application if the name of your war file is:
xyz.war --> http://localhost:8080/xyz
ROOT.war --> http://localhost:8080 [No need to explicitly mention the context path ; port 8080 is the port on which your tomcat server is running]
Anyhow , I will write down the settings of "uriworkermap.properties" too, incase anybody still needs that.
# worker configuration file
# Mount the Servlet context to the ajp13 worker
/xyz/=loadbalancer
/xyz/*=loadbalancer
|
Below are the content of the 3 files mentioned above which should be used for setting up Apache to make it work as Software Load Balancer.
1) mod_jk.conf
# Load mod_jk module
LoadModule jk_module modules/mod_jk.so
#It's better to define Listen port and comment out that in httpd.conf file
Listen 80
<VirtualHost *:80>
#ServerName 10.34.24.54
DocumentRoot /var/www/html
RewriteEngine On
RewriteLogLevel 1
RewriteLog logs/rewrite.log
JkMount /jkmanager/* jkstatus
JkMount /* loadbalancer
JkUnMount /siteimages/* loadbalancer # Static images served from here
JkUnMount /robots.txt loadbalancer
</VirtualHost>
# Where to find workers.properties
JkWorkersFile conf.d/workers.properties
# Where to put jk logs
JkLogFile logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat "[%a %b %d %H: %M: %S %Y]"
# JkOptions indicates to send SSK KEY SIZE
JkOptions +ForwardKeySize -ForwardDirectories
# JkRequestLogFormat
JkRequestLogFormat "%w %V %T"
# Mount your applications
#JkMount /* loadbalancer
# You can use external file for mount points.
# It will be checked for updates each 60 seconds.
# The format of the file is: /url=worker
# /examples/*=loadbalancer
#JkMountFile conf.d/uriworkermap.properties
# Add shared memory.
# This directive is present with 1.2.10 and
# later versions of mod_jk, and is needed for
# for load balancing to work properly
JkShmFile logs/jk.shm
# Add jkstatus for managing runtime data
#Add the jkstatus mount point
# Add jkstatus for managing runtime data
#<Location /jkstatus/>
# JkMount status
# JkUnMount status
# Order deny,allow
# Allow from all
#</Location>
# Add the jkstatus mount point
#JkMount /jkmanager/* jkstatus
#Enable the JK manager access from localhost only
<Location /jkmanager/>
JkMount jkstatus
Order deny,allow
Allow from all
</Location>
<IfModule mod_expires.c>
<FilesMatch "\.(ico|pdf|flv|jpe?g|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault "access plus 2 year"
</FilesMatch>
</IfModule>
#SetEnvIf Request_URI "^/check.txt$" dontlog
SetEnvIf Request_URI "(\.gif$|\.jpg$|\.JPG$|\.Jpg$|\.png$|\.js$|\.css$|\.woff$|\.ttf$|\.eot$|\.ico$|server-status$|jkmanager)" dontlog
LogFormat "@%{X-Forwarded-For}i@ %h %l %u %t %T \"%r\" %>s %b %T \"%{Referer}i\" \"%{User-Agent}i\" \"%{JSESSIONID}C\" \"%{HS_ID}C\" " custom
CustomLog "|/usr/local/sbin/cronolog -S /var/log/httpd/access.log --period='1 days' /var/log/httpd/access.%Y%m%d.log" custom env=!dontlog
|
2) ssl.conf
Not going to explain this in detail. Just take care of your certificates path. Get a self signed certificate if you are setting this up in dev/preprod environment.
And don't forget to add this line in this file too.
JkMount /* loadbalancer
|
3 ) workers.properties
# Define list of workers that will be used for mapping requests
worker.list=loadbalancer,jkstatus
# Define the first member worker
worker.jvmnode1.port=8010
#8010 is the ajp port and not http port
worker.jvmnode1.host=10.34.24.54
worker.jvmnode1.type=ajp13
worker.jvmnode1.lbfactor=1
#LBFactor is very crucial parameter. It decides load distribution. In this scenario it's 50:50
# Define the second member worker
worker.jvmnode2.port=8010
worker.jvmnode2.host=10.34.24.55
worker.jvmnode2.type=ajp13
worker.jvmnode2.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=jvmnode1, jvmnode2
worker.loadbalancer.sticky_session=1
worker.jkstatus.type=status
|