This tutorial will kick start you in deploying Spring Boot (and generally any Java application) to OpenShift/Kubernates using OpenShift Maven plugin

The openshift-maven-plugin is your best friend to bring your Java applications on to OpenShift. It provides a tight integration into Maven and benefits from the build configuration already provided. This plugin focus on building Docker images and creating Kubernetes resource descriptors. It ships with a flexible configuration and supports multiple models for creating resources such as a Zero-Config setup which allows for a quick ramp-up with some opinionated defaults.

openshift spring boot tutorial openshift spring boot tutorial

For more advanced requirements, an XML configuration provides additional configuration options which can be added to the pom.xml. For the full power, in order to tune all facets of the creation, external resource fragments and Dockerfiles can be used.

We will now demonstrate how to deploy a Spring Boot 2 application to OpenShift just by adding the OpenShift Maven plugin in its pom.xml:

<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			   <plugin>
            <groupId>org.eclipse.jkube</groupId>
            <artifactId>openshift-maven-plugin</artifactId>
            <version>1.0.0-alpha-1</version>
            <configuration>

              <resources>
                <labels>
                  <all>
                    <testProject>spring-boot-sample</testProject>
                  </all>
                </labels>
              </resources>

              <generator>
                <includes>
                  <include>spring-boot</include>
                </includes>
                <config>
                  <spring-boot>
                    <color>always</color>
                  </spring-boot>
                </config>
              </generator>
              <enricher>
                <excludes>
                  <exclude>jkube-expose</exclude>
                </excludes>
                <config>
                  <jkube-service>
                    <type>NodePort</type>
                  </jkube-service>
                </config>
              </enricher>
            </configuration>

            <executions>
              <execution>
                <goals>
                  <goal>resource</goal>
                  <goal>build</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
		</plugins>

Here is a brief explanation to its configuration:

The labels section with resources contains labels which should be applied to objects of various kinds

The definition of generators and enrichers is used to generate specific applications for OpenShift. In our case, we will be using the spring-boot generator.

The generator adds Kubernetes liveness and readiness probes pointing to either the management or server port as read from the application.properties. If the management.port (for Spring Boot 1) or management.server.port (for Spring Boot 2) and management.ssl.key-store (for Spring Boot 1) or management.server.ssl.key-store (for Spring Boot 2) properties are set in application.properties otherwise or server.ssl.key-store property is set in application.properties then the probes are automatically set to use https.

Within its configuration, you can find the color parameter, which when set to true forces the use of color in the Spring Boot console output.

All you need to deploy the example application on OpenShift is the following goal

mvn oc:deploy

This goal is designed to run first the generation of resources (oc:build and oc:resource) and deploy them on OpenShift. Check that the output contains at the end:

[INFO] OpenShift platform detected
[INFO] oc: Using project: spring-boot-sample
[INFO] oc: Creating a Service from openshift.yml namespace spring-boot-sample name spring-boot-sample
[INFO] oc: Created Service: target/jkube/applyJson/spring-boot-sample/service-spring-boot-sample.json
[INFO] oc: Creating a DeploymentConfig from openshift.yml namespace spring-boot-sample name spring-boot-sample
[INFO] oc: Created DeploymentConfig: target/jkube/applyJson/spring-boot-sample/deploymentconfig-spring-boot-sample.json
[INFO] oc: Creating Route spring-boot-sample:spring-boot-sample host: null
[INFO] oc: HINT: Use the command `oc get pods -w` to watch your pods start up

Let's check that the application has been deployed in a Pod:

$ oc get pods
NAME                             READY   STATUS      RESTARTS   AGE
spring-boot-sample-1-88ljl       1/1     Running     0          8s
spring-boot-sample-1-deploy      0/1     Completed   0          10s
spring-boot-sample-s2i-1-build   0/1     Completed   0          81s

Perfect, now grab the route name:

$ oc get routes
NAME                 HOST/PORT                                                PATH   SERVICES             PORT   TERMINATION   WILDCARD
spring-boot-sample   spring-boot-sample-spring-boot-sample.apps-crc.testing          spring-boot-sample   8080                 None

And test one of the available endpoints:

$ curl -s spring-boot-sample-spring-boot-sample.apps-crc.testing/list | jq
[
  {
    "id": 1,
    "name": "frank"
  },
  {
    "id": 2,
    "name": "john"
  }
]

You can check the example application from GitHub: https://github.com/fmarchioni/masterspringboot/tree/master/openshift/deploy-openshift

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials