DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AppOps with Kubernetes and Devtron - The Perfect Fit
  • Auto-Scaling a Spring Boot Native App With Nomad
  • Manage Microservices With Docker Compose
  • Spring Boot With Kubernetes

Trending

  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  • You Are Using Claude Wrong (And So Is Everyone You Know)
  • Introduction to Retrieval Augmented Generation (RAG)
  • Reactive Kafka With Spring Boot
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Running SpringBoot Application On OpenShift

Running SpringBoot Application On OpenShift

In this article, I will try to demonstrate how to install and deploy a simple spring-boot application in OpenShift (Minishift).

By 
Ashish Choudhary user avatar
Ashish Choudhary
·
Nov. 17, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
24.8K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I will try to demonstrate how to deploy a simple spring-boot application on OpenShift (Minishift).

But wait what the hell is Openshift after all?.

OpenShift is RedHat’s cloud development Platform as a Service (PaaS). It is built on top of Docker containers and the Kubernetes container cluster orchestrator.

What is Paas?.

As per Wikipedia.

Platform as a service (PaaS) or application platform as a service (aPaaS) or platform-based service is a category of cloud computing services that provides a platform allowing customers to develop, run, and manage applications without the complexity of building and maintaining the infrastructure typically associated with developing and launching an app.

What Is Minishift?

Minishift is a tool that helps you run OpenShift locally by running a single-node OpenShift cluster inside a VM. You can try out OpenShift or develop with it, day-to-day, on your localhost.

Let’s get started then. I will be installing it on my mac but there are plenty of blogs/ resources available explaining how to install it on other popular OS like Windows, Linux, etc. You can go through the official installation instructions or follow the steps mentioned below.

Installation

These are basically the steps you need to follow to run minishift locally on macOS.

  1. Prerequisites
  2. Set up your virtualization environment
  3. Download Minishift software for your operating system from the Minishift Releases page manually.
  4. Install Minishift using Homebreq on macOS.
  5. Start Minishift
  6. Configure Minishift so you can use it efficiently.

As explained in point 5 you can start minishift with the below command

Shell
 




x


 
1
minishift start


Once you run the above command minishift performs some system checks like this.

Plain Text
 




xxxxxxxxxx
1
21


 
1
>Starting profile ‘minishift’
2

          
3
>Check if deprecated options are used … OK
4

          
5
>Checking if https://github.com is reachable … OK
6

          
7
>Checking if requested OpenShift version ‘v3.11.0’ is valid … OK
8

          
9
>Checking if requested OpenShift version ‘v3.11.0’ is supported … OK
10

          
11
>Checking if requested hypervisor ‘xhyve’ is supported on this platform ..OK
12

          
13
>Checking if xhyve driver is installed …
14

          
15
If everything is OK then you would see message like this at then end of your terminal.
16

          
17
>OpenShift server started.
18

          
19
>The server is accessible via web console at:
20

          
21
>https://192.168.64.3:8443/console


Accessing Web Console

Now you are good to go and browse the minishift web console. Since there is no authentication/authorization enabled by default you can create your own username and password when logging in for the first time.

Minishift Login page

Creating Project

First, you need to create a project. You have the option to create projects etc. via the command line and via the web console. We will be using the command line option. First login using below command as an admin user.

Shell
 




xxxxxxxxxx
1


 
1
oc login -u system:admin


Then by default, we can use the default project which is myproject for this demo. Alternatively, you can create a new project using oc new-project command.

Shell
 




xxxxxxxxxx
1


 
1
oc new-project <project_name> \
2

          
3
    --description="<description>" --display-name="<display_name>"


Creating Application

Now you need to select a base image for the application that you are going to create. Since this is a spring-boot application we will be using openjdk8 base image for our application. Use the below command line command to create an application with openjdk8 base image. Source code is available here.

Shell
 




xxxxxxxxxx
1


 
1
oc new-app registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift~https://github.com/userac/springboot-kubernetes.git — name=springboot-demo-openshift
2

          


Output of Above Command

Plain Text
 




