Migrate to Cloud

Apache-Tomcat Load Balanced Persistent Session Setup on Amazon EC2

Although Tomcat is a good option for heavy java applications, it gives a poor performance under high pressure.The best way to solve this problem is to set up an Apache-Tomcat Load Balanced on your Amazon EC2 environment. In this case you will have more than one parallel running tomcat instances and each will be able to share the part of the traffic.

The following documentation will give you a clear picture about how to deploy an Apache-Tomcat Load Balanced setup on Amazon EC2 in such a way that all the sensitive datas are stored in their highly reliable storage solutions like EBS and S3 so that you wont lose your data even if your instance crashes. You can see in the below figure that all the application files, static files and images are stored on EBS and are regularly backed up to s3 by taking snapshots. This can be done by creating softlinks for document root of apache and tomcat ( webapps folder ) or you can change the data directory and run it from EBS. To see how to run the database from EBS , Please check our article on how to  run “PostgreSQL _from_EBS

Lets start with the apache-tomcat environment with load-balanced on the local system.

1. Basic Apache-Tomcat Setup

i) Install tomcat onto the web server.

ii) Install the mod_jk module and verify its presence under apache/modules directory.
Enter the below content to the apache configuration file.

LoadModule jk_module modules/mod_jk-apache-2.2.4.so

iii) Now create the worker file named jkworkers.properties under the apache/conf/ location.  The worker file refers to the workers  to whom the apache’s power is to be transferred.

Enter the below content to it:

worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

iv) Specify the worker file in the apache’s configuration file as given belove

JkShmFile “/etc/httpd/logs/mod_jk.shm”
JkWorkersFile “/etc/httpd/conf/jkworkers.properties”
JkLogFile “/etc/httpd/logs/mod_jk.log”
# 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”
JkMount /jsp-examples1 worker1
JkMount /jsp-examples1/* worker1

This tells the apache where to look for the definition of its worker and tells them whenever there is a request for the page named jsp-examples1, the power should hand over  to the tomcat instance represented by worker1.

Now restart the apache and the tomcat. Open the browser to access  http://localhost/jsp-examples1/.

2. Load Balanced set-up on a single web server

We can run more than one tomcat on the single instance. You just need is to follow the steps given below.

i) Copy the tomcat folder to a new location named tomcat2. And edit the its server.xml file as given below

<Connector port=”8080″ maxHttpHeaderSize=”8192″
maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
enableLookups=”false” redirectPort=”8443″ acceptCount=”100″
connectionTimeout=”20000″ disableUploadTimeout=”true” />

to

<Connector port=”8019″ maxHttpHeaderSize=”8192″
maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
enableLookups=”false” redirectPort=”8443″ acceptCount=”100″
connectionTimeout=”20000″ disableUploadTimeout=”true” />

and,

<Connectort port=”8009” enbaleLookups=”false” redirectPort=”8443” protocol=”AJP/1.3” />

to

<Connectort port=”8010” enbaleLookups=”false” redirectPort=”8443” protocol=”AJP/1.3” />

Also change the port number from

<Server port=”8006″ shutdown=”SHUTDOWN”>

to

<Server port=”different port number but greater than 1024″ shutdown=”SHUTDOWN”>

ii) Now add the below contents to the jkworkers file

worker.list=worker2
worker.worker2.type=ajp13
worker.worker2.host=localhost
worker.worker2.port=8010

iii) Open the apache configuration file and add the below contents too.

JkMount /jsp-examples2 worker2
JkMount /jsp-examples2/* worker2

Start the tomcat2 and open the browser to access the tomcat home page through link http://localhost:8019. Then try the link http://localhost/jsp-examples2/

3. LoadBalanced  Setup with cluster of tomcat instances

We have setup two tomcats on a single instance. Now lets see how we set these tomcats to perform load balancing.

i) Change the jkworkers.properties file as given below

worker.list=workers
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8080
worker.worker1.lbfactor=1
worker.worker2.type=ajp13
worker.worker2.host=localhost
worker.worker2.port=8019
worker.worker2.lbfactor=1
worker.workers.type=ajp13
worker.workers.balance_workers=worker1, worker2
worker.workers.lbfactor=1

ii) Comment out the JkMount specified before in the apache configuration file and enter the below content to it.

JkMount /jsp-examples workers
JkMount /jsp-examples/* workers

Try to access the link http://localhost/jsp-examples in your browser. (Please make sure the folder named jsp-examples are present under both the tomcat1 and tomcat2. If not just create it.)
The page will be provided by either of the tomcat1 or tomcat2. This can be verified by editing any one of the file under jsp-examples folder.

Now lets run these tomcats on different instances say instance1 and instance2, and the apache with mod_jk running on different instance. Here we have total of three instances.

To perform the load balancing in this setup, you just need is to change the worker file present on the web server as given below:

worker.list=workers
worker.worker1.type=ajp13
worker.worker1.host=”ip of instance1”
worker.worker1.port=8080
worker.worker1.lbfactor=1
worker.worker2.type=ajp13
worker.worker2.host=”ip of instance2”
worker.worker2.port=8080
worker.worker2.lbfactor=1
worker.workers.type=ajp13
worker.workers.balance_workers=worker1, worker2
worker.workers.lbfactor=1

Make sure that java and tomcat is installed on both the tomcat instances and also check for the folder named js-examples under their webapps folder. Now try to access http://localhost/js-examples
Here the page will be provided by either of the tomcat instances.
When we are using more than one workers as mentioned in the above workers file, we  must set the domain property for each so that the mod_jk sends the request to the respective tomcat instances.

Just edit the server.xml files of each tomcat as shown below:
For the tomcat running on first instance

<Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”jvm1″>

and for the second tomcat running in the other instance

<Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”jvm2″>

Now edit the workers file in the apache server and add the line given below:

worker.worker1.domain=jvm1
worker.worker2.domain=jvm2

So that the worker file will look like:

worker.list=balancer
worker.worker1.domain=jvm1
worker.worker1.type=ajp13
worker.worker1.host=”ip of instance1”
worker.worker1.port=8080
worker.worker1.lbfactor=1
worker.worker2.domain=jvm2
worker.worker2.type=ajp13
worker.worker2.host=”ip of instance2”
worker.worker2.port=8080
worker.worker2.lbfactor=1
worker.balancer.type=ajp13
worker.balancer.balance_workers=worker1, worker2
worker.balancer.lbfactor=1
worker.balancer.sticky_session=1

Here we have setup a sticky session with load balance. If any of the two instances gets crashed then the request is passed to the third tomcat which is working properly. If the tomcats get back to the working state then the request will be accepted by the any one of the working tomcats.
The sticky session on the above worker file checks whether the requests with the session ID’s should be routed back to the same tomcat. If the sticky session is set true or 1, the session is set sticky.

4. Persistent Session Manager with a shared file store

Presistent session is generally used for uninterrupted flow of session when tomcat servers get crashed or restart properly. To setup this session manager first we have to comment out the “<cluster>” element in each instance. Then add the below content to the content.xml file.

<Context>
<Manager className=”org.apache.catalina.session.PersistesntManager”>< Store className-“org.apache.catalina.session.FileStore”>
directory=”cluster shared directory”   <!– the path of the directory shared by the tomcat cluster –>
/>
</Manager>
</Context>

Associated Links

  • Amazon.com
  • Java enterprise platform
  • Web services
  • Cloud infrastructure
  • Java platform
  • Configuration files
  • Apache Tomcat
  • Apache HTTP Server
  • Amazon Elastic Compute Cloud
  • .properties
  • Mod jk

  • Exit mobile version