- How to: Set up a GlassFish Cluster
- How to: Apache Server as a Load Balancer for your 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
- Step 2: Create a Cluster
- Step 3: Add routing config for sticky sessions
- Step 4: Start the cluster
- Step 5: test the Cluster
- Step 6: Add an SSH Node
- Step 7: Create an Instance on the SSH Node
- Step 8: Start the remote instance
- Step 9: Verify Load Balancing
- Step 10: Deploy your application to the cluster
- Summary
- See also
Step 1: Start the Default Domain
- Open a terminal and navigate to the
bin
directory of your GlassFish installation. - Start the default domain:
asadmin start-domain
- 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
- Navigate to “Clusters”
- Click on the “New…” button to create a new cluster.
- Enter a name for the cluster (e.g.,
myCluster
) - 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” - 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).
Step 3: Add routing config for sticky sessions
- Navigate to
Clusters → myCluster
- Click on the “myCluster-config” link in the “Configuration” field
- Click on “System Properties” to open the “System Properies” configuration for the cluster
- Click on “Add Property” button
- In the new row, set the value in the “Instance Variable Name” column to “jvmRoute”
- 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.
Step 4: Start the cluster
- Navigate to “Clusters”
- Click the checkbox in the “Select” column next to your cluster
- Click “Start Cluster” button and wait until the cluster is started
- 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.
Step 5: test the Cluster
Deploy a cluster tester application, which you can download from //github.com/OmniFish-EE/clusterjsp/releases.
- In GlassFish Admin Console, navigate to
Applications
and click the “Deploy…”
button - In the “Targets” section, click on your cluster in the “Available Targets” column and click the “Add >” button
- In the “Location” section, click
Browse…
and select the application WAR file - Set the “Context Root” field to “cluster”
- Click “OK”
- 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.
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:
- In GlassFish Admin Console, navigate to
Nodes
. There’s always at least 1 node, e.g.localhost-domain1
, which represents the local machine - Click on the “New…” button to add a new SSH node.
- Enter a name for the SSH node (e.g.,
sshNode1
). - Set the “Type” to “SSH”
- Set the “Host” to the IP address of the remote machine.
- If you want to install GlassFish on the remote machine, enable the ckeckbox “Install GlassFish Server”
- 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
- Set the “SSH User Authentication” based on your authentication method. Fill in the authentication details, e.g. “SSH User Password” for “Password” authentication
- Click “OK” to add the SSH node.
Step 7: Create an Instance on the SSH Node
- Navigate to
Clusters → myCluster
. - Select the “Instances” tab.
- Click on the “New…” button to create a new instance.
- Enter a name for the new instance (e.g.,
remoteInstance
) and select the SSH node (sshNode1
) as the target “Node”. - 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:
- Navigate to
Clusters → myCluster
, the tab “Instances” - Click on “remoteInstance” in the “Name” column
- The admin port number is the first port in the “HTTP Port(s)” field
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.
- Navigate to “Clusters”
- Click the checkbox in the “Select” column next to your cluster
- Click “Start Cluster” button and wait until the cluster is started
Alternatively, you can start the remoteInstance
individually:
- Navigate to the “Instances” tab
- 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:
Now, deploy your application to the cluster, similarly as you deployed the tester application before in the Step 5:
- In GlassFish Admin Console, navigate to
Applications
and click the “Deploy…”
button - In the “Targets” section, click on your cluster in the “Available Targets” column and click the “Add >” button
- 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