8 min read

Configuring load balancers

For a cluster with multiple server instances, using a load balancer in front of the server instances not only simplifies the client’s view of the system through a single address, but also improves the overall system’s reliability. If one server fails, the load balancer can detect the failure and distribute the load to other live server instances. In this section, we will discuss the load balancer support in GlassFish, and how to configure it.

GlassFish can work with a variety of load balancers, both hardware and software based. For example, the GlassFish project maintains and releases a load balancer plug-in, which works with Apache, Sun Java System Web Server, and Microsoft IIS. The load balancer plug-in is freely downloadable at http://download. java.net/javaee5/external//aslb/jars, where indicates the operating system on which the web server is running.

Also, GlassFish can be load balanced by Apache using mod_jk. However, the mod_jk based load balancing is not officially supported in GlassFish 2. In this article, we will focus on the GlassFish load balancer plugin.

The mod_jk based load balancing will be supported in GlassFish 3.

The GlassFish load balancer plug-in accepts HTTP and HTTPS requests and forwards them to one of GlassFish instances in the cluster. It uses a health checker to detect server instance status. If a server instance fails, requests are redirected to existing, available machines. Once a failed server instance comes back online, the load balancer can also recognize when a failed instance has recovered and redistributed the load accordingly.

Like most load balancer solutions for web applications, the GlassFish load balancer plugin implements session affinity or sticky session. On the first request from a client, a new session is established on the server instance node that is processing the request. The load balancer routes all subsequent requests for this session to that particular instance.

The load balancer plugin configuration actually involves two steps. First, we need to configure the web server to enable the load balancer; we then need to configure the GlassFish Server to specify how the load balancer should be configured for a cluster, or an application in a cluster.

In the following section, let’s discuss how to configure the Apache web server for the load balancer plugin.

Configuring the load balancer plug-in for Apache web server

The Apache web server is the most popular web server. Its modular architecture makes it easy to extend for additional functionality. The GlassFish load balancer plugin for Apache is configured as an additional module. In this section, we discuss how to configure this module.

In this section, we discuss the configuration of the load balancer plug-in for the default Apache server, version 2.2, with SSL enabled on port 443, installed on Solaris x86. The configuration process for other operating systems is very similar; the only difference is the directory structure of the Apache server. For more detailed information, you can refer to the GlassFish High Availability Administration Guide, located at http://docs.sun.com/app/docs/doc/821-0182.

Complete the following steps to configure the load balancer plug-in on the Apache web server:

  1. Download the latest load-balancer plug-in from http://download.java.net/javaee5/external//aslb/jars. Extract the JAR file, and then extract the two ZIP files, SUNWaspx.zip and SUNWaslb.zip to a directory, $lbplug-in_root.
  2. Copy the errorpages directory from $lbplug-in_root/lib/webserverplug-in//apache2.2 to /usr/apache2/2.2/modules/errorpages. The errorpages directory contains the web pages used by the load balancer plug-in to render load balancer error messages.
  3. Copy the two files, LBPlug-inDefault_root.res and LBPlug-inD_root.res from $lbplug-in_root/lib/webserver-plug-in//apache2.2 to the /usr/apache2/2.2/modules/resource directory.
  4. Copy the three files, cert8.db, key3.db, and secmpd.db from $lbplugin_root/lib/webserver-plug-in//apache2.2 to the /usr/apache2/2.2/sec_db_files directory.
  5. Copy the file mod_load_balancer.so from $lbplug-in_root/lib/webserver-plug-in//apache2.2 to the /usr/apache2/2.2/libexec directory.
  6. Copy the file loadbalancer.xml.example from $lbplug-in_root/lib/install/templates to the /etc/apache2/2.2/loadblancer.xml. The loadbalancer.xml file is the primary configuration file that defines the load balancer. We will discuss this file in the next section. At this moment, we can consider it as a place holder.
  7. Copy the file sun-loadbalancer_1_2.dtd from $lbplug-in_root/lib/dtds to /etc/apache2/2.2.
  8. Append the following elements to /etc/apache2/2.2/conf.d/ modules-32.load:
    LoadModule apachelbplug-in_module libexec/mod_loadbalancer.so

  9. Add the following definition of the LD_LIBRARY_PATH variable to /etc/apache2/2.2/envvars:
  10. Copy the mpm.conf file from the /etc/apache2/2.2/envvars/samplesconf.d directory to /etc/apache2/2.2/envvars/conf.d, and change the values of the StartServers and MaxClients variables in the file to 1. This change is necessary. Otherwise, every new session request will spawn a new Apache process and the load balancer plug-in will be initialized resulting in requests landing in the same instance.
  11. Restart the Apache server.

Once we have applied these steps to the Apache web server, it is ready to work as a load balancer for our GlassFish cluster. As we discussed earlier, the essential configuration file for the load balancer plug-in is loadbalancer.xml. This file captures the essential information about the server instances being load balanced, and the load distribution configuration. This information is actually generated on the GlassFish Server, and then transferred to the web server. In the next section, let’s discuss how we can configure the load balancer information on GlassFish.

Configuring GlassFish for load balancing

Once the web server has been configured, we can define a GlassFish load balancer configuration. This can be done using the Admin Console, or the create-http-lb command of the asadmin CLI utility. To create a load balancer configuration using the Admin Console, click the HTTP Load Balancer node in the navigational panel, and then click New in the content panel. The following screenshot shows the input form for creating a load balancer configuration.

GlassFish Administration

The important input parameters for the load balancer configuration are explained as follows:

  • Name: A unique name of the new load balancer configuration.
  • All instances: Where all the server instances of the selected target will be load balanced.
  • All applications: Whether all the applications deployed to the target will be load balanced.
  • Device host and admin port: The load balancing device’s server information. GlassFish can rely on this information to automatically push the new load balancer configuration information to the device.
  • For software load balancers, the device is the server that runs the load balancing software. The admin port is the port through which administration tasks can be performed on the load balancing device. The port must be SSL enabled. If the device requires authentication, you should configure the device to support client certificate authentication. The information is used purely for pushing the load balancer configuration. Therefore, if you have a different mechanism to transfer the information, the device host and admin port values are not important, even though they are necessary.

  • Automatically apply changes: Whether GlassFish pushes the new load balancer configuration information immediately to the device.
  • Targets: The clusters and server instances that will participate in the load balancing.

Once the load balancer configuration is created, we can modify the settings for the load balancer, including the following parameters:

  • Response timeout: Time in seconds within which a server instance must return a response. If no response is received within the time period, the server is considered unhealthy. The default is 60.
  • HTTPS routing: Whether HTTPS requests to the load balancer result in HTTPS or HTTP requests to the server instance. For more information, see Configuring HTTPS Routing.
  • Reload pool interval: Interval between checks for changes to the load balancer configuration file loadbalancer.xml. When the check detects changes, the configuration file is reloaded. A value of 0 disables reloading.
  • Monitoring: Whether monitoring is enabled for the load balancer.
  • Route cookie: Whether to use cookie to store the session routing information. Name of the cookie the load balancer plug-in uses to record the route information.
  • Target: Target for the load balancer configuration. If you specify a target, it is the same as adding a reference to it. Targets can be clusters or stand-alone instances.

In addition, we can configure the load balancing algorithm, and the health checker of the load balance plug-in. By default, the load balancer uses simple round-robin mechanism to distribute the load across server instances. The load balancer plug-in also supports a weighted round robin algorithm; it allows us to favor certain instances based on their relative weights. If neither algorithm satisfies the requirement, we can develop a custom algorithm.

By default, the health checker of the load balancer plugin uses a specified URL to check all unhealthy GlassFish instances, and determines if they have returned to the healthy state. If the health checker finds that an unhealthy instance has become healthy, that instance is added to the list of healthy instances, and the load balancer will distribute load to it.

To configure the health checker, click the Target tab of the load balancer configuration form, and click the Edit Health Check link of a specific target. The load balancing algorithm and health checker configuration form is shown in the following screenshot.

GlassFish Administration

If we click the Export tab in the load balancer editing form, we get the options to either export the load balancer configuration as loadbalancer.xml file. This file can be copied to the load balancer host, which effectively updates the load balancer information. We can also click Apply Changes Now to push the configuration, provided that the load balancer device is configured appropriately.

Subscribe to the weekly Packt Hub newsletter. We'll send you this year's Skill Up Developer Skills Report.

* indicates required


Please enter your comment!
Please enter your name here