Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

High availability Tomcat

Connect Tomcat servers to Apache and to each other to keep your site running

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

You've written a Web application that runs happily in production on Tomcat, probably on port 80. Some early problems might include complaints from your security team because your server runs as a root facing the outside world and excessive traffic—traffic beyond what a single server can deal with.

A far more likely problem, however, is that you will need to issue new releases of your application. You may even need to upgrade your version of Tomcat or the operating system. These tasks will require you to restart Tomcat. And whenever you do that, your site will become unavailable.

To prevent such an outcome, you move your main server to port 8081, set up a secondary server identical to the primary one on 8082, and some form of dispatcher on port 80. The dispatcher directs traffic to the secondary server while you upgrade the primary one.

Options for the dispatcher are:

  • iptables: Part of the Linux kernel, primarily used for enforcing firewall rules. They allow you to map all traffic from one port to another port. For example, this command maps all traffic from port 80 to the primary server on 8081:

     iptables -t nat -A PREROUTING -p tcp -d lo --dport 80 -j DNAT --to 127.0.0.1:8081 
    


    This option is only possible on a Linux system with iptables compiled into the kernel.

  • Apache with mod_proxy: Set up Apache as a reverse proxy. To switch servers, update the configuration file (httpd.conf or apache2.conf) and ask Apache to reload it. This is a good solution if you only need to switch servers. A potential downside is the difficulty inherent in automating the switching, since only the root user can ask Apache to reload its configuration file.
  • Apache with mod_jk2: This option allows you to cluster Tomcat servers together, implement load-balancing and failover strategies, and dynamically add and remove members from the cluster. It makes for a great setup, even if you simply want to switch traffic from a primary to a secondary server. Throughout the rest of this article, I cover how to set up this option.


Note: For more details on iptables and mod_proxy, see Resources.

Step-by-step setup

If you don't already have Tomcat 5.0.x, download a recent copy and unpack it twice. I put mine in /usr/local/tomat1 and /usr/local/tomcat2.

Edit the server.xml file (/usr/local/tomcat1/conf/server.xml or /usr/local/tomcat2/conf/server.xml) on each Tomcat by changing the non-SSL (Secure Socket Layer) Coyote HTTP/1.1 Connector element for the first server to:

  <Connector port="8081" ... />



and the second server to:

  <Connector port="8082" ... />



Also on the second server, change the port in the server entry at the top. For example:

 <Server port="8006" ...>



Start both servers and check if you can get the default homepage. Download and install Apache 2 and make sure it runs.

Install mod_jk2

A binary build or packaged build of mod_jk2 is available for many platforms. A binary or packaged build is the easiest way to install mod_jk2—if you have one of these, skip to the next section ("Apache mod_jk2 Configuration"). Otherwise, you need to build mod_jk2 from source, which is still quite easy.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

Tomcat is the most famous server used while developing java applBy Anonymous on October 30, 2009, 9:55 amTomcat is the most famous server used while developing java applications..

Reply | Read entire comment

You may even need to upgrade your version of Tomcat or the operating system...By Anonymous on September 24, 2009, 1:37 amYou may even need to upgrade your version of Tomcat or the operating system. These tasks will require you to restart Tomcat. And whenever you do that, your site...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources