Configuring HTTP Load Balancing

This chapter describes how to configure HTTP load balancing on Payara Server.

Setting Up HTTP Load Balancing

This section describes how to set up load balancing for Payara Server.

Prerequisites for Setting Up HTTP Load Balancing

Before configuring your load balancer, you must:

Configuring Payara Server with Apache HTTP Server and mod_jk

Payara Server can be configured for load balancing with Apache HTTP Server as a front end by enabling the Apache mod_jk connector module. To enable the mod_jk module in Payara Server, set the Payara Server jk-enabled network-listener attribute.

You can also create jk-connectors under different virtual-servers using the jk-enabled network-listener attribute.

To Configure the mod_jk Connector Module

  1. Install Apache HTTP Server and mod_jk.

  2. Configure workers.properties and httpd.conf. For example:

    • apache2/config/workers.properties

      # Define 1 real worker using ajp13
      worker.list=worker1
      # Set properties for worker1 (ajp13)
      worker.worker1.type=ajp13
      worker.worker1.host=localhost
      worker.worker1.port=8009
    • apache2/conf/httpd.conf

      LoadModule jk_module /Users/Amy/apache2/modules/mod_jk-1.2.25-httpd-2.2.4.so
      JkWorkersFile /Users/Amy/apache2/conf/worker.properties
      # Where to put jk logs
      JkLogFile /Users/Amy/apache2/logs/mod_jk.log
      # Set the jk log level [debug/error/info]
      JkLogLevel debug
      # Select the log format
      JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
      # JkOptions indicate to send SSL KEY SIZE,
      JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
      # JkRequestLogFormat set the request format
      JkRequestLogFormat "%w %V %T"
      # Send everything for context /examples to worker named worker1 (ajp13)
      JkMount /examples/* worker1
  3. Start the Apache HTTP Server.

  4. Create a jk-enabled network listener by using the create-network-listener subcommand.

    asadmin> create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true jk-connector
  5. If you are using an external file (like payara-jk.properties), set the jk-configuration-file property of the network listener to its absolute path’s location.

    asadmin> set server-config.network-config.network-listeners.network-listener.jk-connector.jk-configuration-file=domain-dir/config/payara-jk.properties
  6. If you expect to need more than five threads for the listener, increase the maximum threads in the http-thread-pool pool:

    asadmin> set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=value
  7. Restart the Payara Server instances.

This example shows an httpd.conf file that is set for load balancing.

LoadModule jk_module /usr/lib/httpd/modules/mod_jk.so
JkWorkersFile /etc/httpd/conf/worker.properties
# Where to put jk logs
JkLogFile /var/log/httpd/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to Payara Server
JkMount /*.jsp worker1
# Send all glassfish-test requests to Payara Server
JkMount /payara-test/* loadbalancer

The following example shows a workers.properties or payara-server-jk.properties file that is set for load balancing. The worker.worker*.port should match with the JK ports you configured:

worker.list=worker1,worker2,loadbalancer
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.lbfactor=1
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300
worker.worker2.type=ajp13
worker.worker2.host=localhost
worker.worker2.port=8010
worker.worker2.lbfactor=1
worker.worker2.socket_keepalive=1
worker.worker2.socket_timeout=300
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=worker1,worker2

Configuring Payara Server with Apache HTTP Server and mod_proxy_ajp

Payara Server can be configured for load balancing with Apache HTTP Server as a front end by enabling the Apache mod_proxy_ajp connector module.

To enable the mod_proxy_ajp module in Payara Server, set the Payara Server jk-enabled network-listener attribute. You can also create jk-connectors under different virtual-servers using the jk-enabled network-listener attribute.

To Configure the mod_proxy_ajp Connector Module

  1. Install Apache HTTP Server.

  2. Configure the httpd.conf configuration file. For example:

    LoadModule proxy_module /usr/lib/httpd/modules/mod_proxy.so
    LoadModule proxy_ajp_module /usr/lib/httpd/modules/mod_proxy_ajp.so
    
    Listen 1979
    NameVirtualHost *:1979
    <VirtualHost *:1979>
       ServerName localhost
       ErrorLog /var/log/apache2/ajp.error.log
       CustomLog /var/log/apache2/ajp.log combined
    
       <Proxy *>
         AddDefaultCharset Off
         Order deny,allow
         Allow from all
       </Proxy>
    
       ProxyPass / ajp://localhost:8009/
       ProxyPassReverse / ajp://localhost:8009/
    </VirtualHost>
  3. Start the Apache HTTP Server.

  4. Create a jk-enabled network listener by using the create-network-listener subcommand.

    asadmin> create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true jk-connector
  5. If you expect to need more than five threads for the listener, increase the maximum threads in the http-thread-pool pool:

    asadmin> set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=value
  6. Restart the Payara Server instances.

HTTP Load Balancer Deployments

You can configure your load balancer in different ways, depending on your goals and environment, as described in the following sections:

Using Clustered Server Instances

The most common way to deploy the load balancer is with a cluster or clusters of server instances. By default, all the instances in a cluster have the same configuration and the same applications deployed to them. The load balancer distributes the workload between the server instances and requests fail over from an unhealthy instance to a healthy one. If you’ve configured HTTP session persistence, session information persists when the request is failed over.

If you have multiple clusters, requests can be load balanced across clusters but are only failed over between the instances in a single cluster. Use multiple clusters in a load balancer to easily enable rolling upgrades of applications.

Using Multiple Standalone Instances

It is also possible to configure your load balancer to use multiple standalone instances, and load balance and failover requests between them. However, in this configuration, you must manually ensure that the standalone instances have homogenous environments and the same applications deployed to them.

Because clusters automatically maintain a homogenous environment, for most situations it is better and easier to use clusters.

Load balancing across multiple standalone instances only provides failover for requests, and any associated HTTP session data will not be failed over.
This is another reason why using a cluster, which can provide session failover, is a more desirable load balancing configuration.