Load Balancing Across Payara Server 5 Instances with Apache Web Server

Photo of Mark Wareham by Mark Wareham

*Note: This blog post is an update to Load Balancing Across Payara Server Instances with Apache Web Server, which was written for Payara Server 4.

Introduction

Continuing our introductory blog series (Payara Server 5 Basics), this blog will demonstrate how to add load balancing capability to Apache Web Server and forward to our simple Payara Server cluster.

 

A load balancer can redirect requests to multiple instances, primarily for the purpose of distributing incoming requests between cluster members based on pre-determined rules. This could be a simple "round-robin" algorithm, where the workload is distributed to each instance in turn, or a weighted algorithm where requests are delivered based on a pre-determined weight for each cluster member. A load balancer plays a key role in our cluster:

 

Screen Shot 2018-07-19 at 11.25.30 AM

So far in this series we have:

This blog will demonstrate how you enable Apache Web Server to load balance requests across our cluster using the Apache Web Server mod_proxy_balancer module.

 

Prerequisites

To follow along with this blog, the cluster we created in Part 3 and our Apache configuration from Part 2 will be required.

As it currently stands, this environment is:

  • Our cluster:
    • One Ubuntu 16.04 host with JDK8, Apache Web Server, and Payara Server installed, with a Domain Administration Server and another instance ("local-instance").
    • One Ubuntu 16.04 host with JDK8, OpenSSH, and Payara Server installed, with a single Payara Server instance ("second-Ubuntu-host-instance") controlled by the DAS on the first host.

 

Since this blog is demonstrating Apache's load balancing capabilities, we will need the clusterjsp sample application, which we will deploy to our cluster.

 

Deploying the clusterjsp Application

Clusterjsp is a simple web application which can show HTTP session information alongside details of the server instance which served the request. This should make it clear if Apache is load balancing our requests between our two server instances.

To deploy the clusterjsp application follow these steps:

  • Visit the Admin Console on http://localhost:4848 ( or if not running on the same computer)
  • Click Applications the left hand menu
  • Click Deploy
deploy-1
  • Select your clusterjsp WAR file
  • Choose the deployment group created the previous blog post
  • select_dg-1The application is now deployed, click Launch to display the links to the clusterjsp application:
launch

Now we have the endpoints we need to add to our load balancer! We have the choice of HTTP or HTTPS direct links to the Payara Server instances. Since we are going to use Apache, we can terminate SSL when requests reach the load balancer rather than continuing to pay the performance cost to HTTPS at this stage. It should be noted that the performance cost is not such an issue with HTTP/2. Payara 5 supports HTTP/2, however HTTP/2 still has a few issues that need working out.

 

Setting up Apache Web Server as a Load Balancer

If you have been following this series of blogs you should already have Apache Web Server installed with a basic mod_proxy_http, and your a site configuration called "payaraSite" at /etc/apache2/sites-available/payaraSite.conf, which should look like this:

payaraSite_conf

While this worked when we were using the ProxyPass directives to forward to the Domain Admin Server (DAS - A Payara Server instance that manages the other instances), we now have two cluster members as separate instances at http://computer1:28080/ and http://computer2:28080/  - so we no longer want to send traffic to the DAS.

 

It's not possible to use multiple endpoints in the same ProxyPass directive, so we need to introduce mod_proxy_balancer, which will allow us to point our ProxyPass directive to a list of endpoints.

 

The modified configuration is similar to what we had previously done by forwarding traffic to a specific URL, except the URL will now point to a block of possible endpoints within a <Proxy> tag, and a load balancer method to determine how the proxy is chosen.

  • First, enable the module proxy_balancer and a simple load balancing method using our Apache Web Server helper tools which were installed as part of the Ubuntu apache2 package:

sudo a2enmod proxy_balancer lbmethod_byrequests

The by_requests method of load balancing simply alternates between instances in a round-robin.

  • Reopen the payaraSite.conf file with your chosen text editor.
  • Create a <Proxy> block which contains a list of endpoints where our app is located as shown below. Note that the ports of the clustered instances (as shown in the endpoints screenshot above) are different to those of the DAS:
<Proxy balancer://payara>
BalancerMember http://computer1:28080
BalancerMember http://computer2:28080
</Proxy>

ProxyPass / balancer://payara/
ProxyPassReverse / balancer://payara/

Once we have a list of endpoints, we can change the ProxyPass directives to use one of the endpoints we just specified, by referencing the <Proxy> block. We could create multiple <Proxy> blocks with different names to refer to different lists of endpoints, depending on which applications we wanted to load balance and what endpoints they were deployed to.

In our case, the configuration file should now look something like this:

payaraSite_conf_final
  • Save your configuration and restart Apache:

sudo service apache2 restart

On the host where Apache is installed, whenever we load the page http://localhost/clusterjsp we find that each request alternates between being served from the first or second Payara Server instances:

clusterjsp_result

Summary

So with that done, we have implemented basic load balancing to spread requests across multiple Payara Server instances, thanks to Apache Web Server and its mod_proxy_http and mod_proxy_balancer modules. This configuration is relatively naive and won't take into account session data, so in our next blog, we will enable sticky sessions to make sure that users' sessions will be pinned to a single Payara Server instance, rather than bouncing between the two.

 

For now, if you have any questions, feel free to post a comment below.

 

Need help with your Payara Server Migration?

Learn More About Migration & Project Support

 

Want to get more out of Payara Server in Production?

Payara Enterprise

 

Comments