Friday, November 30, 2012

Tomcat Clustering - Apache WebServer

Hello There,

Today early morning I tried something new, which I never tried to do it but always wanted to, had a new assignment to learn tomcat (http://tomcat.apache.org) and clustering tomcat, finding and adding up a little to the statement - "pressure makes man perfect", I thought what is there to learn.?, bunch of configuration files and batch files, Google it and you will be settled to live with this guy.!, did the same and was able to see my tomcat running with hosted applications (example application) now I need to bring another exceptionally performing mini software to work with him called Apache (http://www.apache.org/) to work as frontend help desk for the processor behind. Let's see how are we able to establish this.!!

Steps To Follow
-------------------

1) Download and Install both Softwares

  • Apache - http://mirror.reverse.net/pub/apache/httpd/binaries/win32/httpd-2.0.64-win32-x86-no_ssl.msi
    • Or you can manually go the site http://httpd.apache.org/download.cgi and download desired versions from desired mirrors.
    • Need Admin Permission to Install.
    • I tried it on httpd-2.0.64-win32-x86-no_ssl.msi
    • I have used localhost for the hostname and admin@localhost as email id
  • Apache Tomcat - http://tomcat.apache.org/download-70.cgi
    • Download any version that suits your requirement
    • I tried it on apache-tomcat-7.0.32
    • Just Unzip file and you are done with installation.
  •  Verify the setup details and test the installation by testing the default application after starting them.
  • For More Information refer : 
    • http://httpd.apache.org/docs/2.0/platform/windows.html
    • http://tomcat.apache.org/tomcat-5.5-doc/setup.html#Windows
  •  Done

2) Configure Tomcat for Clustering - ( Vertical Clustering) 

For Clustering we need to have at least 2 instances of the server instances and within that we need to have load balancing established, for creating another instance of tomcat just copy the tomcat folder, paste in the same directory and rename it.

C:\SandBox\WebServers\TomcatCluster>dir
 Volume in drive C is OSDisk
 Volume Serial Number is E604-B574

 Directory of C:\SandBox\WebServers\TomcatCluster

23/11/2012  17:58    <DIR>          .
23/11/2012  17:58    <DIR>          ..
23/11/2012  17:58    <DIR>          apache-tomcat-1
23/11/2012  17:58    <DIR>          apache-tomcat-2
               0 File(s)              0 bytes
               4 Dir(s)  175,658,151,936 bytes free


few of the configurations we need to change to enable the tomcat to work for other server ( to establish cluster)

modify the server.xml file present in apache-tomcat-x/conf/ as below for both instances.

apache-tomcat-1
---------------------
1) Change the port number as below.

<Server port="8105" shutdown="SHUTDOWN">

2) Change the Port Number as below for HTTP Protocol.


<Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />


3) Enable the AJP Protocol if it is disabled and change the port number 

<Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />

4) Enable the Cluster feature for Tomcat by disabling the comments as below.

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

5) Open the Application for which you would like to test the Clustering Feature and add the <distributable/> within <web-app></web-app> tags..............

I have tested it for examples webapp under <CATALINA_HOME>/webapps/ (CATALINA_HOME) is another name for Tomcat Home - you need to specify this in system's environment variables.

Example : Go to C:\SandBox\WebServers\TomcatCluster\apache-tomcat-1\webapps\examples\WEB-INF


<servlet-mapping>
      <servlet-name>wsSnake</servlet-name>
      <url-pattern>/websocket/snake</url-pattern>
    </servlet-mapping>
    <distributable/>
</web-app>

Now repeat the steps for apache-tomcat-2 instance.

apache-tomcat-2


1) Change the port number as below.

<Server port="8205" shutdown="SHUTDOWN">

2) Change the Port Number as below for HTTP Protocol.


<Connector port="8082" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />


3) Enable the AJP Protocol if it is disabled and change the port number 

<Connector port="8209" protocol="AJP/1.3" redirectPort="8443" />

4) Enable the Cluster feature for Tomcat by disabling the comments as below.

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

5) Open the Application for which you would like to test the Clustering Feature and add the <distributable/> within <web-app></web-app> tags..............

I have tested it for examples webapp under <CATALINA_HOME>/webapps/ (CATALINA_HOME) is another name for Tomcat Home - you need to specify this in system's environment variables.

Example : Go to C:\SandBox\WebServers\TomcatCluster\apache-tomcat-2\webapps\examples\WEB-INF


<servlet-mapping>
      <servlet-name>wsSnake</servlet-name>
      <url-pattern>/websocket/snake</url-pattern>
    </servlet-mapping>
    <distributable/>
</web-app>



Restart both Tomcat Instances.

3) Configure Apache for Clustering with Tomcat

Hope you have already installed Apache WebServer and tested on http://localhost/

Configuring Apache Server involves below steps :


  • Downloading mod_jk library
    • Please download the mod_jk.so file from link
    • once you get the zip file, unzip it and copy the mod_jk.so file to Apache2/modules folder.
  • Configure mod_jk.so file in Apache Configuration
    • Create mod_jk.conf file with following entries
# Load mod_jk module

# Update this path to match your modules location
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties
# Update this path to match your conf directory location
JkWorkersFile C:/SandBox/WebServers/apache2.0.64/Apache2/conf/workers.properties

# Where to put jk logs
# Update this path to match your logs directory location
JkLogFile C:/SandBox/WebServers/apache2.0.64/Apache2/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 indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

# Send everything for context /examples to worker ajp13
JkMount /examples loadbalancer
JkMount /examples/* loadbalancer
    • Add following entry in httpd.conf file.
include C:/SandBox/WebServers/apache2.0.64/Apache2/conf/mod_jk.conf
  • Create worker.properties for handling requests from Apache and copy the below content to the file.
    • You can specify the tomcat home for first tomcat instance
# Define 1 real worker named ajp13
workers.tomcat_home=C:/SandBox/WebServers/TomcatCluster/apache-tomcat-1
workers.java_home=%JAVA_HOME%
worker.list=node1,node2,loadbalancer

# Set properties for worker named ajp13 to use ajp13 protocol,
# and run on port 8009

worker.node1.port=8109
worker.node1.host=localhost
worker.node1.type=ajp13
worker.node1.lbfactor=1

worker.node2.port=8209
worker.node2.host=localhost
worker.node2.type=ajp13
worker.node2.lbfactor=1

worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=node1,node2
worker.loadbalancer.sticky_session=1
  • Copy the file worker.properties file to Apache2/conf directory.
  • Restart Apache Server.
3) Access the URL : http://localhost/examples or deploy any session replication enabled application to test whether session is getting replicated or the clustering load balancer is working properly or not.














Additionally if you see the mod_jk.log file, following entries are noticed during this operation.


[Fri Nov 30 15:02:16 2012]loadbalancer localhost 2.102120
[Fri Nov 30 17:07:40 2012]loadbalancer localhost 0.442025
[Fri Nov 30 17:07:40 2012][8720:6044] [info] ajp_send_request::jk_ajp_common.c (1619): (node2) all endpoints are disconnected, detected by connect check (1), cping (0), send (0)
[Fri Nov 30 17:07:40 2012]loadbalancer localhost 0.235014
[Fri Nov 30 17:14:07 2012]loadbalancer localhost 0.101006


Hope I haven't missed anything yet.!!

Regards
Shailesh Dyade



No comments: