Load Balance/Cluster Your Tomcat (or any) Server

I began my adventure with the need to ensure a lot of uptime for a particular web service that was running on a tomcat server. I investigated a lot of different avenues and, eventually, ended up figuring out a lot on my own.

Most of the information on the web about clustering tomcat servers tells you that you need to use the mod_jk apache module. This is not true and, in some cases, not what you really want. One such page is the following:

http://www.easywayserver.com/implementation-tomcat-clustering.htm

Which is not a bad way to go. I started doing that and then a colleague of mine suggested some information.

Requirements:
* Apache 2.2.x
* mod_proxy
* mod_rewrite
* a web application container (tomcat) or j2ee application server (glassfish)

Setup:
At the time, I was using glassfish application server and so I decided to use it for my application server. Add the following to make Apache load balance to your Glassfish cluster:

RewriteEngine On
ProxyRequests Off

Order deny,allow
Allow from all

SetHandler balancer-manager

BalancerMember http://10.1.1.2:38080 route=w1
BalancerMember http://10.1.1.3:38080 route=w2
BalancerMember http://10.1.1.4:38080 route=w3

Order allow,deny
Allow from all

ProxyPass /clusterjsp balancer://mycluster/clusterjsp lbmethod=byrequests

And for Glassfish you have the option to set up a cluster. See the cluster article for more information on how to setup a glassfish cluster.

You now have both Apache and the app server cluster configured so it is time to check if the load-balancer is online. Browse to http://localhost/balancer-manager (where localhost is your Apache server) and verify if the load-balancer sees the 3 workers and if the routes are set. Now the last remaining step is to deploy a clusterable application.

The Glassfish distribution contains an example application that you can deploy on a cluster. See GLASSFISH_ROOT/samples/quickstart/clusterjsp. Go to the administration console of the administration server and select your cluster. Click on the Applications tab and click on Deploy, select the clusterjsp.ear file that you found in the Glassfish samples and make sure Availability is checked (this is for the fail-over). Then click OK. Then browse to http://localhost/clusterjsp and see your work in action! For other application servers, a similar application or even a simple .jsp page deployed to a container would suffice.

You now are using Apache load-balancing, mod_proxy with a Glassfish cluster together.

There are a couple of things to keep in mind:

1. You must make sure that you load the proxy load balancer. For instance, in your httpd.conf file there is a section where the apache modules are loaded. Make sure you have a LoadModule for the load balancer:

LoadModule proxy_balancer_module <file path to your apache server>/modules/mod_proxy_balancer.so

2. When you are configuring your cluster, consider where you are putting these rules. They have to be included in the startup. So, either in the httpd.conf or maybe a rewrite rules file.

3. The “Order” parameter does not allow spaces after the comma.

4. Pay attention to what you have put in your <Location> tag. This is the context you will use to access the load balancer management console. More on that later….

5. The balancer defined in the <Proxy> tag. This is referenced in the ProxyPass parameter. Also in the ProxyPass parameter is the context of the application that needs to be load balanced. In the example, /clusterjsp is the context of the application that is to be load balanced. There is a bunch of information about how to do different configurations for load balancer. A decent place to start for more information is here:

http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html

6. You define the nodes in your cluster in the <Proxy> section. The nodes are defined by the BalancerMember entries. These directives take the same parameters as the ProxyPass directive. So you can assign the route for your nodes to take. For instance:

BalancerMember http://localhost:8080 route=myroute1
BalancerMember http://localhost:8081 route=myroute2
BalancerMember http://localhost:8082 route=myroute3

7. Once you have this all set up and working (you have restarted your apache server), the next thing to do is to take a look at the load balancer console. If you have set this up on your local machine, and we are still referring to the example, the URL to this console would be this:

http://localhost/balancer-manager

This will load an apache page where you can enable/disable nodes in the cluster and set the failover node for a particular node. Click the link for one of the nodes. A new section of the page opens to allow you to enter information about the load factor (how much load should that node get), the route (can be anything you want), and the route redirect (which node to go to if this node becomes unavailable). The route redirect is the name of the node. So, if you call your first node “node1”, then the other nodes can refer to this node as “node1” in the redirect route box.

Also on this page are statistics about errors, usage, status of a particular node. Some useful information about the health of your cluster.

Finally, if you want to disable a particular node (for some maintenance) you can do so from the load balancer console.

There is lots more stuff that you can do with load balancer. If you are the least bit interested, make sure you read the Apache documentation.

http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: