Deploying Java Spring Boot Applications using Kubernetes

Somansh Kumar
Akeo
Published in
5 min readJan 24, 2020

Spring Boot is an open-source framework based on Java which is used to create microservices. Spring MVC which is a widely used module of spring which is used to create scalable web applications. Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can run. We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

It is time to learn how to deploy the sample Java Spring Boot application using Kubernetes and automated deployment through Jenkins.

Before we move ahead, please ensure that you have a sample Java Spring Boot application ready with you. In order to host the application with Kubernetes, we need to build the docker image. In case if you don’t have a ready application, a sample Java Spring Boot application is available here: https://github.com/akeotech/devops-kubernetes/tree/master/java-springboot/src

Note: We assume that you have basic knowledge of setup for dockers, Kubernetes and Jenkins. (if not, you can refer to our blog How to setup Dockers, Kubernetes and Jenkins)

Step 1: Creating simple dockerfile

##DockerFile# Pulling Base Image from Dockerhub
FROM maven:3.6.3-jdk-8
# Add Maintainer Info
MAINTAINER Somansh Kumar <somansh@akeo.no>
# Creating Code repo
RUN mkdir /home/app
# Working Directory
WORKDIR /home/app
# Copying files from repo to Working Directory
COPY . /home/app
# Creating JAR package
RUN mvn clean package
# Add a volume
VOLUME /tmp
# Expose port 8080 available to the world
EXPOSE 8080
# The application’s jar file
ARG JAR_FILE=/home/app/target/sample-websocket-demo-0.0.1.jar
# Copying JAR file
RUN cp /home/app/target/sample-websocket-demo-0.0.1.jar /sample-websocket.jar
# Run the jar file
ENTRYPOINT [“java”,”-Djava.security.egd=file:/dev/./urandom”,”-jar”,”/sample-websocket.jar”]

After this we will create a Kubernetes template.

Step 2: Creating Kubernetes yaml file

Create a Kubernetes yaml file (k8-springboot-app.yaml) with the following contents and place it in the root location of the project repository.The Kubernetes yaml file can be found here: https://github.com/akeotech/devops-kubernetes/blob/master/java-springboot/k8-springboot-app.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-springboot-app
labels:
app: sample-springboot-app
spec:
selector:
matchLabels:
app: sample-springboot-app
template:
metadata:
labels:
app: sample-springboot-app
spec:
containers:
- image: ip_address_of_the_machine:5000/sample-springboot-application:latest
name: springboot-chat
imagePullPolicy: Always
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: sample-springboot-app-svc
spec:
ports:
- name: “sample-springboot-app”
targetPort: 8080
port: 8080
nodePort: 30205
protocol: TCP
selector:
app: sample-springboot-app
type: NodePort

Lets, discuss the Kubernetes terminology from the above snippet:

  • apiVersion — Which version of the Kubernetes API being used to create this object
  • kind — What kind of object is being created
  • metadata — data that helps uniquely identify the object, including a name string, UID, and an optional namespace
  • spec — What state is being desired for the object
  • The selector field defines how the deployment finds the pods to manage, it is defined in the template (app: sample-springboot-app).
  • The template.spec defines the docker image being used to create the pod. Replace the ip_address_of_the_machine:5000 with the private docker registry details or the global dockerhub repository.
  • The containerport defines the port on which application will run inside the pod.

The Kubernetes Service section: The Kubernetes services is responsible for enabling network access to a set of pods.

  • port: defines the port on which application is running inside the pod
  • nodePort: defines the port to be bind to the base machine
  • protocol: TCP default protocol for network communication
  • selector: to identify the deployment created above

So till now we have done all the configurations and our repository looks like this.

Step 3: For Automated Jenkins Configuration

  • Creating Jenkins job
  • Open the jenkins url and click on “New Item”
  • Provide a name for the job and select freestyle job
  • Provide the git repository url and credentials for cloning the project. Also please specify the branch.
  • Select “Execute Shell” from the add build step
  • Provide the steps as shown
  • Save the job and execute it
  • This will create a docker image and push to the private docker registry. Also, the kubectl apply command will configure the pod with Sample Java Spring Boot application running inside it. The purpose of pushing the docker image to the docker registry is that this image can be used on any other Ubuntu machine to run the application.

Note: You can push images to dockerhub.com account too just need to replace the docker private registry.

sudo docker push <registry_name.dockerhub.com/sample-springboot-application:latest>

Access the sample application on the url http://ip_address_of_machine:30205

In this blog, we walked you through how to deploy the Java Spring Boot application using Kubernetes and automated deployment through Jenkins. Hopefully, you have gained enough knowledge to do the same and will take it to the next level by launching your own app with Kubernetes.

--

--