Deploy your First Spring Boot Application on Kubernetes

Deploy your First Spring Boot Application on Kubernetes

Hi all. Hope everyone is doing well. Many of us will find K8's a lot more theory than practical. I will say it's more practical than practical. Kubernetes is the future of our DevOps, and it is said to be getting advanced day after day.

With this, let's make our hands dirty with the very first Spring Boot application deployment on Kubernetes.


Prerequisites

  1. Docker - Download
  2. Kubernetes (You can install MiniKube)
  3. kubectl - Download
  4. STS/Intellij or any IDE - Download


minikube start        

Minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes. Run above command in order to instantiate minikube cluster.


Demo Application

As we have our cluster in running state, we can focus on creating our Spring Boot Application. Create a new Spring Boot Application like this:

Article content
Spring Boot Controller

application.properties

spring.application.name=demo
server.servlet.context-path=/demo        

Let's run this application to check if its working properly or not.

curl http://localhost:8080/demo/test        
If everything is working fine, let's do a mvn clean install to build our project. We will get a jar file after the build.

Now, we can write our Dockerfile in the root of the project in order to containerize our application.

FROM openjdk:17-jdk-alpine

EXPOSE 8080

ADD target/demo-0.0.1-SNAPSHOT.jar demo-0.0.1-SNAPSHOT.jar

ENTRYPOINT [ "java", "-jar", "demo-0.0.1-SNAPSHOT.jar" ]        

This file says: - the base image should be openjdk 17 and it should expose to port 8080. And our jar file will be taken as a target for the container, and command to run the application is java -jar demo-0.0.1-SNAPSHOT.jar.

Now, let's build our docker image.

Article content
Building Docker Image
Article content
Check for the Image
Article content
Run the Image

Let's run this application to check if it's working properly or not.

curl http://localhost:8080/demo/test        

It is working fine, but Docker is not providing us the Auto Heal and Auto Scaling features for our containers.

One should avoid Docker in production.

We have to build our Docker image in minikube cluster. Either we need to load this image to minikube cache or just create a new image in minikube itself.

For more, visit: How to Use Local Docker Images With Minikube? - GeeksforGeeks

For now, let's assume we wanted to use the existing Docker image in Minikube cluster.

minikube image load spring-boot-docker:latest        


Article content
Check whether image loaded or not.

As our image is successfully loaded in Kubernetes/Minikube cluster. Let's deploy our application.


Kubernetes Deployment

In advance to the docker deployment, we will also get the benefit of Auto Healing and Auto Scaling with K8's deployment.

Let's create a deployment.yaml file in the root directory: -

apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-boot-k8-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: spring-boot-k8-app
  template:
    metadata:
      labels:
        app: spring-boot-k8-app                 #this have to provide in service.yaml slector spec name
    spec:
      containers:
      - name: spring-boot-k8-container
        image: spring-boot-docker:latest 
        ports:
        - containerPort: 8080
        imagePullPolicy: IfNotPresent
        

imagePullPolicy: ifNotPresent will allow your Minikube to use the preexisting images.

kubectl apply -f deploment.yaml        
Article content
Two replicas created and running
Article content
Running on port 8080


But in order to test this out, the consumer needs to ssh into the cluster.

Article content
App is running fine

Users can access this application, but they need to be inside the cluster which is not an ideal scenario. This is where Kubernetes Services are into the action.


Kubernetes Services

Kubernetes creates a layer before deployment known as a service layer which act as a load balancer, service discovery and it can expose your resources to the outside world.

  • Service Types: -

Node Port: Someone from the organization can only access through Node Port. We are going to use Node Port for testing our application locally.

Load Balancer: Anyone can access through Load Balancer.

Let's create a service.yaml file in the root directory: -

apiVersion: v1
kind: Service
metadata:
  name: spring-bbot-deploy-service
  labels:
    app: spring-boot-k8-app
spec:
  type: NodePort
  ports:
  - port: 8080          # Port exposed within the cluster
    targetPort: 8080  # Port on the pods
    nodePort: 30007   # Port accessible externally on each node
  selector:
    app: spring-boot-k8-app  # Select pods with this label        
Providing the right selector and port is a crucial step.
Article content


Here, we have created a Node Port which will be mapped with Pod/Container port. We can access with <Node IP(Minikube IP)>:Node Port.

curl http://<minikube ip>:30007/demo/test        
Article content
Tested Successfully


We discussed in detail, how to:

  • Install Minikube on our local machine
  • Develop and build an example consisting of Spring Boot application
  • Deploy the services on a one-node cluster, using configuration files


For repository link or any doubts/suggestions: - You can DM me. Thanks, Happy Learning :)



To view or add a comment, sign in

More articles by P S

  • Payment Systems: Journey from Monoliths to Microservices

    Payment systems are definitely one of the best engineered systems out there. And one could probably gain a lot about…

  • Why Kubernetes?

    One might obviously think, what is the need to use Kubernetes. If it's all about containers, why not Docker then? Some…

  • K8's Architecture

    In worker nodes, data plane is being used and is responsible for executing the actual workload of the application. Some…

  • Mastering DevOps: A Roadmap for Backend Developers

    Backend developers, responsible for creating the server-side logic that powers web applications, have a valuable skill…

    2 Comments
  • Create Google Login with OAuth2 and Spring Security in 6 Easy Steps

    Are you tired of remembering multiple usernames and passwords for countless online accounts? Welcome to the world of…

    4 Comments

Others also viewed

Explore content categories