Learn how to deploy SpringBoot Application to Kubernetes using Minikube and a deployment YAML file, create a Docker image, configure a service, and test the application.
TL;DR
-
Create a Docker image for the SpringBoot application.
-
Configure Minikube to access the local Docker repository.
-
Create a Kubernetes
deployment.yamlfile for the SpringBoot application. -
Apply the deployment and check the status of the deployment and pods.
-
Create a
service.yamlfile using a NodePort service. -
Access and test the SpringBoot application using the Minikube node IP and NodePort.
In the previous tutorial, you learnt How to Setup a Local Kubernetes Cluster (Minikube) and deploy a SpringBoot application. You also learn how to access the Minikube dashboard in the browser.
In this lesson, you will learn how to deploy Spring Boot application using a deployment file.
- Create a Docker Image
- Create a Deployment File
- Create a Deployment Object using Apply Command
- Create and Test the Service
1. Create a Docker Image
You first need to have a SpringBoot application. Then you also need to create a docker image of your SpringBoot application.
Step 1 – First use this command to allow Minikube access to our docker repositories:
eval $(minikube docker-env)
Step 2 – Use this command to create a docker image.
docker build -t springboot-kubernetes:1.0 .
At this point we have a docker image with name springboot-kubernetes with a tag of 1.0
2. Create a Deployment File
Inside the resources directory, create a file named deployment.yaml. The content is as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-kubernetes
spec:
selector:
matchLabels:
app: springboot-kubernetes
replicas: 3
template:
metadata:
labels:
app: springboot-kubernetes
spec:
containers:
- name: springboot-kubernetes
image: springboot-kubernetes:1.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
3. Create the Deployment Using the Apply Command
Now that we have created the deployment file, we need to apply it.
Step 1 – Start minikube using the command minikube start.
Step 2 – Navigate to the resources directory containing the deployment
Step 3 – Run the command below to apply the deployment
kubectl apply -f deployment.yaml
Step 4 – You can check the status of the deployment using the command:
kubectl get deployment
Step 5 – Use the command below to view the pods and their statuses:
kubectl kubectl get pods
Step 6 – Check the status of the pods using the command
kubectl logs
4. Create and Test the Service
We would need to be able to access the application from outside the Kubernetes cluster. For this, we need to create a service object.
Step 1 – Create a service.yaml file inside the resources directory. This is the content of the service.yaml file
apiVersion: v1
kind: Service
metadata:
name: springboot-kubernetes-service
spec:
ports:
- protocol: "TCP"
port: 8080 # The port inside the cluster
targetPort: 8080 # The port exposed by the service
type: NodePort # Type of service
selector:
app: springboot-kubernetes
Step 2 – Apply the service using the command below
kubectl apply -f service.yaml
The service would then be created
You can then use kubectl get services command to check that the services is created.
To access the application, you can can use a combination of the internal-ip and the node port. The internal ip and the minikube ip are the same. To get these details, use the command below:
kubectl get nodes -o wide
I do recommend you watch the video on my YouTube Channel for more details.
FAQs
How do I deploy a SpringBoot application to Kubernetes?
Create a Docker image for the application, define a Kubernetes Deployment in a YAML file, apply the deployment with kubectl apply, and expose the application using a Kubernetes Service.
How do I create a Docker image for a SpringBoot application?
After configuring Minikube to use the Docker environment, run docker build -t springboot-kubernetes:1.0 . to create the Docker image.
How do I create a Kubernetes Deployment YAML file?
Create a deployment.yaml file that defines the Deployment, application image, number of replicas, container port, and pod labels.
How do I expose a SpringBoot application running in Kubernetes?
Create a Kubernetes Service using the NodePort service type, configure it to target port 8080, and apply the service YAML file.
How do I access a SpringBoot application deployed to Minikube?
Use kubectl get nodes -o wide to obtain the node’s internal IP, then combine the IP with the NodePort assigned to the service to access the application.
Final Thoughts
Deploying a SpringBoot application to Kubernetes using Minikube provides a practical introduction to Kubernetes Deployments, Pods, Services, and Docker images.
Once the application image is created, the Deployment YAML can be applied to create multiple application pods. A NodePort Service then makes the application accessible from outside the cluster. From here, you can build on this setup by adding databases, persistent storage, configuration, security, and more advanced Kubernetes deployment features.
[…] Deploy SpringBoot Application to Kubernetes(Minikube) Using Deployment yaml file […]
[…] For YAML-only deployment patterns, compare with: Deploy Spring Boot using Deployment YAML. […]