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 JSF, CDI, EJB, JPA, and Bean Validation. See the kitchensink
quickstart that ships with JBoss EAP 7 for more information.
2.1. Prerequisites
This workflow assumes that you already have an active OpenShift Online subscription and that you have installed the OpenShift CLI.
2.2. Prepare OpenShift for Application Deployment
-
Log in to your OpenShift instance using the
oc login
command. 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
For example, for the
kitchensink
quickstart, create a new project namedeap-demo
using the following command.$ oc new-project eap-demo
Optional: Create a keystore and a secret.
NoteCreating 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
eap73-https-s2i
template (for JDK 8) or theeap73-openjdk11-https-s2i
template (for JDK 11), 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.Create a keystore.
WarningThe 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 using the following command.$ keytool -genkey -keyalg RSA -alias ALIAS_NAME -keystore KEYSTORE_FILENAME.jks -validity 360 -keysize 2048
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
Create a secret from the keystore.
Create a secret from the previously created keystore using the following command.
$ oc create secret SECRET_NAME KEYSTORE_FILENAME.jks
For example, for the
kitchensink
quickstart, use the following command to create a secret.$ oc create secret eap7-app-secret keystore.jks
2.3. Import the Latest JBoss EAP for OpenShift Imagestreams and Templates
You must import the latest JBoss EAP for OpenShift imagestreams and templates for your JDK into the namespace of your OpenShift project.
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
for resource in \ eap73-amq-persistent-s2i.json \ eap73-amq-s2i.json \ eap73-basic-s2i.json \ eap73-https-s2i.json \ eap73-image-stream.json \ eap73-sso-s2i.json \ eap73-starter-s2i.json \ do oc replace --force -f \ https://raw.githubusercontent.com/jboss-container-images/jboss-eap-7-openshift-image/eap73/templates/${resource} done
This command imports the following imagestreams and templates.
-
The JDK 8 builder imagestream:
jboss-eap73-openshift
-
The JDK 8 runtime imagestream:
jboss-eap73-runtime-openshift
- All templates specified in the command.
Import command for JDK 11
for resource in \ eap73-openjdk11-amq-persistent-s2i.json \ eap73-openjdk11-amq-s2i.json \ eap73-openjdk11-basic-s2i.json \ eap73-openjdk11-https-s2i.json \ eap73-openjdk11-image-stream.json \ eap73-openjdk11-sso-s2i.json \ eap73-openjdk11-starter-s2i.json \ do oc replace --force -f \ https://raw.githubusercontent.com/jboss-container-images/jboss-eap-7-openshift-image/eap73/templates/${resource} done
This command imports the following imagestreams and templates.
-
The JDK 11 builder imagestream:
jboss-eap73-openjdk11-openshift
-
The JDK 11 runtime imagestream:
jboss-eap73-openjdk11-runtime-openshift
- All templates specified in the command.
Import command for Eclipse OpenJ9 on IBM Z and IBM Power Systems
oc replace --force -f \ https://raw.githubusercontent.com/jboss-container-images/jboss-eap-openshift-templates/eap73/eap73-openj9-image-stream.json for resource in \ eap73-amq-persistent-s2i.json \ eap73-amq-s2i.json \ eap73-basic-s2i.json \ eap73-https-s2i.json \ eap73-sso-s2i.json \ do oc replace --force -f \ https://raw.githubusercontent.com/jboss-container-images/jboss-eap-openshift-templates/eap73/templates/${resource} done
This command imports the following imagestreams and templates.
-
The Eclipse OpenJ9 builder imagestream:
jboss-eap-7-openj9-11-openshift
-
The Eclipse OpenJ9 runtime imagestream:
jboss-eap-7-openj9-11-runtime-openshift
- All templates specified in the command.
The JBoss EAP imagestreams and templates imported using these commands are only available within that OpenShift project.
If you want to import the image streams and templates into a different project, add the -n PROJECT_NAME
to the oc replace
line of the command. For example:
...
oc replace -n PROJECT_NAME --force -f
...
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.
2.4. Deploy a JBoss EAP Source-to-Image (S2I) Application to OpenShift
After you import the images and templates, you can deploy applications to OpenShift.
OpenJDK 8 and Eclipse OpenJ9 template names use the prefix eap73-*
; for example, eap73-https-s2i
. OpenJDK 11 template names use the prefix eap73-openjdk11-*
; for example, eap73-openjdk11-https-s2i
.
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
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 theeap73-basic-s2i
template in theeap-demo
project, created in Prepare OpenShift for Application Deployment, with thekitchensink
source code on GitHub. This quickstart does not support the trimming capability.oc new-app --template=eap73-basic-s2i \1 -p IMAGE_STREAM_NAMESPACE=eap-demo \2 -p SOURCE_REPOSITORY_URL=https://github.com/jboss-developer/jboss-eap-quickstarts \3 -p SOURCE_REPOSITORY_REF=7.3.x-openshift \4 -p CONTEXT_DIR=kitchensink5
- 1
- The template to use. The
eap73
prefix specifies the JDK 8 template. - 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
- URL to the repository containing the application source code.
- 4
- The Git repository reference to use for the source code. This can be a Git branch or tag reference.
- 5
- The directory within the source repository to build.
NoteUse a modified version of this command for the Eclipse OpenJ9 builder image on IBM Z and IBM Power Systems. Include the following image name parameters in the command. The JDK environment uses default values for these parameters.
- EAP_IMAGE_NAME=jboss-eap-7-openj9-11-openshift \
- EAP_RUNTIME_IMAGE_NAME=jboss-eap-7-openj9-11-runtime-openshift \
As another example, to deploy the
helloworld-html5
quickstart using the JDK 11 runtime image and trimming JBoss EAP to include only thejaxrs-server
layer, enter the following command. The command uses theeap73-openjdk11-basic-s2i
template in theeap-demo
project, created in Prepare OpenShift for Application Deployment, with thehelloworld-html5
source code on GitHub.oc new-app --template=eap73-openjdk11-basic-s2i \1 -p IMAGE_STREAM_NAMESPACE=eap-demo \2 -p SOURCE_REPOSITORY_URL=https://github.com/jboss-developer/jboss-eap-quickstarts \3 -p SOURCE_REPOSITORY_REF=7.3.x-openshift \4 -p GALLEON_PROVISION_LAYERS=jaxrs-server \5 -p CONTEXT_DIR=helloworld-html56
- 1
- The template to use. The
eap73-openjdk11
prefix specifies the JDK 11 template. - 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
- URL to the repository containing the application source code.
- 4
- The Git repository reference to use for the source code. This can be a Git branch or tag reference.
- 5
- Provision a trimmed server with only the
jaxrs-server
layer. - 6
- The directory within the source repository to build.
NoteYou might also want to configure environment variables when creating your new OpenShift application.
For example, if you are using an HTTPS template such as
eap73-https-s2i
, you must specify the required HTTPS environment variablesHTTPS_NAME
,HTTPS_PASSWORD
, andHTTPS_KEYSTORE
to match your keystore details.NoteIf 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.Retrieve the name of the build configuration.
$ oc get bc -o name
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
For example, for the
kitchensink
quickstart, the following command shows the progress of the Maven build.$ oc logs -f buildconfig/eap-app
Additional Resources
2.5. 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.
Get the service name of your application using the following command.
$ oc get service
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
NoteIf you used a template to create the application, the route might already exist. If it does, continue on to the next step.
Get the URL of the route.
$ oc get route
Access the application in your web browser using the URL. The URL is the value of the
HOST/PORT
field from 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 behttp://HOST_PORT_VALUE/kitchensink/
.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
For example, for the
kitchensink
quickstart, use the following command to scale up the application.$ oc scale deploymentconfig eap-app --replicas=3
2.6. Chained Build Support in JBoss EAP for OpenShift
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.
Additional Resources