Chapter 2. Build and Run a Java Application on the JBoss EAP for OpenShift Image


The following workflow demonstrates using the Source-to-Image (S2I) process to build and run a Java application on the JBoss EAP for OpenShift image.

As an example, the kitchensink quickstart is used in this procedure. It demonstrates a Jakarta EE web-enabled database application using Jakarta Server Faces, Jakarta Contexts and Dependency Injection, Jakarta Enterprise Beans, Jakarta Persistence, and Jakarta Bean Validation. See the kitchensink quickstart that ships with JBoss EAP 7 for more information.

2.1. Prerequisites

You have an OpenShift instance installed and operational. For more information on installing and configuring your OpenShift instance, see OpenShift Container Platform Getting Started guide.

2.2. Prepare OpenShift for Application Deployment

  1. Log in to your OpenShift instance using the oc login command.
  2. Create a new project in OpenShift.

    A project allows a group of users to organize and manage content separately from other groups. You can create a project in OpenShift using the following command.

    $ oc new-project <project_name>
    Copy to Clipboard Toggle word wrap

    For example, for the kitchensink quickstart, create a new project named eap-demo using the following command.

    $ oc new-project eap-demo
    Copy to Clipboard Toggle word wrap
  3. Optional: Create a keystore and a secret.

    Note

    Creating a keystore and a secret is required if you are using any HTTPS-enabled features in your OpenShift project. For example, if you are using the eap74-https-s2i template, you must create a keystore and secret.

    This workflow demonstration for the kitchensink quickstart does not use an HTTPS template, so a keystore and secret are not required.

    1. Create a keystore.

      Warning

      The following commands generate a self-signed certificate, but for production environments Red Hat recommends that you use your own SSL certificate purchased from a verified Certificate Authority (CA) for SSL-encrypted connections (HTTPS).

      You can use the Java keytool command to generate a keystore:

      $ keytool -genkey -keyalg RSA -alias <alias_name> -keystore <keystore_filename.jks> -validity 360 -keysize 2048
      Copy to Clipboard Toggle word wrap

      For example, for the kitchensink quickstart, use the following command to generate a keystore:

      $ keytool -genkey -keyalg RSA -alias eapdemo-selfsigned -keystore keystore.jks -validity 360 -keysize 2048
      Copy to Clipboard Toggle word wrap
    2. Create a secret from the keystore.

      Create a secret from the previously created keystore using the following command.

      $ oc create secret generic <secret_name> --from-file=<keystore_filename.jks>
      Copy to Clipboard Toggle word wrap

      For example, for the kitchensink quickstart, use the following command to create a secret.

      $ oc create secret generic eap7-app-secret --from-file=keystore.jks
      Copy to Clipboard Toggle word wrap

Before you can import and use the JBoss EAP for OpenShift image, you must first configure authentication to the Red Hat Container Registry.

Red Hat recommends that you create an authentication token using a registry service account to configure access to the Red Hat Container Registry. This means that you don’t have to use or store your Red Hat account’s username and password in your OpenShift configuration.

  1. Follow the instructions on Red Hat Customer Portal to create an authentication token using a registry service account.
  2. Download the YAML file containing the OpenShift secret for the token. You can download the YAML file from the OpenShift Secret tab on your token’s Token Information page.
  3. Create the authentication token secret for your OpenShift project using the YAML file that you downloaded:

    oc create -f 1234567_myserviceaccount-secret.yaml
    Copy to Clipboard Toggle word wrap
  4. Configure the secret for your OpenShift project using the following commands, replacing the secret name in the example with the name of your secret created in the previous step.

    oc secrets link default 1234567-myserviceaccount-pull-secret --for=pull
    oc secrets link builder 1234567-myserviceaccount-pull-secret --for=pull
    Copy to Clipboard Toggle word wrap

See the OpenShift documentation for more information on other methods for configuring access to secured registries.

See the Red Hat Customer Portal for more information on configuring authentication to the Red Hat Container Registry.

You must import the latest JBoss EAP for OpenShift imagestreams and templates for your JDK into the namespace of your OpenShift project.

Note

Log in to the Red Hat Container Registry using your Customer Portal credentials to import the JBoss EAP imagestreams and templates. For more information, see Red Hat Container Registry Authentication.

Import command for JDK 8

