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

তৈদুছড়া ঝর্ণা : খাগড়াছড়ি

(সংগৃহীত) তৈদুছড়া ঝর্ণা:তৈদুছড়া এলাকায় খুব কাছাকাছিই বড় ঝর্ণা আছে তিনটি।এছাড়া এক ঝর্ণা থেকে আরেক ঝর্ণায় যাওয়ার পথে আছে বেশ কয়েকটি ছোট ঝর্ণা এবং যাওয়ার ঝিরিপথটাও অসাধারণ সুন্দর। ঝর্ণা দেখার সাথে সাথে পাহাড়ের ঢাল বেড়ে ট্রেকিং- এর থ্রিলটাও পাওয়া যাবে এখানে। পুরো এলাকাতেই জুম চাষ করা হয়,তাই পাহাড়ের ঢালে ঢালে দেখা পাবেন অসংখ্য সুন্দর সুন্দর জুম ঘরের। যেভাবে যাবেন: ঢাকা থেকে শান্তি পরিবহনে সরাসরি আসতে পারেন দিঘীনালায়,অথবা শ্যামলী, এস আলম সেন্টমার্টিন পরিবহনে খাগড়াছড়ি এসে খাগড়াছড়ি থেকে সিএনজ ি তে আসতে পারবেন দিঘীনালা।খাগড়াছড়ি -দিঘীনালা সময় লাগবে ৪০ মিনিট। ভাড়া ৬০টাকা। দিঘীনালার জামতলি থেকে হেঁটে যেতে হবে ঝর্ণায়,দিঘীনালা থেকে জামতলি যেতে পারবেন অটোরিকশায়।ঝর্ণায় যেতে ২-৩ ঘন্টা লাগে,সকালে রওনা দিলে বিকেলের মধ্যেই ফিরে আসা যায়। থাকবেন কোথায়:দিঘীনালায় ফ্যামিলি নিয়ে থাকার মত হোটেল রয়েছে বাস স্টেশনের পাশেই। বাস স্টেশন থেকে তিন মিনিট হাঁটলে বোয়ালখালি বাজারেও পাবেন থাকার হোটেল। সাথে যা যা নিতে হবে:শুকনো খাবার নিয়ে নিতে হবে সাথে,পানিও নিয়ে নেবেন।দড়ি নেবেন,কাজে লাগবে।অবশ্যাই ভালো গ্রিপ আ...

How to automatically rotate catalina.out daily

How to automatically rotate catalina.out daily or when it becomes bigger than 5M 1. Create this file /etc/logrotate.d/tomcat   2. Copy the following contents into the above file /usr/tomcat/default/logs/catalina.out {    copytruncate    daily    rotate 7    compress    missingok     size  5M   }   About the above configuration: Make sure that the path  /var/log/tomcat/catalina.out  above is adjusted to point to your tomcat’s catalina.out daily  -  rotates the catalina.out  daily rotate  – keeps at most  7  log files compress –  compresses   the rotated files size  – rotates if the size of catalina.out is bigger than  5M copytruncate  – truncates the original log file in place after creating a copy, instead of moving the old log file and optionally creatin...

How to batch INSERT and UPDATE statements with Hibernate

Courtesy: VLAD MIHALCEA Introduction JDBC  has long been offering support for  DML  statement batching. By default, all statements are sent one after the other, each one in a separate network round-trip. Batching allows us to send multiple statements in one-shot, saving unnecessary socket stream flushing. Hibernate hides the database statements behind a transactional  write-behind abstraction layer . An intermediate layer allows us to hide the JDBC batching semantics from the persistence layer logic. This way, we can change the JDBC batching strategy without altering the data access code. Configuring Hibernate to support JDBC batching is not as easy as it should be, so I’m going to explain everything you need to do in order to make it work. Testing time We’ll start with the following entity model: The  Post  has a one-to-many association with the  Comment  entity: 1 2 3 4 5 @OneToMany (    ...