OpenShift: Bringing to life a SpringBoot binary

Aymen ABDELWAHED
Feb 28 · 4 min read

This article promises on bringing a SpringBoot binary file to life and make from it a fully running application on OpenShift.

Our today’s Use-Case: Deploy to Openshift a resultant artifact of an already built packaged springboot application. This is useful for software built on a platform different from the one where the application is going to run.

Prerequisites for our Experiments

First, we need a running OpenShift cluster on which we can do our experiments. I invite you then to run the next steps using either a free account on OpenShift.io or download & run an OpenShift cluster locally using minishift.

Do not forget to download/install the OpenShift-Client tool.

Our Project

OpenShift provides natively with different options for building and deploying containers, which includes building/deploying applications either from source code or from binaries.

Platforms like Java allows developers to package all the dependencies, resources and configuration files in a single archive file, or which we call JAR binary files.

Our project is based on an OpenSource SpringBoot project.

The JAR file can be downloaded from hereHelloWorld-2.0.jar”, renamed as “springboot.jar” and placed in a deployments folder.

Build Process

For our use-case, we need a builder image that contains tools like Maven to build our artifacts provided as a jar file.

In OpenShift, an OpenJDK Image stream is already provided in the “openshift” namespace.

Let’s check what images are available for us:

oc get is -n openshift | grep -i openjdk

Create a new Project

Let’s start by creating an OpenShift project.

oc new-project springproject --display-name "Spring Project"

Create a new Build

We’re going to use a builder image, so we’ll use the “redhat-openjdk18-openshift”. The following creates a build, and accept a binary content as a source.

Here we are naming the application as myspringboot.

oc new-build --name=myspringboot --strategy=source --binary \
--image-stream=openshift/redhat-openjdk18-openshift:1.8

Executing the Build

Once the BuildConfig is created in OpenShift, we can then take care of the creation of a final docker container image. This image is based on OpenJDK as a Base-Image and our artifact file, which is mainly a binary JDK file.

oc start-build myspringboot --from-file=myspringboot.jar --follow

The command starts by uploading/copying the binary file into the appropriate location within the destination container.

The “ — follow” flag is helpful as it provides you with the ability to watch the build logs.

You can check the builds history by launching this command:

oc get builds

Create the Application

Now we already have the image built, including the application artifcats. One easy step is remaining to make the application deployed and up & running.

Let’s launch the creation of our spring boot application:

oc new-app myspringboot

As a result, this command dynamically generates a DeploymentConfig, make the application run as pods and expose it using a Service. However, there is no route for this application yet. So you cannot access this application from outside.

Expose your application

Let’s create an OpenShift route to make the “myspringboot” service exposed to the internet.

oc expose svc/myspringboot

We can check the exposed routes:

oc get route myspringboot

Let’s curl our application:

curl $(oc get route myspringboot \
-o jsonpath=’{.status.ingress[0].host}’)

What’s next?

We’ve succeeded in building, and deploying a basic JAR file. Now there’s an opportunity to improve these experiments and I have some suggestions for you:

This how you can export / create a template file from an existing OpenShift resources:

oc get bc myspringboot -o yaml > myspringboot-bc.yaml

Please do not hesitate to tweet me with your valuables comments/questions.

Aymen ABDELWAHED

Written by

Is a DevOs & Cloud Consultant / Architect with 10 plus years of experience. He likes to immerse himself in the latest technologies, learn & getting certified

More From Medium

Top on Medium

Ed Yong
Mar 25 · 22 min read

26K

Top on Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade