Chapter 3. Using source-to-image for OpenShift
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
Log in to the OpenShift instance by running the following command and by providing your credentials:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc login
$ oc login
Create a new project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc new-project <project-name>
$ oc new-project <project-name>
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.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc new-app <source-location>
$ oc new-app <source-location>
For example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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'
$ 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'
Get the service name:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc get svc
$ oc get svc
Expose the service as a route, so that you can use the server from your browser:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc expose svc/ --port=8080
$ oc expose svc/ --port=8080
Get the route:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc get route
$ oc get route
-
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
- For more detailed example, see the Running flat classpath JAR on source-to-image for OpenShift.
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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow git clone https://github.com/jboss-openshift/openshift-quickstarts.git
$ git clone https://github.com/jboss-openshift/openshift-quickstarts.git
Build the application:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow cd openshift-quickstarts/undertow-servlet/
$ cd openshift-quickstarts/undertow-servlet/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow mvn clean package
$ 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow undertow-servlet]$ ls dependency-reduced-pom.xml pom.xml README src target
undertow-servlet]$ ls dependency-reduced-pom.xml pom.xml README src target
Copy to Clipboard Copied! Toggle word wrap Toggle overflow mkdir -p ocp/deployments
$ mkdir -p ocp/deployments
Copy to Clipboard Copied! Toggle word wrap Toggle overflow cp target/undertow-servlet.jar ocp/deployments/
$ cp target/undertow-servlet.jar ocp/deployments/
Procedure
Log in to the OpenShift instance by running the following command and by providing your credentials:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc login
$ oc login
Create a new project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc new-project jdk-bin-demo
$ oc new-project jdk-bin-demo
Create a new binary build, and specify the image stream and the application’s name:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc new-build --binary=true \ --name=jdk-us-app \ --image-stream=java:11
$ 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'
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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc start-build jdk-us-app --from-dir=./ocp --follow
$ 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
Create a new OpenShift application based on the build:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc new-app jdk-us-app
$ 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.
Expose the service as route.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc expose svc/jdk-us-app
$ oc expose svc/jdk-us-app route "jdk-us-app" exposed
Get the route:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc get route
$ oc get route
-
Access the application in your browser by using the URL (value of
HOST/PORT
field from the previous command output).
Additional resources
- Use the binary source capability to deploy existing Java applications on OpenShift.
- For more information on how to configure maven repository, see Use the Maven Repository.