Welcome!

Java Authors: Yeshim Deniz, Pat Romanski, Jim Kaskade, Liz McMillan, Elizabeth White

Related Topics: Java, Cloud Expo, Apache

Java: Article

Dynamic Clustering for J2EE Cloud Environments

Cloud computing is one of the emerging paradigms in today's computing world

Cloud computing is one of the emerging paradigms in today's computing world. One of the main advantages of migrating to the cloud is its elastic nature. Elasticity allows dynamic provisioning and de-provisioning of resources according to the application's workload requirement.

In a traditional on-premise J2EE infrastructure, information about the application server and web server resources are available during deployment. Clustering of such an infrastructure to achieve scalability is much simpler since the information about resources is known beforehand. But in a cloud environment, because of its elastic nature, resources get provisioned and de-provisioned dynamically based on the workload. So a j2ee cloud environment has challenges like reconfiguring automatically for the addition/removal of application server instances to/from the cluster. One solution from the open source space is to use Apache Httpd web server with mod_cluster load balancing module and JBoss application server.

The article will discuss the features of mod_cluster which enable it to operate in a cloud environment and also the steps to set up a highly scalable J2EE cloud environment in your lab.

Introduction to mod_cluster
mod_cluster is an extension of the Apache httpd mod_proxy load balancing module and can balance http requests across multiple instances of JBoss Application Server, JBoss Web standalone, or Tomcat. The unique feature of mod_cluster is that once the initial configuration is done, there is no need of any manual configuration changes for adding or removing JBoss AS or Tomcat instances.

mod_cluster uses two communication channels for its working. It uses ajp, http or https to forward requests from httpd to one of the application server nodes. The backward channel is used by the application server nodes to send server side information to the httpd side and is the key differentiator for mod_cluster with respect to other load balancing modules. This channel sends real time information about load balancing factors for each node and application life cycle events.

Some of the key features of mod_cluster that enable it to be used in cloud environments are

Dynamic configuration
Common httpd based load balancers like mod_jk and mod_proxy require the configuration of workers (application servers) at the httpd side. So if you want to add a new worker, you will have to change the configuration in httpd side and restart the proxy. This is an overhead in case of large or dynamically varying clusters like in cloud environments.

But with mod_cluster the proxy information is maintained in the application server side through a static list or through the advertise mechanism (using mod_advertise) . As the workers start, the listeners receive multicast pings which contain the host and port information about the proxies. Now the workers can send events to the detected proxies and the proxies auto configure themselves to balance request between the nodes.

Dynamic determination of load balancing factor
In common httpd based load balancers, the ratio in which load is distributed among the workers is determined by a static factor we provide at the httpd load balancer configuration. But in the case of mod_cluster the load balancing factor is determined at the application server side based on the real time values monitored at runtime. Load computation is pluggable and you can write your own LoadMetric based on the metric you want in addition to the default load metrics.

Fine grained web application life cycle
In the case of mod_cluster, the applications deployed in the application server side are registered with the httpd side through the Mod-Cluster Management protocol. So the http side has information about which all applications are deployed in which instances and transfers the requests only to the nodes which have the requested application. Since the proxies have information on the applications deployed on each of the workers, we can keep highly sensitive applications on our private cloud and move lesser critical applications to some public provider. This enables us to scale up without compromising security as the data sensitive applications will be in our local premises only . For example in a shopping cart scenario browsing the catalog can go into the public cloud and sensitive requirements like payments can stay in company's private cloud.

The following section describes how we can set up a highly scalable j2ee environment using vmware(or any other private cloud solution like Eucalyptus or Open Nebula) in your lab setup.

Environment Set-Up
We have VMware vCenter server setup in one of the machines and connected to an ESXi host. The discussion is based on the assumption that the reader knows how to create a virtual machine and install guest operating system in a vmware environment. Our virtual machine has CENTOS 5.4 installed in it.

To set up dynamic cluster in cloud, we need minimum of two instances:

  1. Apache httpd + mod_cluster
  2. JBoss 5.1 application server with mod_cluster

Now we can look into creating each of these images/templates with necessary startup scripts in details. These steps can be done in any of the CentOS 5 installed machine or virtual machines.

Creating Apache httpd image
Step 1. Create a base virtual machine with centos 5.x as the guest operating system.
Refer creating a virtual machine and installing guest os for VMWare virtual machine creation.

Step 2. Install Apache httpd and mod_cluster in the virtual machine
Download the apache httpd integrated with latest mod_cluster distribution here. To install httpd with mod_cluster, move the distribution to the vm and extract mod_cluster-1.1.xxx-linux2-x86-ssl.tar.gz file using the following command

tar xvf mod-cluster-1.1.0.xxx-linux2-x86-ssl.tar.gz

This by default installs httpd with required mod_cluster modules in /opt/jboss directory.

Step 3. Configuring mod_cluster at httpd side
The httpd configuration file will be httpd.conf which is located in /opt/JBoss/httpd/httpd/conf. From mod_cluster1.1.0CR2 mod_cluster comes with some quick start values.

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

LoadModule slotmem_module modules/mod_slotmem.so

LoadModule manager_module modules/mod_manager.so

LoadModule proxy_cluster_module modules/mod_proxy_cluster.so

LoadModule advertise_module modules/mod_advertise.so

The above configuration specifies the extra modules required for httpd with mod_cluster. If you are adding mod_cluster to the existing httpd installation, you have to download the modules and add the above configuration to httpd.conf file.

# MOD_CLUSTER_ADDS

# Adjust to you hostname and subnet.

<IfModule manager_module>

Listen *:6666

ManagerBalancerName mycluster

<VirtualHost *:6666>

<Directory />

Order deny,allow

Deny from none

Allow from all

</Directory>

KeepAliveTimeout 300

MaxKeepAliveRequests 0

#ServerAdvertise on http://@IP@:6666

AdvertiseFrequency 5

#AdvertiseSecurityKey secret

#AdvertiseGroup @ADVIP@:23364

<Location /mod_cluster_manager>

SetHandler mod_cluster-manager

Order deny,allow

Deny from none

Allow from all

</Location>

</VirtualHost>

</IfModule>

Customize the above configuration for your own needs as this is not suitable for production environment.

Step 4. Starting httpd at boot up
We need the httpd to be up and running when the machines boots up. To achieve this we have to expose httpd as a service through init scripts.

The below script can be used to start and stop httpd at boot up.

#!/bin/sh

# chkconfig: - 64 36

# description: Apache Start|Restart|Stop Web Server

APACHE_HOME=/opt/jboss/httpd

case "$1" in

start)

echo "Starting Apache ..."

# Change the location to your specific location

$APACHE_HOME/sbin/apachectl start

;;

stop)

echo "Stopping Apache ..."

# Change the location to your specific location

$APACHE_HOME/sbin/apachectl stop

;;

graceful)

echo "Restarting Apache gracefully..."

# Change the location to your specific location

$APACHE_HOME/sbin/apachectl graceful

;;

restart)

echo "Restarting Apache ..."

# Change the location to your specific location

$APACHE_HOME/sbin/apachectl restart

;;

*)

echo "Usage: '$0' {start|stop|restart|graceful}" >&2

exit 64

;;

esac

exit 0

Copy the above script to /etc/init.d/httpd file or write your own startup script for apache httpd.

Give the file execute permission

chmod +x /etc/init.d/httpd

Add httpd as service at required run levels

chkconf -add httpd

chkconfig -level 345 httpd on

Now to test the set up try

service httpd start

Starting httpd:                                         [ OK ]

Try http://[ip]:[mod_clusterport]/mod_cluster_manager in the browser.

You should be able to see the following window

Step 5. Convert virtual machine to template
To avoid repeating the same steps for creating httpd virtual machine, you can create the clone of the vm. For vmware powerOff the virtual machine and clone it to template

Now we will look into how to create the jboss image.

Creating JBoss image
Step 1. Create the Centos vm

Refer image creation for httpd

Step 2. Install Java
You can get the latest Java from the following location and the second link explains steps for java installation.

http://www.oracle.com/technetwork/java/javase/downloads/index.html

http://www.oracle.com/technetwork/java/javase/index-137561.html

Step 3. Installing JBoss AS with mod_cluster

We are using JBoss 5.1GA which can be obtained here. Let $JBOSS_HOME is the JBoss installation directory. For installing jBoss AS simply extract the downloaded tar file.

For the demo JBOSS_HOME = /home /JBoss-5.1.0.GA

Download the latest java bundles for mod_cluster here. mod_cluster 1.1.0 work with with JBoss AS 5.1 with out of box.Extract the mod_cluster-1.1.0.xxx-bin.tar.gz file and copy the mod_cluster.sar to the deploy folder.

tar xvf mod_cluster-1.1.0.CR3-bin.tar.gz

cp -r /tmp/mod_cluster.sar $JBOSS_HOME/server/all/deploy

Assuming you have extracted to /tmp directory

cp -r /tmp/mod_cluster.sar $JBOSS_HOME/server/all/deploy

Step 4. Configuration

The main configuration file is mod_cluster-JBoss-beans.xml under

$JBOSS_HOME /server/all/deploy/ mod_cluster.sar/ META-INF/

By default mod_cluster is configured to work in clustered mode. In clustered mode, a single JBoss node is responsible for providing the entire cluster view to the front-end httpd processes.  The default configuration uses advertise mechanism using the mod_advertise module.

Step 5. JBoss as a service at startup

Execute the following commands to add new user jboss and give the startup file execute permission.

#create and give permissions to user jboss

adduser jboss

chown -Rf jboss.jboss /$JBOSS_HOME

#copy the default startup script to /etc/init.d

cd /$JBOSS_HOME /bin

cp JBoss_init_redhat.sh /etc/init.d/jboss

chmod +x /etc/init.d/jboss

Modify the /etc/init.d/jboss file to point JBOSS_HOME and JAVAPTH to point to the actual installed directories.

# chkconfig: - 35 90

# description: JBoss Start|Restart|Stop Application Server

# pidfile: /var/run/JBoss.pid

....

JBOSS_HOME=${JBOSS_HOME:-"$JBOSS_HOME"}

#define the user under which JBoss will run, or use 'RUNASIS' to run as the current user

JBOSS_USER=${JBOSS_USER:-"JBoss"}

#make sure java is in your path

JAVAPTH=${JAVAPTH:-"jdk installation folder "}

#configuration to use, usually one of 'minimal', 'default', 'all'

JBOSS_CONF=${JBOSS_CONF:-"all"}

#if JBOSS_HOST specified, use -b to bind JBoss services to that address

OS=`uname`

IP="" # store IP

case $OS in

Linux) IP=`ifconfig eth0| grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;

FreeBSD|OpenBSD) IP=`ifconfig eth0 | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;

SunOS) IP=`ifconfig -a eth0 | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;

*) IP="Unknown";;

Esac

#Bind to the current ip address

JBOSS_HOST=$IP

JBOSS_BIND_ADDR=${JBOSS_HOST:+"-b $JBOSS_HOST"}

The above modification is to bind jboss to the ip address of the jboss instance.

To add jboss as a service at startup,

#command to start jboss at runlevel 3,4 and 5

chkconfig --add jboss

chkconfig --level 345 jboss on

service jboss start

Check in the browser if JBoss is started

http://[jboss-ip]:8080

Step 6. Convert vm to template

For vmware powerOff the virtual machine and clone it to template.

Testing the environment
Create virtual machines from the above created templates using vijava api or vCenter client. After one instance of both apache httpd and jboss got powered on, check the mod_cluster_manager using the url,

http://[webserverip]:[mod_clusterport]/mod_cluster_manager/

We can see that the JBoss worker is balanced by the mod_cluster. If we create one more JBoss instance, the new one will get added to that balancer. So if you have an application deployed on both of the JBoss instances, the requests will be distributed across the JBoss instances. Similarly when you start up new JBoss instances, the instances will get registered automatically to the proxy and become available for load balancing.If we kill a JBoss instance that will automatically get de-registered from the proxy balancer.

Set up in an environment where multicast is not supported
The above setup showed mod_cluster configuration using advertise mechanism, which uses muticast pings for auto discovery. But major cloud providers like Amazon EC2, Rackspace, GoGrid etc doesn't support multicast in their environment. To overcome this, information about the proxies can be passed through an JBoss argument (JBoss.mod_cluster.proxyList) at start instance up or use the addProxy method exposed by mod_cluster through JMX. The addProxy method takes the IP of httpd proxy and the port on which mod_cluster is listening. You can go to the JBoss AS JMX-Console to do this or use the java code to invoke this method remotely.

To disable the advertise mechanism following configuration changes need to be done :

At httpd side : Set the ServerAdvertise property to off in httpd.conf config file in /opt/JBoss/httpd/httpd/conf

ServerAdvertise off

At JBoss Side : Set advertise property in ModClusterConfig bean to false in mod_cluster-JBoss-beans.xml under $JBOSS_HOME /server/all/deploy/ mod_cluster.sar/ META-INF/

<property name="advertise">false</property>

After setting these properties, create virtual machines from the templates. We can see in the mod_cluster_manger of the apache instance that that jboss node is not added. Now  we have to add the proxy instance to the jboss mod_cluster configuration through JMX. The following code snippet can be used to add a proxy to the balancer.

Hashtable contextProps = new Hashtable();

contextProps.put("java.naming.factory.initial"," org.JBoss.naming.HttpNamingContextFactory");

contextProps.put("java.naming.provider.url", http://+JBossinstanceip+":8080/invoker/JNDIFactory");

contextProps.put("java.naming.factory.url.pkgs", "org.JBoss.naming.client");

InitialContext ctx = new InitialContext(contextProps);  // From table

MBeanServerConnection server = (MBeanServerConnection) ctx.lookup("jmx/invoker/RMIAdaptor");

Object op = server.invoke(new ObjectName("JBoss.web:service=ModCluster"), "addProxy", new Object[]{webServerIP,webServermod_clusterPort},new String[]{"java.lang.String","int"} );

Now if you check the mod_cluster_manager we can see that the jboss node now balanced by mod_cluster.

Conclusion
With capabilities like dynamic addition of workers without any configuration changes, knowledge of deployed applications and calculation of the real time load balancing factor based on different metrics , it is certain that mod_cluster is the future of load balancer modules for apache and it will also have a huge impact in the cloud environment.

More Stories By Joel Mathew

Joel Mathew works as a Technology Analyst at SETLabs, R&D division, at Infosys Technologies Ltd. He has close to 3 years of experience in development of Cloud computing, Java and Java EE applications, Web 2.0,etc.