Thursday, February 28, 2008

load balance & fail-over with tomcat using apache proxy_ajp_module proxy_balancer_module

In the past, we have been using mod_jk as the connector to front apache http server for tomcat, it does load balance and fail-over nicely. Now apache has modules such as proxy_ajp_module and proxy_balancer_module that would serve the same purpose, but with simpler configuration. Below is the VirtualHost config in apache httpd:
NameVirtualHost *:8080
<VirtualHost *:8080>

<Location /balancer-manager>
SetHandler balancer-manager
</Location>

<Proxy balancer://ajpCluster>
BalancerMember ajp://localhost:8109 route=jvm1
BalancerMember ajp://localhost:8209 route=jvm2
</Proxy>

ProxyPass /balancer-manager !
ProxyPass / balancer://ajpCluster/ stickysession=JSESSIONID nofailover=On
</VirtualHost>

On the tomcat end there are a few things needs to be changed in server.xml:

Uncomment the section where it defines the AJP connector and change the port number:
<!– Define an AJP 1.3 Connector on port 8009 –->
<Connector port=”8109″ enableLookups=”false” redirectPort=”18443″ protocol=”AJP/1.3″ />

This part I totally forgot which took me a while to realize that I did not define the jvmRoute name in tomcat. This would be appended to the JSESSIONID cookie so it knows which tomcat to route to:
<Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”jvm1">

Now when you hit http://hostname:8080/ it hits the apache httpd server and it would route you to the least loaded tomcat that is up running.
http://hostname:8080/balancer-manager
would show you the status of the load balancer and tomcat servers.

I have tested this worked well with JIRA.

No comments: