Thursday, January 22, 2015

Load balancer Apache Tomcat with apache httpd and mod_jk



Load balancer  Apache Tomcat with apache httpd and mod_jk


A load balancer is a worker that does not directly communicate with Tomcat. Instead it is responsible for the management of several "real" workers, called members or sub workers of the load balancer.
This management includes:
·      Instantiating the workers in the web server.
·      Using the worker's load-balancing factor, perform weighted load balancing (distributing load according to defined strengths of the targets).
·      Keeping requests belonging to the same session executing on the same Tomcat (session stickyness).
·      Identifying failed Tomcat workers, suspending requests to them and instead falling-back on other workers managed by the load balancer.
·      Providing status and load metrics for the load balancer itself and all members via the status worker interface.
·      Allowing to dynamically reconfigure load-balancing via the status worker interface.
Start to install Load Balancer with apache Tomcat and apache Httpd Server
Install Apache from source
            Download Apache 2.4.7 from http://httpd.apache.org/download.cgi
            Download APR and APR UTIL from
http://apr.apache.org/download.cgi
            untar apache
$ tar zxvf httpd-2.4.7.tar.gz
            similarly untar apr and apr-utils and copy it to apahce/srclib folder
$ mv apr-1.5.0 httpd-2.4.7/srclib/apr
$ mv apr-util-1.5.3 httpd-2.4.7/srclib/apr-util
            We plan to install apache in the /usr/local folder with all modules and DSO enabled.
# ./configure --prefix=/usr/local/apache --enable-mods-shared=all  --enable-so --with-included-apr
# make
# make install

 Building mod_jk

mod_jk doesn’t come bundled with the HTTPD distribution, so you’ll need to download it from the link given in the Introduction to this article and build it from source.
I downloaded tomcat-connectors-1.2.37-src.tar.gz to /usr/local/src and extracted as follows:

$ tar xzf  tomcat-connectors-1.2.37-src.tar.gz
$ cd  tomcat-connectors-1.2.37-src
Next, run configure – ensuring that the correct path to apxs is specified for your configuration.
# ./configure –with-apxs=/usr/local/apache/bin/apxs

If not errors next compilation
# make
# make install
edit /usr/local/apache/conf/httpd.conf
add 2 lines :
LoadModule jk_module modules/mod_jk.so
Include  conf/mod_jk.conf
create file in /usr/local/apache/conf/ mod_jk.conf
# vim /usr/local/apache/conf/mod_jk.conf
# Where to find workers.properties
JkWorkersFile /usr/local/apache/conf/workers.properties
# location of log file
JkLogFile /usr/local/apache/logs/mod-jk.log
# log level
JkLogLevel info
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
#Send everything for context /test to worker ajp13
JkMount /examples/* loadbalancer
JkMount /examples  loadbalancer
create file # vim /usr/local/apache/conf/workers.properties
worker.list=loadbalancer
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.lbfactor=1
worker.worker2.port=8008
worker.worker2.host=localhost
worker.worker2.type=ajp13
worker.worker2.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=worker1,worker2
  • balance_workers is a comma separated list of names of the member workers of the load balancer. These workers are typically of type ajp13. The member workers do not need to appear in the worker.list property themselves, adding the load balancer to it suffices.
  • sticky_session specifies whether requests with SESSION ID's should be routed back to the same Tomcat instance that created the session. You can set sticky_session to False when Tomcat is using a session manager which can share session data across multiple instances of Tomcat - or if your application is stateless. By default sticky_session is set to True.
  • lbfactor can be added to each member worker to configure individual strengths for the members. A higher lbfactor will lead to more requests being balanced to that worker. The factors must be given by integers and the load will be distributed proportional to the factors given. Higher factors lead to more requests.
  update your Tomcat server .xml files replacing
<Engine name= "Catalina" defaultHost="localhost">
with
<Engine name= "Catalina" defaultHost="localhost" jvmRoute=<worker>>
 you should restart your apache
# ./usr/local/apache/bin/apachectl restart




No comments:

Post a Comment