Skip to main content

Load balancing using Apache, Tomcat, mod_jk


How Apache httpd web server communicate to Tomcat server
               Before we going to detail about how communication happen between httpd server and tomcat.
How many ports are bind when we start single tomcat?
  • shutdown port
  • http connector port
  • https connector port (optional)
  • ajp port

The port configuration are stored in $CATALINA_HOME/conf/server.xml file. we can change the ports when its necessary.

Steps to Implement Virtual Host Concept in this Scenario:

  1. Install Apache httpd Web Server
  1. Install mod_jk connector
  1. Configure JK Connector
  1. Configure Apache httpd server apply virtual host concepts

Prerequisite : We already installed Tomcat in different departments and deployed the application and works fine.


Install Apache httpd web server:
        We can install Apache web server in two ways.
                         - Binary module
                         - From Source
We can install Apache httpd server from distribution package manager (either apt-get or yum). Or we can download the source code and then compile and install.


          Second option: First download the httpd server source code from here . then extract it and install


./configure --prefix=/usr/local/apache    --enable-rewrite=shared  --enable-proxy=sharedmakesudo make install


Install mod_jk connector
 Now Apache httpd server is ready. we need to add ajp support to server.
download the mod_jk connector module from here. extract it and install it

cd native
./configure    --with-apxs=/usr/local/apache/bin/apxs

make
sudo make install

here  --with-apxs option to specify where apxs module is located. so we need to give Apache httpd server location.

now mod_jk.so files is created on modules directory in apache installed location (/usr/local/apache/modules)

Configure mod_jk connector
This step have 2 sub step
  •   Create workers.properties file
  •    Load and configure the JK connector module in apache httpd.conf file
Create workers.properties file 
mod_jk connector is ready. but this connector is works based on configuration file. so we need to create configuration file called workers.properties file

this file syntax is key=value pair,
here we define the workers. i.e all department tomcat hosts IP address and ajp port for corresponding tomcat.

here entry format is look like
worker..property =
for example 
worker..type=ajp13
worker..port=
worker..host=
worker.list=

here 
worker.list key have all workers name separated by comma.
type = here type of the worker. we use ajp13 version 
port= we specify the ajp port (not http port ) of that server
host= IP address or host name of tomcat server

workers.properties

# Refer to http://kbase.redhat.com/faq/docs/DOC-15866
# for instructions on setting up mod_jk.
#
# Configuration directives valid for mod_jk version 1.2.28 and later.
#

# Define list of workers that will be used for mapping requests
worker.list=loadbalancer,status

# Define template worker
worker.template.port=8009
worker.template.type=ajp13
worker.template.ping_mode=A
worker.template.reply_timeout=10000
worker.template.socket_connect_timeout=10000
# Not necessary to specify connection_pool_timeout with worker mpm 
worker.template.connection_pool_timeout=600

# Referencing the template worker properties makes the workers.properties
# shorter and more concise.
worker.node1.reference=worker.template
worker.node1.host=192.168.1.2
worker.node1.lbfactor=50

worker.node2.reference=worker.template
worker.node2.host=192.168.1.3
worker.node2.lbfactor=100
worker.loadbalancer.type=lb worker.loadbalancer.balance_workers=node1,node2 worker.loadbalancer.sticky_session=True worker.status.type=status



-----------
Configure httpd.conf
Apache httpd server is installed. mod_jk module is installed and workers.properties file is created. but these 3 are isolated. we put together,we need to configure the httpd server. find the conf/httpd.conf file in Apache installed location and add these following entries to it
LoadModule    jk_module  modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile     logs/mod_jk.log
JkLogLevel    emerg
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions     +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat     "%w %V %T"
here LoadModule    - Load mod_jk shared module to Apache httpd server (enable the mod_jk module)JkWorkersFile - Specify the mod_jk module file location. 
Specify the workers.properties file location. all others are logging system of mod_jk. Its boilerplate code just copy and paste.
......
Delegate httpd to Tomcat
now we inform to Apache httpd server how delegate the request to corresponding server.


JkMount  /status/*  stat
JkMount  /*  loadbalancer
here 
JkMount - specify the if URL have /* pattern then that request delegate to loadbalancer worker. that worker IP address and port is specified in workers.properties file.

Comments

Popular posts from this blog

Nginx startup script

#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig:   - 85 15 # description:  NGINX is an HTTP(S) server, HTTP(S) reverse \ #               proxy and IMAP/POP3 proxy server # processname: nginx # config:      /etc/nginx/nginx.conf # config:      /etc/sysconfig/nginx # pidfile:     /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() {    # make required directories    user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed...

Tomcat NIO Connector and How to Configure It

In  HTTP 1.1 , all connections between the browser and the server are considered persistent unless declared otherwise. Persistence, in this context, means to use a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair. In tomcat, the default HTTP connector is blocking and follows a one thread per connection model. This means that in order to serve 100 concurrent users, it requires 100 active threads. We end up wasting resources (the thread) because connections may not be used heavily, but just enough to avoid a timeout. Opposed to this is the relatively new NIO or non blocking connector. This connector has a couple of poller threads used to keep the connection alive for all connected users while worker threads are called whenever data (a new HTTP request) is available. This model leads to a much better sharing of resources (threads) and a larger number of concurrent users can ...

Java + HTTPS: Unable to Find Valid Certification Path to Requested Target

Run  InstallCert.java , with your hostname and https port, and press “ 1 ” when ask for input. It will add your “localhost” as a trusted keystore, and generate a file named “ jssecacerts “. C : \ > java InstallCert localhost : 8443 Loading KeyStore C : \Program Files\Java\jre6\lib\security\cacerts . . . Opening connection to localhost : 8443 . . . Starting SSL handshake . . . javax . net . ssl . SSLHandshakeException : sun . security . validator . ValidatorException : PKIX path building failed : sun . security . provider . certpath . SunCertPathBuilderException : unable to find valid certification path to requested target at com . sun . net . ssl . internal . ssl . Alerts . getSSLException ( Unknown Source ) at com . sun . net . ssl . internal . ssl . SSLSocketImpl . fatal ( Unknown Source ) at com . sun . net . ssl . internal . ssl . Handshaker . fatalSE ( Unknown Source ) at com . sun . net . ssl . internal . ssl . Handshaker...