Red Hat Training

A Red Hat training course is available for Red Hat JBoss Enterprise Application Platform

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 Java EE 7 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

  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

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

    $ oc new-project eap-demo
  3. Create a service account for this deployment.

    Service accounts are API objects that exist within each OpenShift project. You can create a service account using the following command.

    $ oc create serviceaccount SERVICE_ACCOUNT_NAME

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

    $ oc create serviceaccount eap-service-account
  4. Add the view role to the service account.

    This enables the service account to view all the resources in the project namespace, which is necessary for managing the cluster. You can add the view role to a service account using the following command.

    $ oc policy add-role-to-user view system:serviceaccount:PROJECT_NAME:SERVICE_ACCOUNT_NAME

    For example, for the kitchensink quickstart, add the view role to the service account using the following command.

    $ oc policy add-role-to-user view system:serviceaccount:eap-demo:eap-service-account
  5. Create a keystore.

    JBoss EAP for OpenShift requires a keystore to be imported to properly install and configure the image on your OpenShift instance.

    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 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
  6. Create a secret from the keystore.

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

    $ oc secret new SECRET_NAME KEYSTORE_FILENAME.jks

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

    $ oc secrets new eap-app-secret keystore.jks
  7. Add the secret to the service account.

    Add the secret to your project’s service accoung using the following command.

    $ oc secrets link SERVICE_ACCOUNT_NAME SECRET_NAME

    For example, for the kitchensink quickstart, use the following command to add the previously created eap-app-secret secret to the eap-service-account service account.

    $ oc secrets link eap-service-account eap-app-secret

2.3. Deploy a JBoss EAP Source-to-Image (S2I) Application to OpenShift

  1. Create a new OpenShift application using the JBoss EAP for OpenShift image and your Java application’s source code.

    Using the following command, specify the image stream and the path to the application source code.

    $ oc new-app IMAGE_STREAM~PATH_TO_SOURCE_CODE

    For example, for the kitchensink quickstart, use the following command to use the JBoss EAP image stream with the kitchensink source code on GitHub.

    $ oc new-app jboss-eap70-openshift~https://github.com/jboss-developer/jboss-eap-quickstarts.git#7.0.0.GA --context-dir=kitchensink
  2. Retrieve the name of the build configuration.

    $ oc get bc -o name
  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

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

    $ oc logs -f buildconfig/jboss-eap-quickstarts

2.4. 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
  2. Expose the 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/jboss-eap-quickstarts --port=8080
  3. Get the URL of the route.

    $ oc get route
  4. 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.
  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

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

    $ oc scale deploymentconfig jboss-eap-quickstarts --replicas=3