Search

Chapter 3. Using source-to-image for OpenShift

download PDF

You can use the source-to-image (S2I) for OpenShift image to run your custom Java applications on OpenShift.

3.1. Building and deploying Java applications with source-to-image for OpenShift

To build and deploy a Java application from source on OpenShift by using the source-to-image (S2I) for OpenShift image, use the OpenShift S2I process.

Procedure

  1. Log in to the OpenShift instance by running the following command and by providing your credentials:

    $ oc login
  2. Create a new project:

    $ oc new-project <project-name>
  3. Create a new application using the S2I for OpenShift image:

    The <source-location> is the URL of GitHub repository or path to a local folder.

    $ oc new-app <source-location>

    For example:

    $ oc new-app --context-dir=getting-started --name=quarkus-quickstart \
     'registry.access.redhat.com/ubi8/openjdk-11~https://github.com/quarkusio/quarkus-quickstarts.git#2.12.1.Final'
  4. Get the service name:

    $ oc get svc
  5. Expose the service as a route, so that you can use the server from your browser:

    $ oc expose svc/ --port=8080
  6. Get the route:

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

Additional resources

3.2. Building and deploying Java applications from binary artifacts

You can deploy your existing Java applications on OpenShift by using the binary source capability.

The procedure uses undertow-servlet quickstart to build a Java application on your local machine. The quickstart copies the resulting binary Artifacts into OpenShift by using the S2I binary source capability.

Prerequisites

  • Enable Red Hat JBoss Enterprise Maven Repository on your local machine.
  • Get the JAR application archive and build the application locally.

    • Clone the undertow-servlet source code:

      $ git clone https://github.com/jboss-openshift/openshift-quickstarts.git
    • 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] ------------------------------------------------------------------------
  • Prepare the directory structure on the local file system.

    Copy the application archives in the deployments/ sub-directory (where the main binary build directory) to the standard deployments folder (where the image is build on OpenShift). Structure the directory hierarchy containing the web application data for the application to deploy.

    Create a main directory for the binary build on the local file system and deployments/ subdirectory within it. Copy the 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/

Procedure

  1. Log in to the OpenShift instance by running the following command and by providing your credentials:

    $ oc login
  2. Create a new project:

    $ oc new-project jdk-bin-demo
  3. Create a new binary build, and specify the image stream and the application’s name:

    $ oc new-build --binary=true \
    --name=jdk-us-app \
    --image-stream=java:11
    --> Found image c1f5b31 (2 months old) in image stream "openshift/java:11" under tag "latest" for "java:11"
    
        Java Applications
        -----------------
        Platform for building and running plain Java applications (fat-jar and flat classpath)
    
    --> Creating resources with label build=jdk-us-app ...
        imagestream "jdk-us-app" created
        buildconfig "jdk-us-app" created
    --> Success
    Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
     'oc expose svc/jdk-us-app'
  4. Start the binary build.

    Instruct the oc executable to use main directory of the binary build you have 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
  5. 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.
  6. Expose the service as route.

    $ oc expose svc/jdk-us-app
    route "jdk-us-app" exposed
  7. Get the route:

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

Additional resources

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.

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.

© 2024 Red Hat, Inc.