Clustering WildFly on Openshift using WildFly Operator

Do you want to learn how to start quickly a WildFly cluster running on Openshift using WildFly Operator? then keep reading the rest of this article!

First of all, what is an Operator? In essence, an Operator is a standard method of packaging, deploying and managing a Kubernetes application. With OpenShift 4, everything is deployed as an Operator. And to keep things simple, Red Hat has integrated the Kubernetes Operator Hub into OpenShift 4. I encourage you to go have a look at the Operator Hub, which is available at:

As you can see, the Operator Hub contains shortcuts for installing the Operator on all applications listed in the Hub:

wildfly operator tutorial

Requirements to install Operators

In order to install the Operator we need to make sure kubectl binary is installed on your machine. You can download the latest release with the command:

$ curl -LO`curl -s`/bin/linux/amd64/kubectl

Then, make the kubectl binary executable.

$ chmod +x ./kubectl

Move the binary in to your PATH.

$ sudo mv ./kubectl /usr/local/bin/kubectl

And finally test to ensure the version you installed is up-to-date:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.3", GitCommit:"2d3c76f9091b6bec110a5e63777c332469e0cba2", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:54Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}

Installing WildFly Operator

We will follow the instructions contained in

So at first install the Operator Lifecycle Manager (OLM), which is a tool to help manage the Operators running on your cluster.

curl -sL | bash -s 0.10.0

Then, we can install WildFly Operator itself by running kubectl against WildFly’s Operator YAML file:

$ kubectl create -f created

After installing, verify that the Operator is listed in your resources with:

$ kubectl get csv -n operators

NAME                      DISPLAY   VERSION   REPLACES                  PHASE
wildfly-operator.v0.2.0   WildFly   0.2.0     wildfly-operator.v0.1.0   Succeeded

So from now, you have a Custom Resource Definition named WildFlyServer which can be used to deliver new instances of WildFly Application Server. At minimum, you can provide an application image to it and it will be built on the top of WildFly:

kind: WildFlyServer
  name: quickstart
  applicationImage: ''
  size: 1

Notice the parameter applicationImage which refererences a Docker Image and size which is the number of Pods that will be started with that Image.

Let’s see now a more complex example which involves a custom WildFly configuration to be loaded by the Operator.

Clustering WildFly using the Operator

To install a WildFly cluster, we will need to provide the HA XML configuration as a ConfigMap that is accessible by the Operator. Let’s see how to do it. First of all, some grants are required, so provide to your user the service account Role for your project with:

oc policy add-role-to-user view system:serviceaccount:$(oc project -q):default -n $(oc project -q)
role "view" added: "system:serviceaccount:myproject:default"

Then, we will use the example configuration from GitHub:

As said, the standalone XML file must be put in a ConfigMap that is available to the operator. The standaloneConfigMap must provide the name of this ConfigMap as well as the key corresponding to the name of standalone XML file.

Pick up the standalone-openshift.xml from the config folder and create a ConfigMap with:

$ kubectl create configmap clusterbench-config-map --from-file standalone-openshift.xml 
configmap/clusterbench-config-map created

Now, we will add the Custom Resource Definition, which is available in the crds folder:

$ kubectl apply -f clusterbench.yaml created

Great! The cluster has been created. As you can see from the CRD, the clusterbench.yaml file will start 2 Pods in Cluster:

kind: WildFlyServer
  name: clusterbench
  applicationImage: ""
  size: 2
    name: clusterbench-config-map
key: standalone-openshift.xml

This is verified by:

$ oc get pods
clusterbench-0   1/1       Running   0          3m
clusterbench-1   1/1       Running   0          2m

wildfly operator openshift tutorial

A service named clusterbench-loadbalancer has been created as well:

$ oc get services
NAME                        TYPE           CLUSTER-IP      EXTERNAL-IP                   PORT(S)          AGE
clusterbench-loadbalancer   LoadBalancer,   8080:31886/TCP   3d

In order to test the application, expose the service clusterbench-loadbalancer with a Route:

$ oc expose svc/clusterbench-loadbalancer exposed

Now let’s try to access the application on the browser with:

You will see that by refreshing, the page counter keeps incrementing:

wildfly operator openshift tutorial

Now let’s scale down the Pods, by editing the Operator configuration:

kubectl edit wildflyserver clusterbench

Set the size parameter to 1 and save the changes from the editor. Next, check that the Pods have scaled down:

wildfly operator openshift tutorial

Let’s try to access again the application on the browser with:

wildfly operator openshift tutorial

As you can see, the session counter keeps increasing, even if we scaled down the number of Pods.

Great! We just managed to set up a basic example of WildFly cluster on Openshift using WildFly Operator. Replace the example image with your application image to see your application running on a WildFly cluster.