How to programmatically adjust the disable directive in the mod_jk load balancer configuration?

Geo picture Geo · Apr 20, 2009 · Viewed 8.1k times · Source

We have a setup where we have one httpd (apache) with mod_jk talking in a load balance setup to three tomcat servers. We have to recycle each tomcat instance envery three hours. So tomcat1 will restart at 1, and tomcat2 at 2 and ... until tomcat1 recycles again at 4.

We want to configure a script or a type of program to disable the worker node that is going through a recylce to minimize session errors at the user using our application.

Any suggestions.

Answer

Chris May picture Chris May · Apr 20, 2009

mod_jk re-reads workers.properties on an "apachectl graceful", so you if your workers.properties looks like this:

worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcat1, tomcat2, tomcat3

... 

You could just write a script which replaces the balanced_workers list with the ones you want, and then graceful's apache

Update here's a script to do just that, which I cobbled together from some bits I had lying around. I wouldn't suggest using it in production, but it might give you some ideas for your own version.

#!/bin/bash

# set some paths
WORKERS_PROPERTIES="./workers.properties"
APACHECTL="/usr/sbin/apache2ctl"

# what does the loadbalancer config line look like?
WORKER_LINE_START="worker.loadbalancer.balanced_workers="
# full list of workers
ALL_WORKERS="tomcat1 tomcat2 tomcat3"

# first command line arg is the worker to remove. 
remove=$1

# build up the new line listing the active workers
worker_line=$WORKER_LINE_START
sep=""
for worker in $ALL_WORKERS
do
  if [ ${remove} != ${worker} ]
  then
     worker_line="${worker_line}$sep $worker"
     sep=","
  fi
done

# sed hackery to replace the current line with the one we just built.
# needs gnu sed (or another one that supports in-place editing)
sed -i.bak "s/^$WORKER_LINE_START.*$/$worker_line/" $WORKERS_PROPERTIES

# restart apache
$APACHECTL graceful