Monday, February 27, 2017

HTTP Server mod_jk configuration to load balance Tomcat instances in Hybris

The following configuration can be applicable to any web application that is running in tomcat proxied with Apache http server

Assumption is that already ssl is enabled in Apache refer the blog  HTTP Server Proxy Configuration for Hybris
Download the mod_jk module for the corresponding OS from http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/
Put the mod_jk.so file in the modules directory of
Create a file named workers.properties in the conf directory of apache with the following contents
worker.list=loadbalancer
worker.loadbalancer.port=8009
worker.loadbalancer.host=electronics.local
worker.loadbalancer.type=ajp13
If you are having two servers that you need to load balance then the contents of workers.properties is given below
worker.list=loadbalancer,status
# Define Node1
# modify the host as your host IP or DNS name.
worker.node1.port=8009
worker.node1.host=node1.electronics.local
worker.node1.type=ajp13
worker.node1.ping_mode=A
worker.node1.lbfactor=1
# Define Node2
# modify the host as your host IP or DNS name.
worker.node2.port=8009
worker.node2.host=node2.electronics.local
worker.node2.type=ajp13
worker.node2.ping_mode=A
worker.node2.lbfactor=1
# Load-balancing behavior
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2
worker.loadbalancer.sticky_session=1
# Status worker for managing load balancer
worker.status.type=status

Add the following to the httpd.conf file
LoadModule jk_module modules/mod_jk.so
<IfModule jk_module>
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogStampFormat "[%b %d %Y - %H:%M:%S] "
JkRequestLogFormat "%w %V %T"
JkLogLevel info
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

<Location /*/WEB-INF/*>
deny from all
</Location>

JkMount / loadbalancer
JkMount /* loadbalancer
</IfModule>
 Add the following to the node that is there in httpd-ssl.conf file
JkMount /* loadbalancer
Open the tomcat's server.xml and add/change the AJP connector port with the following
<Connector protocol="AJP/1.3" port="8009" proxyPort="80" redirectPort="443" useIPVHosts="${tomcat.ajp.useipv}" connectionTimeout="10000" keepAliveTimeout="10000" /> 
<Engine name="Catalina" defaultHost="localhost" jvmRoute="node1">
jvmRoute="node1" will add node1 to the jsessionid so that the request can be correctly identified from which node it is coming from
Restart http server
Restart tomcat
Open the following urls(assumption is that electronics.local is already mapped to the host file)

No comments:

Post a Comment