Skip to content

OpenShift Application with WildFly Operator :: Step by Step

Marco Sappé Griot edited this page Nov 16, 2023 · 15 revisions

Describing steps to deploy an application with WildFly (or WildFly bootable jar) to OpenShift. For starting the OpenShift on localhost check first the guide on WildFly Operator.

OpenShift WildFly S2I image deployment

  • Start the WildFly Operator on OpenShift (see https://github.com/wildfly/wildfly-operator/)

  • Start the PostgreSQL on OpenShift

    Note
    We cannot use the simple new-app command as we need to permit XA transactions with max-prepared-transactions. The args of command is used for it in PostgreSQL image.
    The new-app does not provide a way to pass the args.
    # using username/password as 'admin/admin' with database 'sampledb'
    oc create -f config/postgresql.deployment.yaml
    
    # from transaction-xa example
    oc create -f \
      https://raw.githubusercontent.com/ochaloup/wildfly-jar-maven-plugin/6b5c47132e4a0c27b066faa99846d59ab9b36b5c/examples/transactions-xa/config/postgresql.deployment.yaml
    #  from wildfly quickstarts
    oc create -f \
     https://raw.githubusercontent.com/wildfly/quickstart/23.0.0.Final/ejb-txn-remote-call/client/extensions/postgresql.deployment.yaml
  • Configure the WildFly image stream

    # Create WildFly build image streams
    oc create -f https://raw.githubusercontent.com/wildfly/wildfly-s2i/v21.0/imagestreams/wildfly-centos7.json
    Note

    For creating the runtime image it can be defined the runtime image stream and process the chain build. It will be like this

    # runtime image stream definition
    oc create -f https://raw.githubusercontent.com/wildfly/wildfly-s2i/v21.0/templates/wildfly-runtime-imagestream.yml
    # chain build template definition
    oc create -f https://raw.githubusercontent.com/wildfly/wildfly-s2i/v21.0/templates/wildfly-s2i-chained-build-template.yml
    oc new-app --template wildfly-s2i-chained-build-template -p IMAGE_STREAM_NAMESPACE=$(oc project -q) \
      -p APPLICATION_NAME="transactions-xa" \
      -p SOURCE_REPOSITORY_URL=https://github.com/wildfly-extras/wildfly-jar-maven-plugin/.git \
      -p SOURCE_REPOSITORY_REF=main -p CONTEXT_DIR=examples/transactions-xa
  • Build war on the local machine and build the s2i image on OpenShift

    # build the quickstart
    mvn clean install -Dno-bootable-jar
    # prepare the war artifact to an empty directory to upload it with binary build
    mkdir target/openshift
    cp target/transactions-xa*.war target/openshift
    # define binary build which creates ImageStream and BuildConfig at OpenShift
    oc new-build --strategy source --binary --image-stream wildfly --name transactions-xa
    # starts build by pushing the war archive content to the ImageStream
    ## a Build object will be created and Pod running the build will be started
    oc start-build transactions-xa --from-dir examples/transactions-xa/target/openshift
  • Deploy WildFly server definition to be picked by WildFly Operator

    oc create -f examples/transactions-xa/config/transactions-xa-cr.yaml
    Note

    To avoid clustering error on running the Pod (error type .. failed getting JSON response from Kubernetes …​ java.io.IOException: Server returned HTTP response code: 403 for URL: https://172.25.0.1:443/api/v1/namespaces/mywfly/pods?labelSelector= …​) the Pod needs to have permissions to view labels in the namespace. Quick fix is to execute

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

    The transactions-xa-cr.yaml file can be adjusted to form of OpenShift template and parametrized on the fly.
    The template content is like

    cat > /tmp/operator-template-cr.yaml <<EOF
    # OpenShift template for crating WildFlyServer object parametrized with application image
    apiVersion: v1
    kind: Template
    metadata:
      name: wfly-operator-template
      annotations:
        description: "Starting WildFlyOperator application"
        tags: "wildfly,operator,application"
    parameters:
      - description: Application name
        name: APPLICATION_NAME
        value: transactions-xa
      - description: S2I built application image
        name: APPLICATION_IMAGE
        value: image-registry.openshift-image-registry.svc:5000/mywfly/transactions-xa:latest
    labels:
      template: "wildfly-operator-application-template"
    objects:
      - apiVersion: wildfly.org/v1alpha1
        kind: WildFlyServer
        metadata:
          name: \${APPLICATION_NAME}
        spec:
          applicationImage: \${APPLICATION_IMAGE}
          replicas: 1
          # false as running with standalone WildFly
          bootableJar: false
    EOF

    Creating of the application is with

    oc process -n $(oc project -q) -f /tmp/operator-template-cr.yaml \
          -p APPLICATION_IMAGE=image-registry.openshift-image-registry.svc:5000/$(oc project -q)/transaction-xa \
          -p APPLICATION_NAME=transaction-xa \
      | oc create -f -
  • Verification on running application

    # call the REST endpoint running on OpenShift
    curl -i -XGET $(oc get route transactions-xa-route --template='{{ .spec.host }}')/transactions-xa/tasks
  • Scale-up and scale-down the WildFlyServer object in Operator spec

    oc patch wildflyserver transactions-xa \
      -p '[{"op":"replace", "path":"/spec/replicas", "value":3}]' --type json

OpenShift WildFly bootable jar

  • Start the WildFly Operator on OpenShift (see https://github.com/wildfly/wildfly-operator/)

  • Start the PostgreSQL on OpenShift

  • * Import the bare OpenJDK 11 image to run the Java application (ie. bootable jar)

    oc import-image ubi8/openjdk-11 --from=registry.redhat.io/ubi8/openjdk-11 --confirm
  • Build bootable jar with WildFly server embedded on the local machine

    mvn clean package -Popenshift
  • Prepare bootable jar to the directory for being taken to OpenShift

    # prepare the jar artifact to an empty directory to upload it with binary build
    mkdir target/openshift
    cp target/transactions-xa-bootable.jar target/openshift
  • Create the image stream (new-build) and build it (start-build)

    oc import-image ubi8/openjdk-11 --from=registry.redhat.io/ubi8/openjdk-11 --confirm
    oc new-build --strategy source --binary --image-stream openjdk-11 --name transactions-xa
    oc start-build transactions-xa --from-dir ./target/openshift/
  • Deploy WildFly server definition to be picked by WildFly Operator

    oc create -f examples/transactions-xa/config/transactions-xa-cr.yaml