Tomcat Cluster Mod Proxy

tomcat-cluster

On this page, we focus on setting up the cluster to act as a load balancer, without session replication. For session replication, refer to tomcat-cluster-session-replication

What are the basic steps?

  1. Download and install Tomcat on different servers or ports
  2. Download and install Apache HTTPD
  3. Configure mod_proxy

How to download and install Tomcat on different servers or ports?

In most situations, you would be deploying the nodes on physically separate machines, but in this example, we will set them up on a single machine, but on different ports. This allows us to easily test this configuration.

  1. Download Tomcat .zip distribution
  2. We'll use a folder to install all this stuff in. Let's say it's C:\cluster
  3. Unzip the Tomcat distro twice, into two folders - C:\cluster\tomcat-node-1 and C:\cluster\tomcat-node-2
  4. Start up each of the nodes using the bin/startup.bat scripts. Ensure that they start. If they don't start, you may need to point tomcat to appropriate JDK installation directory on your machine.

Next, we need to look under the installation path for the conf/server.xml file and edit it. Look for a line containing "Connector" and a line containing "Engine". On the line containing "Connector", the "port" attribute is important. On the first node, we will keep the default value. On the second node, we will need to change the value for the port attribute to a different value. On the line containing "Engine", we have to add the "jvmRoute" attribute. This configures the name of the node in the cluster. The value for this jvmRoute attribute must be unique across the cluster. On the "Connector" line, we specify protocol="AJP/1.3", so if there is another "Connector" line with protocol="HTTP/1.1", we remove or comment out that Connector.

How to download and install Apache HTTPD?

  1. Download and install Apache HTTPD Server. Use the custom option to install it into C:\cluster\apache-2.2
  2. Now open **C:\cluster\apache2.2\conf\httpd.conf in your text editor and enable the necessary mod_proxy modules.
  3. Finally, go to the end of the file and add the following:
<Proxy balancer://testcluster stickysession=JSESSIONID>
    BalancerMember ajp://127.0.0.1:8009 min=10 max=100 route=node1 loadfactor=1
    BalancerMember ajp://127.0.0.1:8019 min=20 max=200 route=node2 loadfactor=1
</Proxy>
ProxyPass /examples balancer://testcluster/examples

The "route" setting which must match the names of the "jvmRoutes" in the Tomcat server.xml for each node. This in conjunction with the "stickysession" setting is key for a Tomcat cluster, as this configures the session management. It tells mod_proxy to look for the node's route in the given session cookie to determine which node that session is using. This allows all requests from a given client to go to the node which is holding the session state for the client. The ProxyPass line configures the actual URL from Apache to the load balanced cluster. You may want this to be "/" e.g. "ProxyPass /balancer://testcluster/" In our case we're just configuring the Tomcat /examples application for our test.

What is the purpose of the Apache Balancer Manager?

mod_proxy has an additional "balancer manager" component, which provides a nice web interface to manage the cluster. It is worthwhile setting this up if you want to remotely administer or monitor the cluster.

How can we set up the balancer manager?

Add to httpd.conf:

<Location /balancer-manager>
    SetHandler balancer-manager
    AuthType Basic
    AuthName "Balancer Manager"
    AuthUserFile "C:/cluster/apache2.2/conf/.htpasswd"
    Require valid-user
</Location>

This configures the balancer manager at http://localhost/balancer-manager. We need to create a password file to secure it. At the command prompt, you can use:

c:\cluster\apache2.2\bin\htpasswd -c c:\cluster\apache2.2\conf\.htpasswd admin

Then set a password when prompted. Restart your Apache web server, and go to http://localhost/balancer-manager. You should be prompted for a username and password as you set before, and see the balancer manager tool.

References:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License