Skip to content

OpenShift Application with WildFly Operator :: Step by Step

chalda edited this page Jan 11, 2021 · 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 [WildFly Operator](../wiki/OpenShift-WildFly-Operator-::-Step-by-Step).

OpenShift WildFly S2I image deployment

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

  • [[start_the_postgresql]]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
  • 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=master -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)
  • 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