Adsense 1

Sunday, March 22, 2009

Tomcat Failover Setup with Apache2+modjk

Tomcat works fairly well under heavy loads. For critical services we will need some type of backup/failover mechanism. This tutorial is for those who want to configure apache to failover between two tomcat instances. When the first tomcat service fails, apache-httpd automatically routes the requests to second tomcat.

This tutorial shows how to configure failover load balancing between two tomcat instances in the same server (i have also shown how to do this for tomcat instances in two different servers). For this i am assuming you know how to install multiple tomcat instances in the same machine. This is only slightly different from load balancing between two tomcat instances as you see below in the sample workers.properties file.


#
# workers.properties
#
# Below Entry Specifies the load balance factor when used with a load balancing worker.
# Note: ----> lbfactor must be > 0,  Low lbfactor means less work done by the worker.
# worker.tomcat1.lbfactor=100

# In Unix, we use forward slashes:
ps=/

# list the workers by name

# Workers that are member of lb should not be in the list.
worker.list = failover

# ------------------------
# Tomcat1 Primary 
# ------------------------
worker.tomcat1.port=8009        # You can specify your tomcat instance's ajp port here
worker.tomcat1.host=localhost # Specify any hostname/IP here if tomcat is running in a remote server
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat1.connection_pool_timeout=600
worker.tomcat1.socket_timeout=600
worker.tomcat1.redirect=tomcat2

# ------------------------
# Tomcat2 Failover
# ------------------------
worker.tomcat2.port=9009        # You can specify your tomcat instance's ajp port here
worker.tomcat2.host=localhost # Specify any hostname/IP here if tomcat is running in a remote server
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.tomcat2.connection_pool_timeout=600
worker.tomcat2.socket_timeout=600
worker.tomcat2.activation=disabled # Requests are sent to the disabled worker only if first worker fails

# ------------------------
# Failover worker
# ------------------------
#
# The loadbalancer (type lb) worker performs weighted round-robin
# load balancing with sticky sessions.
# Note:
# ----> If a worker dies, the load balancer will check its state
# once in a while. Until then all work is redirected to peer
# worker.
worker.failover.type=lb
worker.failover.balance_workers=tomcat1,tomcat2
worker.failover.sticky_session=False
worker.failover.sticky_session_force=False

# END workers.properties
 

4 comments:

  1. Hi Saravana,

    I have a setup with two tomcat instances(A&B). I have configured an apache web server 2.2 for load balancing and fail over. Setup looks fine as per the configurations suggested. Let tomcat A be handling some request at sometime. When tomcat instance(A) goes down, the session is replicated to another tomcat instance(B) successfully. Now tomcat instance B is handling those requests. Till this point everything goes fine, but when I bring up tomcat instance(A) and after that tomcat instance(B) goes down, the session is no longer replicated. What might be the issue? In status page of apache server I see even if node status is OK session is not replicated to fail over node for second time.

    Thanks,
    Sumedh

    ReplyDelete
  2. You mean failover from A to B works fine, but when master A comes up again apache(httpd) doesn't send the requests to A?

    Httpd takes a while to switch from B to A. If you want to switch over to A immediately then you will have to gracefully restart apache-httpd.

    ---
    SK

    ReplyDelete
  3. also, your first and second instances are using the same port number in this example (9009). I'm assuming that's a typo too? One is supposed to be on 8009 and the other on 9009?

    ReplyDelete
  4. You are correct.

    Fixed the typos. Thanks for pointing out.

    ---
    SK

    ReplyDelete