«

»

Nov
01

Load balancing glassfish using apache

Overview

After my articles about setting up a glassfish cluster and enable session replication, its now time to put a load balancer in front of it.

A couple of times I’ve tried to get the Sun load balancing plugin working, but without success. This was because of  some operating system issues or other problems.
So after looking around the web, I’ve found some good solutions using apache and the mod_jk connector from the apache tomcat project.

Requirements

Installation

  1. Configuring the glassfish cluster
    1. Copy the $CATALINA_HOME/server/lib/tomcat-ajp.jar to your $GLASSFISH_HOME/lib directory on each glassfish installation in your cluster.
    2. Also copy the commons-logging.jar and commons-modeler.jar to your $GLASSFISH_HOME/lib directory on each glassfish installation in your cluster.
    3. Now you have to define jvmRoute and com.sun.enterprise.web.connector.enableJK system properties for the cluster (this can be done from the domain administration server)
      asadmin create-jvm-options --target cluster1 "-DjvmRoute=\${AJP_INSTANCE_NAME}"
      asadmin create-jvm-options --target cluster1 "-Dcom.sun.enterprise.web.connector.enableJK=\${AJP_PORT}"
    4. Now you have to define the values for these properties for each instance running in the cluster (this can also be done from the domain administration server)
      asadmin create-system-properties --target instance1 AJP_INSTANCE_NAME=instance1
      asadmin create-system-properties --target instance1 AJP_PORT=8020
  2. Configuring the apache web server
    1. Copy the mod_jk.so to a apache accessible directory.
    2. Copy the following lines to your apache main config file.
      LoadModule jk_module <path to your mod_jk.so>
      JkWorkersFile <path to your worker.properties (created later)>
      JkLogFile <path to your mod_jk.log>
      JkLogLevel debug
      JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
      JkOptions +ForwardURICompat -ForwardDirectories
      JkRequestLogFormat "%w %V %T"
    3. Copy the following lines to your virtual host configuration (or into the main config, if you don’t use virtual hosts).
      JkMount /* loadbalancer
    4. This will redirect all requests to your load balancer. (You can also use patterns like /*.jsp for only redirecting jsp file request to the load balancer or just any other sensefull pattern).
  3. Writing your worker.properties configuration file.
    worker.list=loadbalancer
    
    # Set properties for instance1
    worker.instance1.type=ajp13
    worker.instance1.host=<host of instance1>
    worker.instance1.port=<AJP port of instance1>
    worker.instance1.lbfactor=50
    worker.instance1.connection_pool_size=10
    worker.instance1.connection_pool_timeout=600
    worker.instance1.socket_keepalive=1
    worker.instance1.socket_timeout=300
    
    # Set properties for instance2
    worker.instance2.type=ajp13
    worker.instance2.host=<host of instance2>
    worker.instance2.port=<AJP port of instance2>
    worker.instance2.lbfactor=50
    worker.instance2.connection_pool_size=10
    worker.instance2.connection_pool_timeout=600
    worker.instance2.socket_keepalive=1
    worker.instance2.socket_timeout=300
    
    worker.loadbalancer.type=lb
    worker.loadbalancer.balance_workers=instance1,instance2
  4. Restart all cluster instances and the web server. Your load balancing should be working now.
    Each time a new session is creted by the glassfish, it appends its instance name to the JSESSIONID, so the load balancer can determine on which instance to route the request.
    If one instance crashes, the load balancer will notice this and redirects all requests to another instance. If you have enabled session replication, your old session will be resumed there.

Further reading

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>