Just a short introduction to deploying a Spring Boot application to a Kubernetes cluster and exposing it as a service

I won’t cover the Spring boot part here, so I assume we have a Jar file, I won’t cover the Kubernetes or Docker architecture either, it’s better described by themselves

When deploying to Kubernetes we need a docker image containing the spring boot jar and we obviously also need a kubernetes cluster.

So either install Minikube locally to play around with or use a Kubernetes cluster in the cloud. I just made a Kubernetes cluster on stackpoint.io and for this, I chose Digital Ocean as provider, since I already have hosted solutions there, but you can choose Google or Amazon or Azure if you prefer that.

On top of this, you will need kubectl command line tool to interact with your cluster, on mac osx just install by using homebrew.

brew install kubectl

You also need to have Docker installed locally and have an account on docker.io.

I’m keeping it short here, if you want to know more about the Kubernetes Architecture you should go read up on that on their site.

Docker part

We need a Docker image, and for this, I simply put the jar file and the configuration file in a directory and made a Dockerfile.

Here is the Dockerfile, with simple Java8 and ready to run a spring boot application on port 9092

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ARG CONFIG_FILE
ADD ${JAR_FILE} app.jar
ADD ${CONFIG_FILE} application.yml
EXPOSE 9092
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Build the docker image on the command line

docker build ./ -t turnipsoft/app --build-arg JAR_FILE=./jar.jar --build-arg CONFIG_FILE=./application.yml

Verify that it can run the docker image

docker run -p 9092:9092 -t turnipsoft/app

The I need to push to the docker repository for my Kubernetes install to find it, later on, so log in to your docker repo, and push it

docker login
docker push turnipsoft/app

 

Kubernetes part

When deploying to Kubernetes we need at least a Pod containing the Spring-boot docker image from above and a Service to expose it to the world. If we need to scale the Pod we also need a ReplicationController and the preferred way to do this is by creating a Deployment in Kubernetes. So there are 2 ways to do this, and I’m just gonna list both, but if you want scaling go for the Deployment method, else you have to make a replication controller afterward.

The first description is with one Pod, next is as a Deployment

Create a Pod

Make the yml descriptor of the Pod

apiVersion: v1
kind: Pod
metadata:
  name: app-backend
  labels:
    app: app
    stage: production
    placement: backend
spec:
  containers:
  - name: app-container
    image: turnipsoft/app
  ports:
  - containerport: 9092
    protocol: TCP

Create the pod in Kubernetes

kubectl create -f <file>.yml

or instead make a Deployment, then you have the replication controller all going as well, and is also the preferred way

Create a Deployment

Make the yml descriptor of the Deployment

apiVersion: apps/v1

kind: Deployment
metadata:
  name: app-deployment
  labels:
    app: <app>
    stage: production
    placement: backend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: <app>
      stage: production
      placement: backend
  template:
    metadata:
      labels:
        app: <app>
        stage: production
        placement: backend
    spec:
      containers:
      - name: virkr-backend
        image: turnipsoft/<app>
        ports:
        - containerPort: 9092

Create the Deployment in Kubernetes

kubectl create -f <file>.yml

Check it

kubectl get pods

Now we need a service to expose the Pod’s to the world

Create a Service

Make a service yml

apiVersion: v1
kind: Service
metadata:
  labels:
    name: app-service
spec:
  ports:
    -port: 9092
  selector:
    app: app
    stage: production
    placement: backend

Create service in Kubernetes

kubectl create -f <service-file>.yml

Check the service

kubectl get services

This will give you information about the assigned external IP and ports

Done

That’s it your service should now be running and you can access it on the IP address that the Kubernetes cluster assigned for the service

You could also explore exposing the service to the world through Ingress instead, go to Kubernetes.io and read more about that.