3

How to deploy WildFly Bootable jar on OpenShift

 2 years ago
source link: http://www.mastertheboss.com/eclipse/eclipse-microservices/how-to-deploy-wildfly-bootable-jar-on-openshift/?utm_campaign=how-to-deploy-wildfly-bootable-jar-on-openshift
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to deploy WildFly Bootable jar on OpenShift

8 October 2021 by F.Marchioni

The WildFly Bootable JAR Maven plugin allows to package both the server and your application in a bootable JAR. The Bootable jar can then be run from the command line having Java installed on your machine. In this tutorial we will learn how to deploy a Bootable Jar application on a Enterprise Kubernetes (OpenShift) environment.

You can build a Bootable Jar application thanks to a Maven plugin which does all the magic. Within this plugin, you can specify the target version of the application server and the runtime layers you want to add. We also need a plugin to deploy it automatically on OpenShift.

Configuring the Bootable Jar plugin for the Cloud

In order to deploy the bootable Jar runtime on Kubernetes, we will create a Maven profile named “bootable-jar-openshift” which contains two plugins in it:

<profile>
<id>bootable-jar-openshift</id>
<build>
<plugins>
<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>jaxrs-server</layer>
<layer>microprofile-platform</layer>
</layers>
<plugin-options>
<jboss-fork-embedded>true</jboss-fork-embedded>
</plugin-options>
<cloud/>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jkube</groupId>
<artifactId>openshift-maven-plugin</artifactId>
<version>${version.jkube.maven.plugin}</version>
<executions>
<execution>
<goals>
<goal>resource</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<enricher>
<config>
<jkube-service>
<type>NodePort</type>
</jkube-service>
</config>
</enricher>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
  <id>bootable-jar-openshift</id>
  <build>
    <plugins>
      <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>jaxrs-server</layer>
            <layer>microprofile-platform</layer>
          </layers>
          <plugin-options>
            <jboss-fork-embedded>true</jboss-fork-embedded>
          </plugin-options>
          <cloud/>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>package</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jkube</groupId>
        <artifactId>openshift-maven-plugin</artifactId>
        <version>${version.jkube.maven.plugin}</version>
        <executions>
          <execution>
            <goals>
              <goal>resource</goal>
              <goal>build</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <enricher>
            <config>
              <jkube-service>
                <type>NodePort</type>
              </jkube-service>
            </config>
          </enricher>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>
  • The plugin wildfly-jar-maven-plugin it’s responsible for the creation of the Bootable JAR. it builds a runnable runtime using two Galleon layers: jax-rs-server and microprofile-platform
  • Next, the openshift-maven-plugin plugin which generates container images and deploys Kubernetes/OpenShift manifests at compile time too.

More about openshift-maven-plugin in this tutorial: Building and deploying a Jakarta EE application on OpenShift

With that configuration in place, let’s deploy an example application on OpenShift.

Deploying a sample application to OpenShift

We will now deploy a sample application which uses Microprofile Health Checks. The application includes a Database Health check:

@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");
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();
@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();

    }

}

Firstly, we will start our OpenShift Cluster. We will use Code Ready Containers as OpenShift cluster.

$ crc start
$ crc start

Next, deploy the required Database instance so that Health check will not prevent our application from failing to start:

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

Then, we will deploy our application on OpenShift using JKube Maven plugin. This is available in our “bootable-jar-openshift” profile so here’s how to deploy the Bootable Jar application:

mvn oc:deploy -Pbootable-jar-openshift
mvn oc:deploy -Pbootable-jar-openshift

In a couple of minutes the application will be available on OpenShift

oc get pods
NAME READY STATUS RESTARTS AGE
microprofile-health-1-deploy 0/1 Completed 0 136m
microprofile-health-1-nscwz 1/1 Running 0 136m
microprofile-health-s2i-1-build 0/1 Completed 0 137m
postgresql-85b5b9bcc-5756g 1/1 Running 0 141m
oc get pods
NAME                              READY   STATUS      RESTARTS   AGE
microprofile-health-1-deploy      0/1     Completed   0          136m
microprofile-health-1-nscwz       1/1     Running     0          136m
microprofile-health-s2i-1-build   0/1     Completed   0          137m
postgresql-85b5b9bcc-5756g        1/1     Running     0          141m

Just add a Service which is able to reach the port 9990 where health checks are available:

apiVersion: v1
kind: Service
metadata:
name: microprofile-health-mgmt
namespace: demo
spec:
selector:
app: microprofile-health
ports:
- protocol: TCP
port: 9990
targetPort: 9990
apiVersion: v1
kind: Service
metadata:
  name: microprofile-health-mgmt
  namespace: 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 --port 9990 --path=/health
oc expose svc/microprofile-health-mgmt --port 9990 --path=/health

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 /health microprofile-health-mgmt 9990
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   /health   microprofile-health-mgmt   9990   

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

curl -s http://microprofile-health-mgmt-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"
curl -s http://microprofile-health-mgmt-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.

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

Post Views: 146

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK