This tutorial explores how you can deploy Quarkus applications in containers and, more specifically, on OpenShift Paas Cloud platforms There are different approaches to deploy Quarkus applications on OpenShift. In this tutorial we will discuss them in detail.

Start by checking out this example, which is an example of JAX-RS application which uses Hibernate ORM API to persist data on a PostgreSQL database:

If you have a look at the directory folder of the example, you will see that it contains the standard structure of all Quarkus applications, which includes a folder src/main/docker with two Dockerfile, one for building JVM based Container images and another one for building native Container images:

├── main
│   ├── docker
│   │   ├── Dockerfile.jvm
│   │   ├── Dockerfile.native
│   ├── java
│   │   └── com
│   │       └── packt
│   │           └── quarkus
│   │               └── chapter5
│   │                   ├──
│   │                   ├──
│   │                   ├──
│   │                   ├──
│   │                   ├──
│   │                   ├──
│   │                   └──
│   └── resources
│       ├──
│       ├── import.sql
│       └── META-INF

To have a quick run of this example on OpenShift, we recommend using Red Hat Code Ready Containers, which are introduced in this tutorial: Getting started with Code Ready Containers

Initial application set up

Before you start CRC, it is required to increase the amount of memory assigned to the CRC process to 16GB, otherwise you won't be able to run the S2I builder process which is quite memory and CPU intensive:

$ crc config set memory 16384

Now start CRC with:

$ crc start

When your OpenShift cluster is ready, check that you are able to connect, using the credentials which have been printed on the Console:

$ oc login -u kubeadmin -p kKdPx-pjmWe-b3kuu-jeZm3 https://api.crc.testing:6443

You should be now logged into the default project. Let's create a new project for our application:

$ oc new-project quarkus

The first application we need to add to our project, is PostgreSQL database. We will create it using the 'oc' command line as follows, which allows setting the username/password/database attributes in one line:

$ oc new-app -e POSTGRESQL_USER=quarkus -e POSTGRESQL_PASSWORD=quarkus -e POSTGRESQL_DATABASE=quarkusdb postgresql

The postgresql image will be pulled from the default repository and in a minute or so you should be able to check:

oc get pods
NAME                      READY   STATUS                      
postgresql-1-xdlwt        1/1     Running              

Ok so now we need to plug into the Quarkus application. We can do it in several ways. Let's check them in detail.

Deploying Quarkus applications from your local folder as Binary Build

When using this approach, you will build your application locally, then create a Build Configuration and upload the content from your local folder. This approach is ideal if you have an overloaded Cloud environment but you have plenty of CPU/RAM on your Host machine. On the other hand, you will need to have installed all the required stuff to build a native Quarkus application, that is:

  • GRAAL_VM 19.2.0 to build native executables with the "native-image" tool
  • Maven 3.6.3

Start by building the native image for your application:

mvn package -Pnative -Dnative-image.docker-build=true -DskipTests=true

When done, create a new Binary Build named "quarkus-hibernate"

oc new-build --binary --name=quarkus-hibernate -l app=quarkus-hibernate

Next, set the dockerfilePath attribute into the Build Configuration

oc patch bc/quarkus-hibernate -p '{"spec":{"strategy":{"dockerStrategy":{"dockerfilePath":"src/main/docker/Dockerfile.native"}}}}'

Then, start the build, uploading content from the local folder:

oc start-build quarkus-hibernate --from-dir=. --follow

As soon as the build has completed, create a new OpenShift Application, using as Input the "quarkus-hibernate" Image Stream:

oc new-app --image-stream=quarkus-hibernate:latest

Expose the Service through a Route:

oc expose svc/quarkus-hibernate

Now verify that your hibernate-demo Pod is up and running:

oc get pods
NAME                      READY   STATUS                      
hibernate-demo-1-rftfm    1/1     Running                      
postgresql-1-xdlwt        1/1     Running              

Check which is the application route:

oc get routes
NAME             HOST/PORT                                 PATH   SERVICES         PORT       TERMINATION   WILDCARD
hibernate-demo   hibernate-demo-quarkus.apps-crc.testing          hibernate-demo   8080-tcp                 None

You can reach that route from the browser and verify that the application is running:

Deploying the application as GraalVM native executable in OpenShift

Let's see now how to use the Source to Image builder images to deploy our application.

Deploying native Quarkus applications on OpenShift using S2I

S2I (Source to Image) builder images are used by OpenShift to build Image Streams. As far as Quarkus is concerned, two S2I are available:

  • GraalVM Native S2I - build your source code using Maven or Gradle and create a new container image from the produced native executable.
  • Binary S2I - build a new container image from a provided native executable. This executable is generally built on your machine, and uploaded.

Both approaches result in containers which are based on UBI images. We will show the first approach, that is using GraalVM Native S2I.

Using the S2I mechanism is ideal if you don't have the environment to build Quarkus applications on your Host machine, which will be enterely built by the S2I mechanism as a Container image.

Before triggering the S2I process, we will clean up our project, deleting the existing hibernate-demo application:

oc delete all -l app=hibernate-demo

In order to run the GraalVM Native S2I builder, we need to provide the name of the image and the GitHub repository which will be cloned and used to build the application:

$ oc new-app --context-dir=Chapter05/hibernate --name=hibernate-demo

This process will take a couple of minutes to complete. You can verify the build logs with:

$ oc logs -f bc/hibernate-demo

When you are done, expose the Service through a Route:

oc expose svc/quarkus-hibernate

Now your application is exposed on OpenShift with just one command!

Deploying Java Quarkus applications on OpenShift using S2I

It is worth to note that you can also expose pure Java applications on OpenShift, therefore without triggering the native compilation. To build the Java image on OpenShift just provide the name of the OpenJDK Container image and the GitHub repository:

oc new-app --context-dir=Chapter05/hibernate --name=hibernate-demo

When you are done, expose the Service through a Route:

oc expose svc/quarkus-hibernate