Over a million developers have joined DZone.

Creating a Simple Cluster with GlassFish

· Integration Zone

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and dPaaS could cost you down the road. Brought to you in partnership with Liaison Technologies.

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, simply go to glassfish.java.net and click the green, arrow-shaped button which is helpfully labelled “Latest and Greatest Download”. At the time of writing, this is 3.1.2.2 (though the Java EE 7 based 4.0 release is imminent), and clicking the arrow will download the ZIP distribution. Extract to a location of your choice and you’re done! All that’s left is configuration.

Before we get there, though, we’ll need to make sure Apache and an SSH server are available. Apache is what we’re using to proxy requests to the cluster and SSH is needed because Glassfish’s communication is all over SSH.

On my Fedora and CentOS, getting Apache up and running is as easy as “yum install httpd” and openSSH was already installed. After starting the services (service sshd start), I made sure they could communicate to each other with a terminal. If you have any problems at this stage, make sure you don’t have any firewall restrictions or (if your Linux distribution is RHEL-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-3.1.2.2/bin.

For development and testing purposes, glassfish comes with a default domain already available, which can be started with the command asadmin start-domain domain1. Once the domain is started, we need to enable secure admin so that the communication over SSH is possible. 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 default admin 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-domain domain1

asadmin change-admin-password

asadmin enable-secure-admin

asadmin restart-domain domain1

Alternatively, you can create a new domain using asadmin create-domain.  Creating a new domain with this command gives you quite a lot of control over the domain configuration at the point of creation. The documentation gives a very good overview of the available commands, but there 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]

[--savemasterpassword={false|true}]

[--usemasterpassword={false|true}]

[--domainproperties (name=value)[:name=value]*]

[--keytooloptions (name=value)[:name=value]*]

[--savelogin={false|true}]

[--checkports={true|false}]

[--nopassword={false|true}]

domain-name

The --portbase and --checkports options are very handy to make sure you avoid port conflicts with anything else you may have installed. A portbase is simply a port number from which the other ports are offset, for example, the admin port will be [portbase] + 48. Including a portbase in your create statement means that you can’t explicitly specify other ports when you create the domain, since they would be overridden.

“Checkports” does what it says on the tin, and checks the availability of the administration, HTTP, JMS, JMX, and IIOP ports. It’s a very handy thing to have, so the default value is true.

Communicate between instances

So far, all we have done is configure one machine to have a Glassfish domain. Before we begin to configure the second, there are two more steps to complete on our admin machine. Open the admin console at localhost:4848 and select “Clusters” and create a new one; mine was called c2b2Cluster. Once that’s created, click the cluster you’ve just created, go to the “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 the cluster’s “General” tab.

This is the part where setting up SSH correctly becomes necessary! On the second machine, make sure glassfish is installed and create another 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 local machine doesn’t actually execute the command; it simply asks the asadmin tool on the remote machine you specify to execute it. So if you were to type “asadmin list-instances” afterwards on machine 2, the list would be empty even though a server instance physically resides on the machine. Running the same command on the 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 server separate to your server instances, you would follow the same procedure up to creating the cluster, but then create both your server instances remotely like this.

If you were to use the admin console to start the new server now, 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, Node Host is either the correct IP address or URL and the authentication settings are correct. SSH user authentication is done by default through a key file, but can 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.

Apache

To configure Apache to proxy requests to the cluster, I chose to use mod_proxy, since it is already installed and available with the Apache base install. For more configuration options and better high availability, you should probably consider mod_jk instead.

Make sure the line “LoadModule proxy_module modules/mod_proxy.so” exists and is uncommented in the httpd.conf file. 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 to make sure Glassfish is listening to whatever port you specified. You can either create a new jk listener or modify an existing http listener. The following command will create a new listener called jk-connector listening to port 8009:

asadmin create-network-listener --listenerport 8009 --protocol http-listener-1 --jkenabled true jk-connector

Alternatively, you can do the same thing through the console. I tested my connection by modifying the admin listener at port 4848 to see 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 how easy and straightforward clustering is with Glassfish. They’re simple concepts, executed simply. Having just one tool to do all the configuration work, rather than editing many properties files makes life much easier!

Discover the unprecedented possibilities and challenges, created by today’s fast paced data climate and why your current integration solution is not enough, brought to you in partnership with Liaison Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}