How to deploy WildFly Bootable JAR on OpenShift

In this updated article we will learn how to deploy an application built with WildFly Bootable JAR Maven on OpenShift using JKube Maven plugin. We will show how to add the plugin configuration to your Maven project and how to run and test the application on OpenShift.

JKube Maven Plugin in a Nutshell

Eclipse JKube is a suite of plugins and libraries designed to streamline the container image-building process, generate OpenShift manifests during compilation, and simplify the creation of essential tasks for cloud-ready applications, such as liveness and readiness probes. JKube also offers an OpenShift Maven plugin that enhances Maven’s capabilities, allowing developers to effortlessly configure the resources required for an application to operate in a PaaS environment.

There are several way to plug in Eclipse JKube Plugin in your Maven project. In this article we will be using the XML configuration Model which inherits the XML configuration for building images from the docker-maven-plugin and provides the same functionality. It supports an assembly descriptor for specifying the content of the Docker image.

In the next section we will show how to combine the Eclipse JKube Maven plugin with WildFly Maven JAR Plugin.

Step 1: Code a Basic MicroProfile application

For the purpose of this example, we will be coding a simple MicroProfile application which performs some Health Checks. The Health Check is a Readiness check which pings through port 5432 to reach PostgreSQL Server:

@Readiness
@ApplicationScoped
public class DatabaseHealthCheck implements HealthCheck {

	@Inject
	@ConfigProperty(name = "POSTGRESQL_SERVICE_HOST", defaultValue = "localhost")
	private String host;
	
	@Inject
	@ConfigProperty(name = "POSTGRESQL_SERVICE_PORT", defaultValue = "5432")
	private int port;

    @Override
    public HealthCheckResponse call() {

        HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("Database connection health check");
     
        try {
            pingServer(host, port);
            responseBuilder.up();
        } catch (Exception e) {

            responseBuilder.down()
                    .withData("error", e.getMessage());
        }

        return responseBuilder.build();
    }

    private void pingServer(String dbhost, int port) throws IOException
    {
        Socket socket = new Socket(dbhost, port);
        socket.close();

    }

}

You can learn more about Health Checks in this article: How to use Microprofile Health Checks for your Services

In order to be able to perform this check, we will bootstrap a PostgreSQL Database. Firstly, create a new project:

oc new-project wildfly-demo

Then, add a PostgreSQL Database to your namespace as follows:

oc new-app -e POSTGRESQL_USER=admin -e POSTGRESQL_PASSWORD=mypassword123 -e POSTGRESQL_DATABASE=postgresdb postgresql

Step 2: Configuring the Bootable JAR plugin for the Cloud

Next, we need to configure WildFly bootable JAR so that it includes the layers to run on the Cloud. Therefore, we will add the cloud-server. Also, since we will use Health Checks in the application, we will also include the microprofile-health layer. :

<plugin>
	<groupId>org.wildfly.plugins</groupId>
	<artifactId>wildfly-jar-maven-plugin</artifactId>
	<version>${version.wildfly-jar.maven.plugin}</version>
	<configuration>
		<feature-pack-location>wildfly@maven(org.jboss.universe:community-universe)#${version.wildfly}</feature-pack-location>
		<layers>
			<layer>cloud-server</layer>
			<layer>microprofile-health</layer>
		</layers>
		<excluded-layers>
			<layer>deployment-scanner</layer>
		</excluded-layers>
		<cloud/>
		<plugin-options>
			<jboss-fork-embedded>${plugin.fork.embedded}</jboss-fork-embedded>
		</plugin-options>
	</configuration>
	<executions>
		<execution>
			<goals>
				<goal>package</goal>
			</goals>
		</execution>
	</executions>
</plugin>

Once you build the application with the WildFly JAR Maven plugin, you will have in he target folder the Bootable file microprofile-health.war . In the next section we will add JKube Plugin which generates the Kubernetes Manifest Files and allows deploying the application on OpenShift.

Step 3: Configuring JKube Maven Plugin

Finally, we will add JKube Maven plugin to our Project. We can add it to a Profile named “openshift” so that the deployment does not fire each time you build your application:

<profile>
	<id>openshift</id>
	<properties>
		<jkube.generator.from>registry.redhat.io/ubi8/openjdk-17:latest</jkube.generator.from>
	</properties>
	<build>
		<plugins>
			<plugin>
				<groupId>org.eclipse.jkube</groupId>
				<artifactId>openshift-maven-plugin</artifactId>
				<version>${version.jkube.maven.plugin}</version>
				<configuration>
					<resources>
						<env>
							<GC_MAX_METASPACE_SIZE>256</GC_MAX_METASPACE_SIZE>
							<GC_METASPACE_SIZE>96</GC_METASPACE_SIZE>
						</env>
					</resources>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>resource</goal>
							<goal>build</goal>
							<goal>apply</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</profile>

Within this plugin we have configured as properties the target JDK that will run the Bootable JAR which is registry.redhat.io/ubi8/openjdk-17:latest. Also, as an example, we have set some JVM Properties such as the Metaspace settings.

You can generate Manifest files and deploy on OpenShift with just one command:

mvn clean install -Popenshift

At the end of it, you should have also the microprofile-health-1-xxxxx Pod up an running along with the PostgreSQL Server:

wildfly bootable jar openshift

Adding a Service to capture Health Checks

As it is, our application exposes the Route to reach out WildFly on Port 8080.

oc get routes
NAME                       HOST/PORT                                                PATH   SERVICES                   PORT   TERMINATION     WILDCARD
microprofile-health        microprofile-health-wildfly-demo.apps-crc.testing               microprofile-health        8080   edge/Redirect   None

Therefore, if you open the browser and point to the application Route you will see:

MicroProfile Health demo deployed successfully. You can find the available operations in the included README file.

On the other hand, the Health Checks are available on port 9990. Therefore, in order to make it accessible outside of OpenShift cluster, we will add a Service which maps port 9990:

apiVersion: v1
kind: Service
metadata:
  name: microprofile-health-mgmt
  namespace: wildfly-demo
spec:
  selector:
    app: microprofile-health
  ports:
    - protocol: TCP
      port: 9990
      targetPort: 9990

Next, expose the above service through the path “/health” so that a Route is created:

oc expose svc/microprofile-health-mgmt

You should have the following routes available:

oc get routes
NAME                       HOST/PORT                                        PATH      SERVICES                   PORT    
microprofile-health        microprofile-health-demo.apps-crc.testing                  microprofile-health        8080    
microprofile-health-mgmt   microprofile-health-mgmt-demo.apps-crc.testing             microprofile-health-mgmt   9990   

Finally, you can test the Health checks from the command line using the Route we have just created:

curl microprofile-health-mgmt-wildfly-demo.apps-crc.testing/health | jq
{
  "status": "UP",
  "checks": [
    {
      "name": "server-state",
      "status": "UP",
      "data": {
        "value": "running"
      }
    },
    {
      "name": "deployments-status",
      "status": "UP",
      "data": {
        "microprofile-health.war": "OK"
      }
    },
    {
      "name": "boot-errors",
      "status": "UP"
    },
    {
      "name": "MemoryHealthCheck Liveness check",
      "status": "UP"
    },
    {
      "name": "Database connection health check",
      "status": "UP"
    },
    {
      "name": "Application started",
      "status": "UP"
    }
  ]
}

Great! You just managed to deploy a WildFly Bootable Jar application on OpenShift.

Conclusion

This article was a walk through the configuration of JKube Maven plugin to deploy a Bootable application on OpenShift. We have demonstrated how to combine it with WildFly JAR plugin to simplify the deployment of a WildFly application on OpenShift.

The source code for this application is available here: https://github.com/fmarchioni/mastertheboss/tree/master/micro-services/mp-health-check

Found the article helpful? if so please follow us on Socials