Getting Started With OpenShift Java S2I
A very interesting update to the OpenShift PaaS allows you to quickly and easily handle your JAR deployment to OpenShift with great simplicity.
Join the DZone community and get the full member experience.Join For Free
The OpenShift Java S2I image, which allows you to automatically build and deploy your Java microservices, has just been released and is now publicly available. This article describes how to get started with the Java S2I container image, but first, let’s discuss why having a Java S2I image is so important.
Why Java S2I?
The Java S2I image enables developers to automatically build, deploy, and run Java applications on demand, in the OpenShift Container Platform, by simply specifying the location of their application source code or compiled Java binaries. In many cases, these Java applications are bootable “fat JARs” that include an embedded version of an application server and other frameworks. Examples of such open source projects are Spring Boot, Eclipse Vert.x, and WildFly Swarm. Having a common Java S2I image that is designed specifically for OpenShift makes the development of bootable fat JARs easier by including a number of useful features such as:
- Simple yet flexible: The Java S2I image can handle complex build structures, but by default, it will assume that the JAR to run will be available in the /target directory after a successful build. If that is not the case, you can tweak it using the environment variable ARTIFACT_DIR. Also, if a build produces multiple JAR files (which is the case in the Eclipse Vert.x example used later in this article), then it’s possible to specify which JAR file to run using the environment variable JAVA_APP_JAR. However, in most cases, all you have to do is to point directly at your source repository and the Java S2I image will work.
- Automatic JVM memory configuration: In OpenShift, resources may be limited by quotas, etc. If such a limitation is in place, the Java S2I image will automatically adopt the JVM memory settings so that the JVM doesn’t use more memory than it’s allowed to use, which, in turn, helps to avoid OutOfMemory exceptions. This feature is enabled by default but can be disabled by setting a fixed value for the heap using -Xmx in the environment variable JAVA_OPTIONS.
- Expose JMX statistics via Jolokia: By default, the Java S2I image will enable Jolokia, which exposes JMX statistics and operations via HTTP. This enables more detailed monitoring capabilities of applications. This feature is enabled by default but can be disabled by setting the environment variable AB_JOLOKIA_OFF to true.
- Reduce the container image size: To keep the size of the container image to a minimum, it’s possible to have the Java S2I image delete any maven repo data before building the final image. This feature is disabled by default to support incremental builds. To enable the removal of maven artifacts from the final container image, set the environment variable MAVEN_CLEAR_REPO to true.
Getting Started With Java S2I
Below are step-by-step instructions that show one way to use the Java S2I image.
Create a new project or use an existing one.
oc new-project myproject
Before we start using the Java S2I image, we need to tell OpenShift how to find it. This is done by creating an image stream. I’ve created an image stream definition here that you can download and use. To add the image stream to your project, run the following command:
oc create -f openjdk-s2i-imagestream.json
Open the OpenShift console, navigate to the project you created, and click on Add to Project.
In the Browse Catalog window, type openjdk in the search field.
Click select and give the application a name and point to a git repo containing the application source code.
For a WildFly Swarm example, you can use: https://github.com/redhat-helloworld-msa/hola.git.
Please note that because of a known issue with WildFly Swarm and Jolokia, you should also click on Show advanced … and add an environment variable to the Deployment Configuration with the name AB_JOLOKIA_OFF and setting the value to true.
For an Eclipse Vert.x example, you can use: https://github.com/redhat-helloworld-msa/aloha.git.
The example project for Eclipse Vert.x produces two JAR files, so we need to tell the Java S2I image which one to use by clicking on Show advanced … and adding an environment variable to the Deployment Configuration with the name JAVA_APP_JAR and setting the value to aloha-fat.jar.
For a Spring Boot example, you can use: https://github.com/redhat-helloworld-msa/ola.git.
Wait for the build to finish and click on the route (the URL in the upper right corner of the dashboard).
For the WildFly example application, the Swagger console should now be displayed.
Try adding /api/hola to the address bar (or /api/ola for the Spring Boot example) and the outcome should be similar to the picture below.
DONE! I hope you have enjoyed this getting started with Java S2I guide.
Published at DZone with permission of Thomas Qvarnström, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.