oc replace -f \
https://raw.githubusercontent.com/jboss-container-images/jboss-eap-openshift-templates/eap74/eap74-openjdk8-image-stream.json
Copy to Clipboard Toggle word wrap

This command imports the following imagestreams and templates.

  • The JDK 8 builder imagestream: jboss-eap74-openjdk8-openshift
  • The JDK 8 runtime imagestream: jboss-eap74-openjdk8-runtime-openshift
Note

If you use OpenShift 3 and create an EAP 7.4 ImageStream for the first time, run the following command instead of oc replace:

oc create -f https://raw.githubusercontent.com/jboss-container-images/jboss-eap-openshift-templates/eap74/eap74-openjdk8-image-stream.json
Copy to Clipboard Toggle word wrap

Import command for JDK 11

oc replace -f \
https://raw.githubusercontent.com/jboss-container-images/jboss-eap-openshift-templates/eap74/eap74-openjdk11-image-stream.json
Copy to Clipboard Toggle word wrap

This command imports the following imagestreams and templates.

  • The JDK 11 builder imagestream: jboss-eap74-openjdk11-openshift
  • The JDK 11 runtime imagestream: jboss-eap74-openjdk11-runtime-openshift

Import command for templates

for resource in \
  eap74-amq-persistent-s2i.json \
  eap74-amq-s2i.json \
  eap74-basic-s2i.json \
  eap74-https-s2i.json \
  eap74-sso-s2i.json

do
  oc replace -f \
https://raw.githubusercontent.com/jboss-container-images/jboss-eap-openshift-templates/eap74/templates/${resource}
done
Copy to Clipboard Toggle word wrap

This command imports all templates specified in the command.

Note

The JBoss EAP imagestreams and templates imported using these commands are only available within that OpenShift project.

If you have administrative access to the general openshift namespace and want the imagestreams and templates to be accessible by all projects, add -n openshift to the oc replace line of the command. For example:

...
oc replace -n openshift -f \
...
Copy to Clipboard Toggle word wrap

If you use the cluster-samples-operator, refer to the OpenShift documentation on configuring the cluster samples operator. See Configuring the Samples Operator for details about configuring the cluster samples operator.

After you import the images and templates, you can deploy applications to OpenShift.

Prerequisites

Optional: A template can specify default values for many template parameters, and you might have to override some, or all, of the defaults. To see template information, including a list of parameters and any default values, use the command oc describe template TEMPLATE_NAME.

Procedure

  1. Create a new OpenShift application that uses the JBoss EAP for OpenShift image and the source code of your Java application. You can use one of the provided JBoss EAP for OpenShift templates for S2I builds. You can also choose to provision a trimmed server.

    For example, to deploy the kitchensink quickstart using the JDK 8 builder image, enter the following command to use the eap74-basic-s2i template in the eap-demo project, created in Prepare OpenShift for Application Deployment, with the kitchensink source code on GitHub. This quickstart does not support the trimming capability.

    oc new-app --template=eap74-basic-s2i \ 
    1
    
     -p IMAGE_STREAM_NAMESPACE=eap-demo \ 
    2
    
     -p EAP_IMAGE_NAME=jboss-eap74-openjdk8-openshift:7.4.0 \ 
    3
    
     -p EAP_RUNTIME_IMAGE_NAME=jboss-eap74-openjdk8-runtime-openshift:7.4.0 \ 
    4
    
     -p SOURCE_REPOSITORY_URL=https://github.com/jboss-developer/jboss-eap-quickstarts \ 
    5
    
     -p SOURCE_REPOSITORY_REF=7.4.x \ 
    6
    
     -p CONTEXT_DIR=kitchensink 
    7
    Copy to Clipboard Toggle word wrap
    1
    The template to use.
    2
    The latest imagestreams and templates were imported into the project’s namespace, so you must specify the namespace where to find the imagestream. This is usually the project’s name.
    3
    The name of the EAP builder image stream for JDK8.
    4
    The name of the EAP runtime image stream for JDK8.
    5
    URL to the repository containing the application source code.
    6
    The Git repository reference to use for the source code. This can be a Git branch or tag reference.
    7
    The directory within the source repository to build.

    As another example, to deploy the helloworld-html5 quickstart using the JDK 11 runtime image and trimming JBoss EAP to include only the jaxrs-server layer, enter the following command. The command uses the eap74-basic-s2i template in the eap-demo project, created in Prepare OpenShift for Application Deployment, with the helloworld-html5 source code on GitHub.

    oc new-app --template=eap74-basic-s2i \ 
    1
    
     -p IMAGE_STREAM_NAMESPACE=eap-demo \ 
    2
    
     -p EAP_IMAGE_NAME=jboss-eap74-openjdk11-openshift:7.4.0 \ 
    3
    
     -p EAP_RUNTIME_IMAGE_NAME=jboss-eap74-openjdk11-runtime-openshift:7.4.0 \ 
    4
    
     -p SOURCE_REPOSITORY_URL=https://github.com/jboss-developer/jboss-eap-quickstarts \ 
    5
    
     -p SOURCE_REPOSITORY_REF=7.4.x \ 
    6
    
     -p GALLEON_PROVISION_LAYERS=jaxrs-server \ 
    7
    
     -p CONTEXT_DIR=helloworld-html5 
    8
    Copy to Clipboard Toggle word wrap
    1
    The template to use.
    2
    The latest imagestreams and templates were imported into the project’s namespace, so you must specify the namespace where to find the imagestream. This is usually the project’s name.
    3
    The name of the EAP builder image stream for JDK11.
    4
    The name of the EAP runtime image stream for JDK11.
    5
    URL to the repository containing the application source code.
    6
    The Git repository reference to use for the source code. This can be a Git branch or tag reference.
    7
    Provision a trimmed server with only the jaxrs-server layer.
    8
    The directory within the source repository to build.
    Note

    You might also want to configure environment variables when creating your new OpenShift application.

    For example, if you are using an HTTPS template such as eap74-https-s2i, you must specify the required HTTPS environment variables HTTPS_NAME, HTTPS_PASSWORD, and HTTPS_KEYSTORE to match your keystore details.

    Note

    If the template uses AMQ, you must include the AMQ_IMAGE_NAME parameter with the appropriate value.

    If the template uses SSO, you must include the SSO_IMAGE_NAME parameter with the appropriate value.

  2. Retrieve the name of the build configuration.

    $ oc get bc -o name
    Copy to Clipboard Toggle word wrap
  3. Use the name of the build configuration from the previous step to view the Maven progress of the build.

    $ oc logs -f buildconfig/BUILD_CONFIG_NAME
    Copy to Clipboard Toggle word wrap

    For example, for the kitchensink quickstart, the following command shows the progress of the Maven build.

    $ oc logs -f buildconfig/eap-app
    Copy to Clipboard Toggle word wrap

2.6. Post deployment tasks

Depending on your application, some tasks might need to be performed after your OpenShift application has been built and deployed. This might include exposing a service so that the application is viewable from outside of OpenShift, or scaling your application to a specific number of replicas.

  1. Get the service name of your application using the following command.

    $ oc get service
    Copy to Clipboard Toggle word wrap
  2. Expose the main service as a route so you can access your application from outside of OpenShift. For example, for the kitchensink quickstart, use the following command to expose the required service and port.

    $ oc expose service/eap-app --port=8080
    Copy to Clipboard Toggle word wrap
    Note

    If you used a template to create the application, the route might already exist. If it does, continue on to the next step.

  3. Get the URL of the route.

    $ oc get route
    Copy to Clipboard Toggle word wrap
  4. Access the application in your web browser using the URL. The URL is the value of the HOST/PORT field from the previous command’s output.

    If your application does not use the JBoss EAP root context, append the context of the application to the URL. For example, for the kitchensink quickstart, the URL might be http://HOST_PORT_VALUE/kitchensink/.

  5. Optionally, you can also scale up the application instance by running the following command. This increases the number of replicas to 3.

    $ oc scale deploymentconfig DEPLOYMENTCONFIG_NAME --replicas=3
    Copy to Clipboard Toggle word wrap

    For example, for the kitchensink quickstart, use the following command to scale up the application.

    $ oc scale deploymentconfig eap-app --replicas=3
    Copy to Clipboard Toggle word wrap

JBoss EAP for OpenShift supports chained builds in OpenShift.

JBoss EAP for OpenShift templates employ chained builds. When you use these templates, two builds result:

  • An intermediate image named [application name]-build-artifacts
  • The final image, [application name]

For details about chained builds, see the OpenShift documentation.

Back to top
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2025 Red Hat