xxxxxxxxxx
1
108


 
1
→ Found Docker image 6c975f1 (2 weeks old) from registry.access.redhat.com for “registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift”
2

          
3
Java Applications
4

          
5
— — — — — — — — -
6

          
7
Platform for building and running plain Java applications (fat-jar and flat classpath)
8

          
9
Tags: builder, java
10

          
11
* An image stream tag will be created as “openjdk18-openshift:latest” that will track the source image
12

          
13
* A source build using source code from https://github.com/userac/springboot-kubernetes.git will be created
14

          
15
* The resulting image will be pushed to image stream tag “springboot-demo-openshift:latest”
16

          
17
* Every time “openjdk18-openshift:latest” changes a new build will be triggered
18

          
19
* This image will be deployed in deployment config “springboot-demo-openshift”
20

          
21
* Ports 8080/tcp, 8443/tcp, 8778/tcp will be load balanced by service “springboot-demo-openshift”
22

          
23
* Other containers can access this service through the hostname “springboot-demo-openshift”
24

          
25
→ Creating resources …
26

          
27
imagestream.image.openshift.io “openjdk18-openshift” created
28

          
29
imagestream.image.openshift.io “springboot-demo-openshift” created
30

          
31
buildconfig.build.openshift.io “springboot-demo-openshift” created
32

          
33
deploymentconfig.apps.openshift.io “springboot-demo-openshift” created
34

          
35
service “springboot-demo-openshift” created
36

          
37
Build Status
38

          
39
Once the application is created the build will be automatically scheduled using S2I. You can view the logs using below command to check the status of the build.
40

          
41
oc logs -f bc/springboot-demo-openshift
42

          
43
Output of above command
44

          
45
Cloning “https://github.com/userac/springboot-kubernetes.git" …
46

          
47
Commit: bdf1e3a36a7c16b69567de1b5343ff9c51114536 (changing message)
48

          
49
Author: ashishchoudhary
50

          
51
Date: Sun Mar 22 00:22:23 2020 +0530
52

          
53
Using registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift@sha256:fa5f725ba5d0ed29f680a21d49e87d88ef0bad3db83158496eda33533cca10f8 as the s2i builder image
54

          
55
INFO Performing Maven build in /tmp/src
56

          
57
INFO Using MAVEN_OPTS -XX:+UseParallelOldGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=100m -XX:+ExitOnOutOfMemoryError
58

          
59
INFO Using Apache Maven 3.6.1 (Red Hat 3.6.1–6.3)
60

          
61
Maven home: /opt/rh/rh-maven36/root/usr/share/maven
62

          
63
Java version: 1.8.0_272, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10–1.el7_9.x86_64/jre
64

          
65
Default locale: en_US, platform encoding: ANSI_X3.4–1968
66

          
67
OS name: “linux”, version: “3.10.0–1127.19.1.el7.x86_64”, arch: “amd64”, family: “unix”
68

          
69
INFO Running ‘mvn -e -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga -Dfabric8.skip=true — batch-mode -Djava.net.preferIPv4Stack=true -s /tmp/artifacts/configuration/settings.xml -Dmaven.repo.local=/tmp/artifacts/m2 package’
70

          
71
[INFO] Error stacktraces are turned on.
72

          
73
[INFO] Scanning for projects…
74

          
75
[INFO] Downloading from central: https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.2.4.RELEASE/spring-boot-starter-parent-2.2.4.RELEASE.pom
76

          
77
[INFO] Downloaded from central: https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.2.4.RELEASE/spring-boot-starter-parent-2.2.4.RELEASE.pom (8.1 kB at 1.7 kB/s)…….
78

          
79
INFO] BUILD SUCCESS
80

          
81
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
82

          
83
[INFO] Total time: 02:44 min
84

          
85
[INFO] Finished at: 2020–11–11T05:07:46Z
86

          
87
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
88

          
89
[WARNING] The requested profile “openshift” could not be activated because it does not exist.
90

          
91
INFO Copying deployments from target to /deployments…
92

          
93
‘/tmp/src/target/springboot-kubernetes-0.0.1-SNAPSHOT.jar’ -> ‘/deployments/springboot-kubernetes-0.0.1-SNAPSHOT.jar’
94

          
95
Pushing image 172.30.1.1:5000/myproject/springboot-demo-openshift:latest …
96

          
97
Pushed 0/4 layers, 1% complete
98

          
99
Pushed 1/4 layers, 29% complete
100

          
101
Pushed 2/4 layers, 70% complete
102

          
103
Pushed 3/4 layers, 95% complete
104

          
105
Pushed 4/4 layers, 100% complete
106

          
107
Push successful
108

          


You’ve just built, pushed, and run a new runnable container image from source code in a git repository, no Dockerfile necessary.

As per Openshift documentation.

Using new-app results in a build configuration, which creates a new application image from your source code. It also constructs a deployment configuration to deploy the new image, and service to provide load-balanced access to the deployment running your image.

OpenShift Enterprise automatically detects whether the docker or source build strategy is being used, and in the case of source builds, detects an appropriate language builder image.

In our case with the new-app command, we have overridden the image the builder uses for a particular source repository by specifying the openjdk8 image stream and the repository, with a - as a separator.

Now let’s understand what an S2I is actually?

S2I

As per Openshift documentation.

Openshift’s Source-to-Image (S2I) is a toolkit and workflow for building reproducible container images from source code. S2I produces ready-to-run images by injecting source code into a container image and letting the container prepare that source code for execution. By creating self-assembling builder images, you can version and control your build environments exactly like you use container images to version your runtime environments.

Status

Run ‘oc status’ to view your application status.

Shell
 




xxxxxxxxxx
1


 
1
oc status


Output of Above Command

Plain Text
 




xxxxxxxxxx
1
10
9


 
1
In project My Project (myproject) on server https://192.168.64.4:8443
2

          
3
svc/springboot-demo-openshift — 172.30.105.115 ports 8080, 8443, 8778
4

          
5
dc/springboot-demo-openshift deploys istag/springboot-demo-openshift:latest <-
6

          
7
bc/springboot-demo-openshift source builds https://github.com/userac/springboot-kubernetes.git on istag/openjdk18-openshift:latest
8

          
9
deployment #1 deployed 20 minutes ago — 1 pod


As you can see that there is 1 pod running. You can view the same status on the web console also. You would see something like below. If you carefully observe we have not exposed the application to the outside world yet because we have not created a route. Let’s do that now and see if we can access the application.

Pod running

Route

In Openshift's term to expose our application to the outside world, we need to create routes. It can be created by executing the command given below:

Shell
 




xxxxxxxxxx
1


 
1
oc expose svc/springboot-demo-openshift


Output of Above Command

route.route.openshift.io/springboot-demo-openshift exposed

Similarly, if you goto web console applications>routes. You can see that route is created. Now our application is exposed to the outside world. Cool.

Route created

Let’s hit the URL and access our spring boot demo application.

We have successfully deployed our first spring-boot demo application to Openshift locally.

In the next article, we will cover how we can trigger builds if we make any changes to our Github repository.

application OpenShift Docker (software) Kubernetes Command (computing) Spring Boot Plain text Web Service operating system

Opinions expressed by DZone contributors are their own.

Related

  • AppOps with Kubernetes and Devtron - The Perfect Fit
  • Auto-Scaling a Spring Boot Native App With Nomad
  • Manage Microservices With Docker Compose
  • Spring Boot With Kubernetes

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook