C2B2 logo icon

Creating a Simple Cluster with Glassfish

Glassfish is Oracle's reference Java EE application server, so you can be sure that it will support the Java EE specification completely. In this post, I’ll go through the first steps of getting Glassfish installed and clustered with Apache to proxy requests. All this will be set up using one CentOS and one Fedora instance, but the steps should require little to no tweaking for other Linux platforms.

Installing Glassfish

To get the latest and greatest version of Glassfish, simplygo to glassfish.java.net and click the green, arrow-shaped button which ishelpfully labelled “Latest and Greatest Download”. At the time of writing, thisis (though the Java EE 7 based 4.0 release is imminent), and clickingthe arrow will download the ZIP distribution. Extract to a location of yourchoice and you’re done! All that’s left is configuration.
Before we get there, though, we’ll need to make sure Apacheand an SSH server are available. Apache is what we’re using to proxy requeststo the cluster and SSH is needed because Glassfish’s communication is all overSSH.
On my Fedora and CentOS, getting Apache up and running is aseasy as “yum install httpd” and openSSH was already installed. After startingthe services (service sshd start), I made sure they could communicate to eachother with a terminal. If you have any problems at this stage, make sure youdon’t have any firewall restrictions or (if your Linux distribution isRHEL-based) that SELinux isn’t blocking communication.

Create a domain

Glassfish is configured with the asadmin command-line tool(asadmin.bat for Windows) which is in the bin directory, so for me that was/opt/glassfish/glassfish-
For development and testing purposes, glassfish comes with adefault domain already available, which can be started with the command asadmin start-domain domain1. Once the domain isstarted, we need to enable secure admin so that the communication over SSH ispossible. Without that, we can’t make a cluster across two separate machines!Secure admin requires an admin account to be set with a password. The defaultadmin account is simply “admin” with no password so, since this is only a test,I set the password to admin too.

Here are all the commands I used:

asadmin start-domaindomain1
asadmin enable-secure-admin
asadminrestart-domain domain1
Alternatively, you can create a new domain using asadmin create-domain.  Creating a new domain with this command givesyou quite a lot of control over the domain configuration at the point ofcreation. Thedocumentation gives a very good overview of the available commands, butthere are a couple I’d like to highlight:
create-domain [--help]
[--adminport adminport]
[--instanceport instanceport]
[--portbase portbase]
[--profile profile-name]
[--template template-name]
[--domaindir domaindir]
[--domainproperties (name=value)[:name=value]*]
[--keytooloptions (name=value)[:name=value]*]
The --portbase and --checkports options are very handy tomake sure you avoid port conflicts with anything else you may have installed. Aportbase is simply a port number from which the other ports are offset, forexample, the admin port will be [portbase] + 48. Including a portbase in yourcreate statement means that you can’t explicitly specify other ports when youcreate the domain, since they would be overridden.
“Checkports” does what it says on the tin, and checks the availabilityof the administration, HTTP, JMS, JMX, and IIOP ports. It’s a very handy thingto have, so the default value is true.

Communicate between instances

So far, all we have done is configure one machine to have aGlassfish domain. Before we begin to configure the second, there are two moresteps to complete on our admin machine. Open the admin console atlocalhost:4848 and select “Clusters” and create a new one; mine was calledc2b2Cluster. Once that’s created, click the cluster you’ve just created, go tothe “Instances” tab and create a new server instance; mine was called c2b2Srv1.Now we have a cluster with an instance, we can start the cluster from thecluster’s “General” tab.
This is the part where setting up SSH correctly becomesnecessary! On the second machine, make sure glassfish is installed and createanother server as follows:
asadmin --host <machine1>--port <admin port> create-local-instance --cluster c2b2Cluster c2b2Srv2


When you invoke this command, the asadmin tool on the localmachine doesn’t actually execute the command; it simply asks the asadmin toolon the remote machine you specify to execute it. So if you were to type “asadminlist-instances” afterwards on machine 2, the list would be empty even though aserver instance physically resides on the machine. Running the same command onthe machine where the domain is located lists all instances in the domain,whether they are physically on the machine or not.
Incidentally, if you wanted to keep your admin serverseparate to your server instances, you would follow the same procedure up tocreating the cluster, but then create both your server instances remotely likethis.
If you were to use the admin console to start the new servernow, you’ll find that it won’t start. We first need to configure the new node.Select the new node in the “Nodes” and make sure the “Type” is set to SSH, NodeHost is either the correct IP address or URL and the authentication settingsare correct. SSH user authentication is done by default through a key file, butcan be changed to password based authentication if you prefer.
Again, if you find you still have communication problems,check your firewall, SELinux and other security settings.


To configure Apache to proxy requests to the cluster, Ichose to use mod_proxy, since it is already installed and available with theApache base install. For more configuration options and better highavailability, you should probably consider mod_jk instead.
Make sure the line “LoadModule proxy_modulemodules/mod_proxy.so” exists and is uncommented in the httpd.conffile. Then it’s simply a matter of including the following two lines:
ProxyPass            /      http://host_name:0000
ProxyPassReverse     /      http://host_name:0000
That’s all you need to do with Apache! The final step is tomake sure Glassfish is listening to whatever port you specified. You can eithercreate a new jk listener or modify an existing http listener. The followingcommand will create a new listener called jk-connector listening to port 8009:
asadmin create-network-listener --listenerport 8009 --protocolhttp-listener-1 --jkenabled true jk-connector
Alternatively, you can do the same thing through theconsole. I tested my connection by modifying the admin listener at port 4848 tosee if Apache would forward me to the Glassfish Admin console. To do the same,open the console and go to: 

Configurations àserver-config àNetword Config àNetwork Listeners àadmin-listener

Enable the JK Listener check box and make sure to save.

Once you’ve gone through the process once, you’ll see howeasy and straightforward clustering is with Glassfish. They’re simple concepts,executed simply. Having just one tool to do all the configuration work, ratherthan editing many properties files makes life much easier!