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.
Enter the below content to it:
iv) Specify the worker file in the apache’s configuration file as given belove
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
Also change the port number from
ii) Now add the below contents to the jkworkers file
iii) Open the apache configuration file and add the below contents too.
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.
ii) Comment out the JkMount specified before in the apache configuration file and enter the below content to it.
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:
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
and for the second tomcat running in the other instance
Now edit the workers file in the apache server and add the line given below:
So that the worker file will look like:
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.