
Archiving automatic cluster failover
In order to archive automatic failover between instances, the application provider should set up a load balancer in front of his backend nodes.
In a load balancing configuration, clients wanting to access the application pass through a unique intermediate URL, that is, the load balancing URL, which, upon receiving a client request, forwards it to an active node.
There are various ways to implement simple load balancers: HAProxy, Apache httpd, even the JBoss Undertow subsystem can be configured to act as a load balancer. We will look at how to set up load balancing using Undertow in Chapter 3, Custom Web Deployment using Undertow and Swarm.
For this lab, we are relying on HAProxy for its simplicity and advanced routing features.
Install the HAproxy as recommended for your target operating system following the official docs:
$haproxy -v
HA-Proxy version 1.7.3 2017/02/28
Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>
Edit a haproxy.cfg configuration file to set up the frontend and backend. Here's the most important part for our example:
listen stats
bind *:9000
stats enable
stats hide-version
stats realm haproxyStatistics
stats auth haproxy:haproxy
stats uri /haproxy
frontend haproxy_in
bind *:9001
default_backend beosbank_http
mode http
backend beosbank_http
mode http
balance roundrobin
server node13 127.0.0.1:8380 check
server node23 127.0.0.1:8780 check
The full haproxy.cfg file used is in the BeOSbank-web app resource folder for your reference (beosbank-web/src/main/resources/haproxy.cfg).
We defined a backend beosbank_http with two nodes: node13 and node23. HAProxy frontend is configured to listen on port 9001 and balance load following the round robin algorithm, and provide statistics on port 9000. Stats are restricted to the haproxy/haproxy user only.
Start the HAProxy server with the -f option to pass the custom configuration file:
$ haproxy -f haproxy.cfg
In your web browser, open the HAProxy URL, http://localhost:9001/beosbank-web/:

The load balancer forwards the request to node13
To verify the automatic failover with HA Proxy, use the following steps.
- Fill in the Transfer details tabs and Payment Infos tab.
- Shut down node13 using the following CLI Command:
[domain@127.0.0.1:9999 /] /host=host1/server-config=node13:stop
{
"outcome" => "success",
"result" => "STOPPING"
}
HAProxy detects that this node is now down and you can see this from the HAProxy console logs:
[WARNING] 077/190646 (14290) : Server beosbank_http/node13 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Now, refresh your browser. You will automatically be redirected to node23 by HAProxy, and your screens will be similar to the previous one. As the session is replicated on node23, the user does not lose his data and will be automatically switched to a working node when the node on which the session was initiated goes down. This is an automatic failover using a load balancer:
You can also see HAProxy statistics at http://localhost:9000/haproxy: