Hi all in this blog we ‘ll learn about how to deploy spring boot application on Kubernetes. As we know that Kubernetes is a container orchestrator and it use to manage the containerized application. We just have to learn some basics of Kubernetes like what’s deployment, service and pod. These are some common terminology of Kubernetes that we must know.
If you don’t know about these 3 Kubernetes terminology no need to worry we are here for you. We have covered these in very simple language with example you can checkout the blog on Learn Kubernetes basics. Please go through this blog then you ‘ll understand better.
Let’s see what’s the prerequisite before we move to this example.
- You must know about docker and how do we create docker images. If you don’t know please follow our blog Spring boot with Docker.
- Basic of Kubernetes like what’s deployment, service and pods. Again if you don’t know please follow our blog on Learn Kubernetes basics.
Let’s start now and see how to deploy spring boot application on Kubernetes.
For this example we ‘ll create a simple spring boot project and expose one endpoint. After that we create the docker image of this project on our local system.
Once we have the docker image ready we ‘ll create the Kubernetes deployment using this image and run this deployment. After running Kubernetes deployment we ‘ll see the status of pods if it’s up and running we ‘ll create the Kubernetes service to expose this deployment pods to end-user.
For this example we are using Docker desktop. If you are following this example on any cloud say AWS or GCP make sure you have your Kubernetes cluster ready. And if you want to perform the same step like us just install the Docker Desktop on your system. This software have Kubernetes also but by default it’s off. We just need to enable this and for enabling Kubernetes on docker desktop we need to follow below step.
Steps to follow
- Go to Setting on Docker Hub Desktop
- Choose Kubernetes and tick the checkbox for Enable Kubernetes and start the cluster.
- Wait for sometime until Kubernetes cluster ready on your local system.
- Run below command to verify if cluster is there or not.
kubectl cluster-info


Now we have our Kubernetes cluster ready let’s start now. We ‘ll create a simple spring boot project and expose one endpoint that return a simple string as response. Then we ‘ll create the docker image for this project. Let’s see this two step first.
We ‘ll be using java 21 if you are using any other version of java please don’t forget to change the version in Dockerfile also.
Create a spring boot project and create a controller for exposing one endpoint
package com.javadream.SpringbootNginx.controlle;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class KubernetesController {
@GetMapping("/kubernetes")
public String testNginx() {
return "Hello from JavaDream you are learning kubernetes";
}
}Code language: CSS (css)
Go to your pom.xml file and in your maven plugin use the finalName attribute. This helps us to create the jar with defined name.

Create the Dockerfile for this spring boot application.
FROM eclipse-temurin:21-jdk-jammy
WORKDIR /javadream
COPY target/kubernetes-example.jar kubernetes-example.jar
ENTRYPOINT ["java", "-jar", "kubernetes-example.jar"]Code language: JavaScript (javascript)
Run below command to create the docker image of this project. Run command at root directory of your project where this Dockerfile present.
docker build -t vasu-kubernetes-image .
Above command ‘ll take some time just wait and let it complete. Once done run below command to verify that your docker image is created or not. Follow the screenshot for your reference.
docker images

We have our docker image ready. Let’s explore the Kubernetes part now and see how we deploy this application on Kubernetes cluster.
Kubernetes deployment and service creation for spring boot application
We already have docker image ready for our spring boot application. Now we have to create our deployment yaml file with this image. You can manually create the deployment yaml file or also can use the below command. Below command generate the deployment yaml file.
kubectl create deployment vasu-kube-deployment --image=vasu-kubernetes-image:latest --dry-run=client -o yaml > vasu-kube-deployment.yaml
In above command just change the –image value and put your image name there. After running this command you ‘ll get a yaml file with below content.
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: vasu-kube-deployment
name: vasu-kube-deployment
spec:
replicas: 1
selector:
matchLabels:
app: vasu-kube-deployment
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: vasu-kube-deployment
spec:
containers:
- image: vasu-kubernetes-image:latest
name: vasu-kubernetes-image
resources: {}
status: {}Code language: JavaScript (javascript)
Note: As we are running this application on our local system so when we create deployment from this file it ‘ll look the define image on DockerHub. But we haven’t push this image so we ‘ll get the error so if we create the deployment with this file we ‘ll get error you can try. So to avoid this error we add imagePullPolicy: Never property inside containers. It means use locally available image don’t pull from dockerhub.
Updated deployment file is
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: vasu-kube-deployment
name: vasu-kube-deployment
spec:
replicas: 1
selector:
matchLabels:
app: vasu-kube-deployment
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: vasu-kube-deployment
spec:
containers:
- image: vasu-kubernetes-image:latest
name: vasu-kubernetes-image
imagePullPolicy: Never
resources: {}
status: {}Code language: JavaScript (javascript)
We have our deployment file ready now just create the deployment using below command.
kubectl apply -f vasu-kube-deployment.yamlCode language: CSS (css)
Once running above command verify the deployment using below command. Follow the screenshot for reference.
kubectl get deploymentCode language: JavaScript (javascript)

Ready column 1/1 indicates the pods are up and running. You can check the pod and check the log of that pod you ‘ll see your spring boot application logs there. Use below command
kubectl get pods
kubectl logs vasu-kube-deployment-64674bd76-gsjx7Code language: JavaScript (javascript)

We have deployment ready now it’s time to expose the application to end-user for this we need to expose the deployment using Kubernetes service. Let’s create service and expose the deployment pods.
Kubernetes Service Creation
We need to create the service to expose the deployment pods so that end-user can access our application. Let’s create a service yaml file using below command.
kubectl expose deployment vasu-kube-deployment --type=LoadBalancer --port=80 --target-port=8080 --dry-run=client -o yaml > vasu-kube-service.yaml
Note: Make sure –target-port=8080 is the port of your spring boot application as our spring boot application running on port 8080 so we are using –target-port=8080
Above command create service yaml file. Let’s create a Kubernetes service using this yaml file. Use below command to create Kubernetes service.
kubectl apply -f vasu-kube-service.yamlCode language: CSS (css)
Verify using below command if service created successfully or not. And look at external-ip column we need to use this IP address to access our application.
kubectl get serviceCode language: JavaScript (javascript)
Follow screenshot for above commands for your reference.

We are done and all the steps that required to deploy spring boot application on Kubernetes cluster has been completed. It’s time to verify that we have successfully deploy our application to Kubernetes or not. Just use the external-ip and call your spring boot application endpoint that you expose.

You can see that we are able to call the endpoint and getting the response. Congratulations you have successfully deployed your application on Kubernetes cluster.
Hope you like this blog, If you like this please share this with others and help us to grow. Please share your feedback in our comment section.
If you need blog on any specific topic please mention that in our comment section we ‘ll try to cover that in our upcoming blogs.