Spring Boot is a framework which helps to get applications up and running as quickly as possible. OpenShift is a Platform as a Service (PaaS) product which makes it easy for developers to deploy applications. Putting both together feels like the natural thing to do. However, OpenShift (being a RedHat product) focuses on JBoss applications. Since we know that application servers are dead, I’m going to show how to deploy Spring Boot applications directly to OpenShift without the need to deploy into a container. So this blog post is for all developers who struggle with deploying Spring Boot applications to OpenShift.
Spring Boot S2I image
Since there is no official Spring Boot S2I image, we have created our own image. The codecentric/springboot-maven3-centos Image is capable of building Maven based projects. The resulting jar will simply be started using the
java -jar command. So this image isn’t really specific to Spring Boot, but can run any maven build that produces a fat jar in the end. We’re currently working on a S2I image for Gradle based builds. Furthermore we’re planning to add detection for maven-wrapper to the builder image, since more and more projects are starting to use it.
Update: The codecentric/springboot-maven3-centos builder image now supports maven wrapper builds.
Lets get our hands dirty and deploy a Spring Boot application to our OpenShift installation hosted at https://your-openshift-installation.com. First of all, we create a new project using the OpenShift CLI:
$ oc new-project spring-boot-sample
Now using project "springboot-sample-app" on server "https://your-openshift-installation.com".
You can add applications to this project with the 'new-app' command. For example, try:
$ oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-hello-world.git
to build a new hello-world application in Ruby.
Next we use the codecentric/springboot-maven3-centos builder image to create a new application. In this example we’re using a very small sample application that doesn’t do much.
$ oc new-app codecentric/springboot-maven3-centos~https://github.com/codecentric/springboot-sample-app.git
--> Found Docker image a118da0 (11 hours old) from Docker Hub for "codecentric/springboot-maven3-centos"
* An image stream will be created as "springboot-maven3-centos:latest" that will track the source image
* A source build using source code from https://github.com/codecentric/springboot-sample-app.git will be created
* The resulting image will be pushed to image stream "springboot-sample-app:latest"
* Every time "springboot-maven3-centos:latest" changes a new build will be triggered
* This image will be deployed in deployment config "springboot-sample-app"
* Port 8080/tcp will be load balanced by service "springboot-sample-app"
--> Creating resources with label app=springboot-sample-app ...
ImageStream "springboot-maven3-centos" created
ImageStream "springboot-sample-app" created
BuildConfig "springboot-sample-app" created
DeploymentConfig "springboot-sample-app" created
Service "springboot-sample-app" created
Build scheduled for "springboot-sample-app" - use the logs command to track its progress.
Run 'oc status' to view your app.
oc status command we can convince our self that everything is setup as we expect it:
$ oc status
In project springboot-sample-app on server https://your-openshift-installation.com
svc/springboot-sample-app - 172.17.240.24:8080
dc/springboot-sample-app deploys imagestreamtag/springboot-sample-app:latest <-
bc/springboot-sample-app builds https://github.com/codecentric/springboot-sample-app.git with springboot-sample-app/springboot-maven3-centos:latest
#1 deployment running for 12 seconds - 1 pod
View details with 'oc describe /' or list everything with 'oc get all'.
With the new-app command we have created a Service for our sample app. This service is accessible only inside the OpenShift installation. To make it accessible to the outside world, we need to expose it via a new route:
$ oc expose service springboot-sample-app --hostname=springboot-sample-app.your-openshift-installation.com
route "springboot-sample-app" exposed
This is all you have to do to get a Spring Boot application deployed and exposed in an OpenShift environment. Note, that you have to modify the hostname to fit your environment. The resulting deployment should look like the following in your OpenShift dashboard:
Deploying Spring Boot applications to OpenShift is a good solution for rapid application development. However, OpenShift does not provide the capability for building and running Spring Boot applications out of the box. In this blog post I showed how to use the codecentric/springboot-maven3-centos S2I builder image for deploying Spring Boot applications to OpenShift. With this you can get your next Spring Boot application up and running in no time.