How to: Set up a GlassFish Cluster

This entry is part 1 of 2 in the series Load balancing with Eclipse GlassFish cluster

This tutorial will guide you to set up a GlassFish cluster that serves a single application on multiple machines.

This setup doesn’t enable HTTP session replication. Session data will be available only on the original cluster instance that created it. Therefore, if you access the cluster instances through a load balancer server, you should enable sticky session support on the load balancer server. With this mechanism, a single user will be always served by the same GlassFish instance, different users (HTTP sessions) may be served by different GlassFish instances. It’s also possible to configure session replication in a GlassFish cluster so that each session is available on each instance but this is not covered by this tutorial.

Step 1: Start the Default Domain

  1. Open a terminal and navigate to the bin directory of your GlassFish installation.
  2. Start the default domain:
asadmin start-domain
  1. Open GlassFish Admin Console, which is running on http://localhost:4848 by default. If you access the Admin Console from a remote machine, you need enable the secure administration first and then access Admin Console via HTTPS, e.g. //glassfish-server.company.com:4848. To enable secure administration, refer to the Eclipse GlassFish Security Guide.

Step 2: Create a Cluster

  1. Navigate to “Clusters”
  2. Click on the “New…​” button to create a new cluster.
  3. Enter a name for the cluster (e.g., myCluster)
  4. In the “Server Instances to Be Created” table, click “New…​”, and then fill in instance1 as an instance name, keep “Node” selected to the default “localhost-domain1”
  5. Click “OK”

This will create a clustering configuration myCluster in GlassFish, with one GlassFish server instance instance1, which runs on the same machine as the GlassFish administration server (DAS).

New Cluster page

Step 3: Add routing config for sticky sessions

  1. Navigate to Clusters → myCluster
  2. Click on the “myCluster-config” link in the “Configuration” field
  3. Click on “System Properties” to open the “System Properies” configuration for the cluster
  4. Click on “Add Property” button
  5. In the new row, set the value in the “Instance Variable Name” column to “jvmRoute”
  6. Set “Default Value” to “${com.sun.aas.instanceName}” so that it’s set to the actual instance name for every cluster instance

This configuration is needed to simplify the sticky session routing mechanism in load balancers, e.g. in Apache HTTP server.

System Properties page

Step 4: Start the cluster

  1. Navigate to “Clusters”
  2. Click the checkbox in the “Select” column next to your cluster
  3. Click “Start Cluster” button and wait until the cluster is started
  4. Navigate to http://DOMAIN_NAME:28080 (e.g. http://localhost:28080), where DOMAIN_NAME is the same as in the URL of your DAS server (e.g. localhost)

This will navigate you to the welcome page of the instance1 GlassFish instance running on port 28080.

Starting the Cluster
Cluster instance running

Step 5: test the Cluster

Deploy a cluster tester application, which you can download from //github.com/OmniFish-EE/clusterjsp/releases.

  1. In GlassFish Admin Console, navigate to Applications and click the “Deploy…” button
  2. In the “Targets” section, click on your cluster in the “Available Targets” column and click the “Add >” button
Deploy an application to the cluster
  1. In the “Location” section, click Browse…​ and select the application WAR file
  2. Set the “Context Root” field to “cluster”
  3. Click “OK”
  4. Navigate to http://DOMAIN_NAME:28080/cluster (e.g. http://localhost:28080/cluster)

Right now, there’s a single instance in the cluster. All requests are handled by that instance, session data is always present, and all should work as expected. As you’ll add more instances to the cluster, you can use this tester application again to verify that all works even if requests are sent to different instances.

Tester application running

Step 6: Add an SSH Node

Now, add a SSH connection to a remote server, where you want to run other GlassFish cluster instances.

This assumes that you already have a remote machine with SSH server and Java installed.

Make sure that GlassFish admin server can access the remote machine on the SSH port (port 22 by default). Then:

  1. In GlassFish Admin Console, navigate to Nodes. There’s always at least 1 node, e.g. localhost-domain1, which represents the local machine
  2. Click on the “New…​” button to add a new SSH node.
  3. Enter a name for the SSH node (e.g., sshNode1).
  4. Set the “Type” to “SSH”
  5. Set the “Host” to the IP address of the remote machine.
  6. If you want to install GlassFish on the remote machine, enable the ckeckbox “Install GlassFish Server”
  7. Set the “SSH User” to a user with sufficient privileges on the remote machine. Set to `${user.name} if it’s the same user as the one running the GlassFish admin server
  8. Set the “SSH User Authentication” based on your authentication method. Fill in the authentication details, e.g. “SSH User Password” for “Password” authentication
  9. Click “OK” to add the SSH node.
GlassFish Nodes page

Step 7: Create an Instance on the SSH Node

  1. Navigate to Clusters → myCluster.
  2. Select the “Instances” tab.
  3. Click on the “New…​” button to create a new instance.
  4. Enter a name for the new instance (e.g., remoteInstance) and select the SSH node (sshNode1) as the target “Node”.
  5. Click “OK” to create the instance.

Make sure that the admin port of the remote instance is open for connections from GlassFish admin server and isn’t blocked by a firewall. The port number is 24848 by default. You can find it in GlassFish Admin Console:

  1. Navigate to Clusters → myCluster, the tab “Instances”
  2. Click on “remoteInstance” in the “Name” column
  3. The admin port number is the first port in the “HTTP Port(s)” field
Info about the instance on the SSH node

Without this, the admin server will be able to start the instance via SSH but will not be able to communicate with it or detect that it is running.

Step 8: Start the remote instance

To start the new remote instance, you can start the cluster again, as you already did before. Starting a cluster if you already started it before will keep the “instance1” instance running, and will start all other instances which are not running.

  1. Navigate to “Clusters”
  2. Click the checkbox in the “Select” column next to your cluster
  3. Click “Start Cluster” button and wait until the cluster is started

Alternatively, you can start the remoteInstance individually:

  1. Navigate to the “Instances” tab
  2. Select remoteInstance and click on the “Start” button.

Step 9: Verify Load Balancing

This assumes that you’ve already set up a load balancer server, e.g. Apache HTTP server, and the load balancer is configured to support sticky sessions.

Access the tester application you deployed previously through the load balancer and verify that requests are load-balanced between the instances:

http://load-balancer-ip:load-balancer-port/cluster

Replace load-balancer-ip and load-balancer-port with the appropriate values for your load balancer server.

  • Verify that after an HTTP session is created, your requests are served by the same GlassFish instance and your session data remains in the session.
  • After you reset the session, it’s possible that your future requests will be served by a different GlassFish instance

After everything is working, you can undeploy the test application clusterjsp and deploy your own application. Remember to select your cluster as the deployment target, so that your application is deployed to all GlassFish instances in the cluster.

Step 10: Deploy your application to the cluster

Before you deploy your application to the cluster, make sure that all resources that your application requires are deployed to the cluster too. For example, a JDBC resource:

JDBC resource targets

Now, deploy your application to the cluster, similarly as you deployed the tester application before in the Step 5:

  1. In GlassFish Admin Console, navigate to Applications and click the “Deploy…” button
  2. In the “Targets” section, click on your cluster in the “Available Targets” column and click the “Add >” button
  3. Configure other deployment properties and click “OK”

Summary

That’s it! You have now created a GlassFish cluster with instances, added an SSH node, and created an instance on this node using the Admin Console. Your application is running on all instances of the cluster. Now you can set up a load balancer server (e.g. Apache HTTP Server) with sticky sessions to proxy incoming requests to GlassFish instances, which we’ll cover in a future article.

See also

Last updated 2024-01-28 13:40:07 +0100

Series NavigationHow to: Apache Server as a Load Balancer for your GlassFish Cluster >>

Leave a Comment

Your email address will not be published. Required fields are marked *

Captcha loading...

Scroll to Top