Chapter 2. Build and Run a Java Application on the JBoss EAP CD 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 CD for OpenShift image.
As an example, the kitchensink quickstart is used in this procedure. It demonstrates a Java 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 OpenShift instance installed and operational, similar to that created in the OpenShift Primer.
2.2. Prepare OpenShift for Application Deployment
-
Log in to your OpenShift instance using the
oc logincommand. 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_NAMEFor example, for the
kitchensinkquickstart, create a new project namedeap-demousing 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
eap-cd-https-s2itemplate, you must create a keystore and secret.This workflow demonstration for the
kitchensinkquickstart 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
keytoolcommand 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
kitchensinkquickstart, 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 secrets new SECRET_NAME KEYSTORE_FILENAME.jks
For example, for the
kitchensinkquickstart, use the following command to create a secret.$ oc secrets new eap7-app-secret keystore.jks
2.3. Configure Authentication to the Red Hat Container Registry
Before you can import and use the JBoss EAP CD 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.
- Follow the instructions on Red Hat Customer Portal to create an authentication token using a registry service account.
- 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.
Create the authentication token secret for your OpenShift project using the YAML file that you downloaded:
oc create -f 1234567_myserviceaccount-secret.yamlConfigure the secret for your OpenShift project using the following commands, replacing the secret name below 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
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.
2.4. Import the Latest JBoss EAP CD for OpenShift Image Streams and Templates
Use the following command to import the latest JBoss EAP CD for OpenShift image streams and templates into your OpenShift project’s namespace.
for resource in \
eap-cd-image-stream.json \
eap-cd-amq-persistent-s2i.json \
eap-cd-amq-s2i.json \
eap-cd-basic-s2i.json \
eap-cd-https-s2i.json \
eap-cd-mongodb-persistent-s2i.json \
eap-cd-mongodb-s2i.json \
eap-cd-mysql-persistent-s2i.json \
eap-cd-mysql-s2i.json \
eap-cd-postgresql-persistent-s2i.json \
eap-cd-postgresql-s2i.json \
eap-cd-third-party-db-s2i.json \
eap-cd-tx-recovery-s2i.json \
eap-cd-sso-s2i.json
do
oc replace --force -f \
https://raw.githubusercontent.com/jboss-container-images/jboss-eap-7-openshift-image/CD14/templates/${resource}
doneThe JBoss EAP image streams and templates imported using the above command are only available within that OpenShift project.
If you have administrative access to the general openshift namespace and want the image streams 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 --force -f \ ...
2.5. Deploy a JBoss EAP Source-to-Image (S2I) Application to OpenShift
Create a new OpenShift application using the JBoss EAP CD for OpenShift image and your Java application’s source code. Red Hat recommends using one of the provided JBoss EAP CD for OpenShift templates for S2I builds.
For example, for the
kitchensinkquickstart, use the following command to use theeap-cd-basic-s2itemplate in theeap-demoproject, created in Prepare OpenShift for Application Deployment, with thekitchensinksource code on GitHub.oc new-app --template=eap-cd-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=openshift \4 -p CONTEXT_DIR=kitchensink5
- 1
- The template to use.
- 2
- The latest images streams and templates were imported into the project’s namespace, so you must specify the namespace of where to find the image stream. 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.
NoteA 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.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
eap-cd-https-s2i, you must specify the required HTTPS environment variablesHTTPS_NAME,HTTPS_PASSWORD, andHTTPS_KEYSTOREto match your keystore details.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_NAMEFor example, for the
kitchensinkquickstart, the following command shows the progress of the Maven build.$ oc logs -f buildconfig/eap-app
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.
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
kitchensinkquickstart, 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/PORTfield 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
kitchensinkquickstart, 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=3For example, for the
kitchensinkquickstart, use the following command to scale up the application.$ oc scale deploymentconfig eap-app --replicas=3

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.