Chapter 3. Get Started

This section describes some of the ways you can use the Java S2I for OpenShift image to run your custom java applications on OpenShift.

3.1. Source to Image (S2I) Build

To run and configure the Java S2I for OpenShift image, use the OpenShift S2I process.

The S2I process for the Java S2I for OpenShift image works as follows:

  1. Log into the OpenShift instance by running the following command and providing credentials.

    $ oc login
  2. Create a new project.

    $ oc new-project <project-name>
  3. Create a new application using the Java S2I for OpenShift image. <source-location> can be the URL of a git repository or a path to a local folder.

    $ oc new-app redhat-openjdk18-openshift~<source-location>
  4. Get the service name.

    $ oc get service
  5. Expose the service as a route to be able to use it from the browser. <service-name> is the value of NAME field from previous command output.

    $ oc expose svc/<service-name> --port=8080
  6. Get the route.

    $ oc get route
  7. Access the application in your browser using the URL (value of HOST/PORT field from previous command output).

3.2. Binary Builds

To deploy existing applications on OpenShift, you can use the binary source capability.

Prerequisite:

  1. Get the JAR application archive or build the application locally.

    The example below uses the undertow-servlet quickstart.

    • Clone the source code.

      $ git clone https://github.com/jboss-openshift/openshift-quickstarts.git
    • Configure the Red Hat JBoss Middleware Maven repository.
    • Build the application.

      $ cd openshift-quickstarts/undertow-servlet/
      $ mvn clean package
      [INFO] Scanning for projects...
      ...
      [INFO]
      [INFO] ------------------------------------------------------------------------
      [INFO] Building Undertow Servlet Example 1.0.0.Final
      [INFO] ------------------------------------------------------------------------
      ...
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD SUCCESS
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time: 1.986 s
      [INFO] Finished at: 2017-06-27T16:43:07+02:00
      [INFO] Final Memory: 19M/281M
      [INFO] ------------------------------------------------------------------------
  1. Prepare the directory structure on the local file system.

    Application archives in the deployments/ subdirectory of the main binary build directory are copied directly to the standard deployments folder of the image being built on OpenShift. For the application to deploy, the directory hierarchy containing the web application data must be correctly structured.

    Create main directory for the binary build on the local file system and deployments/ subdirectory within it. Copy the previously built JAR archive to the deployments/ subdirectory:

    undertow-servlet]$ ls
    dependency-reduced-pom.xml  pom.xml  README  src  target
    $ mkdir -p ocp/deployments
    $ cp target/undertow-servlet.jar ocp/deployments/
    Note

    Location of the standard deployments directory depends on the underlying base image, that was used to deploy the application. See the following table:

    Table 3.1. Standard Location of the Deployments Directory

    Name of the Underlying Base Image(s)Standard Location of the Deployments Directory

    EAP for OpenShift 6.4 and 7.0

    $JBOSS_HOME/standalone/deployments

    Java S2I for OpenShift

    /deployments

    JWS for OpenShift

    $JWS_HOME/webapps

Perform the following steps to run application consisting of binary input on OpenShift:

  1. Log into the OpenShift instance by running the following command and providing credentials.

    $ oc login
  2. Create a new project.

    $ oc new-project jdk-bin-demo
  3. (Optional) Identify the image stream for the particular image.

    $ oc get is -n openshift | grep ^redhat-openjdk | cut -f1 -d ' '
    redhat-openjdk18-openshift
  4. Create new binary build, specifying image stream and application name.

    $ oc new-build --binary=true \
    --name=jdk-us-app \
    --image-stream=redhat-openjdk18-openshift
    --> Found image c1f5b31 (2 months old) in image stream "openshift/redhat-openjdk18-openshift" under tag "latest" for "redhat-openjdk18-openshift"
    
        Java Applications
        -----------------
        Platform for building and running plain Java applications (fat-jar and flat classpath)
    
        Tags: builder, java
    
        * A source build using binary input will be created
          * The resulting image will be pushed to image stream "jdk-us-app:latest"
          * A binary build was created, use 'start-build --from-dir' to trigger a new build
    
    --> Creating resources with label build=jdk-us-app ...
        imagestream "jdk-us-app" created
        buildconfig "jdk-us-app" created
    --> Success
  5. Start the binary build. Instruct oc executable to use main directory of the binary build we created in previous step as the directory containing binary input for the OpenShift build.

    $ oc start-build jdk-us-app --from-dir=./ocp --follow
    Uploading directory "ocp" as binary input for the build ...
    build "jdk-us-app-1" started
    Receiving source from STDIN as archive ...
    ==================================================================
    Starting S2I Java Build .....
    S2I source build with plain binaries detected
    Copying binaries from /tmp/src/deployments to /deployments ...
    ... done
    Pushing image 172.30.197.203:5000/jdk-bin-demo/jdk-us-app:latest ...
    Pushed 0/6 layers, 2% complete
    Pushed 1/6 layers, 24% complete
    Pushed 2/6 layers, 36% complete
    Pushed 3/6 layers, 54% complete
    Pushed 4/6 layers, 71% complete
    Pushed 5/6 layers, 95% complete
    Pushed 6/6 layers, 100% complete
    Push successful
  6. Create a new OpenShift application based on the build.

    $ oc new-app jdk-us-app
    --> Found image 66f4e0b (About a minute old) in image stream "jdk-bin-demo/jdk-us-app" under tag "latest" for "jdk-us-app"
    
        jdk-bin-demo/jdk-us-app-1:c1dbfb7a
        ----------------------------------
        Platform for building and running plain Java applications (fat-jar and flat classpath)
    
        Tags: builder, java
    
        * This image will be deployed in deployment config "jdk-us-app"
        * Ports 8080/tcp, 8443/tcp, 8778/tcp will be load balanced by service "jdk-us-app"
          * Other containers can access this service through the hostname "jdk-us-app"
    
    --> Creating resources ...
        deploymentconfig "jdk-us-app" created
        service "jdk-us-app" created
    --> Success
        Run 'oc status' to view your app.
  7. Expose the service as route.

    $ oc get svc -o name
    service/jdk-us-app
    $ oc expose svc/jdk-us-app
    route "jdk-us-app" exposed
  8. Access the application.

    Access the application in your browser using the URL http://jdk-us-app-jdk-bin-demo.openshift.example.com/.

3.3. Build using the Web Console

Configure and deploy your java application using an application template from the OpenShift web console.

  1. Log in to the OpenShift web console.

    OpenShift Login

  2. Click on New Project, enter the details for Name, Display Name and Description fields, and then click Create.

    Create New Project

  3. Click on the Filter by name or description text field and type jdk to list matching templates. Click Select on the openjdk18-web-basic-s2i template.

    Select Java S2I for OpenShift image

  4. Leave the default values, scroll to the bottom of the page and click Create. Then click Continue to Overview.

    Create a new application

  5. Wait for the build to finish. Once the application pod is running, access the application in your browser by clicking on the listed link (route).

    OpenShift Login