Thursday, December 4, 2008

Load Balancer for Tomcat Servers

Apache HTTP server mod_balancer module can be used as load balancer for tomcat servers. [http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html]
We can also configure the apache HTTP server for sticky sessions

Apache HTTP server supports prefork and worker module. worker module is recommended for less memory usage.
1. Configuring Apache Load Balancer

  • Install Apache HTTP Server
  • Enable following modules in Apache HTTP Server in conf/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so
  • Add the following code to httpd.conf
<VirtualHost *>
RewriteEngine On
UseCanonicalName On

# To reduce the logging on the http server

ErrorLog "/var/log/apache2/rewrite.log"
LogLevel error
CustomLog /dev/null combined

# Sticky session is maintained using JSESSIONID
# No failover

ProxyPass /examinator balancer://mycluster/ stickysession=JSESSIONID nofailover=On maxattempts=0 timeout=1800
ProxyPassReverse / balancer://cluster/
ProxyPreserveHost On
ProxyTimeout 1800

<Proxy balancer://mycluster>
BalancerMember http://perf21:8080 route=151
BalancerMember http://perf22:8080 route=152
</Proxy>

</VirtualHost>
  • To make it sticky sesion, we have to add the route ids to tomcat server config file.
  • Add route id as jvmRoute param in conf/server.xml of tomcat server. This id needs to be unique for each tomcat server. So we are using the last token of the IP address for each node. (eg . perf21 : 10.0.4.151)
This can be done by the following script

// Setting jvmRoute to the last digits of IP address in server.xml
// jvmRoute is specific to sticky session apache load balancer.
// See Load Balancer Configuration

IP=`/sbin/ifconfig | grep inet | grep '10.0' | cut -d '.' -f 4 | cut -d ' ' -f 1`
echo "Changing jvmRoute to $IP"
sed -i 's/IPCONFIG/'"$IP"'/g' $/conf/server.xml

  • This id would get appended to Cookies sent by this tomcat server. The load balancer will route based on this id to the respective server.

No comments: