Deploying a Containerized Spring Boot Application on Kubernetes

Rasalingam Ragul
4 min readMay 10, 2023

--

Kubernetes is a popular open-source container orchestration platform that can help you deploy and manage containerized applications. In this article, we will show you how to deploy a containerized Spring Boot application in Kubernetes.

Prerequisites Before getting started, you will need to have the following installed on your machine:

  • Docker
  • Kubernetes CLI (kubectl)
  1. Build and push your Docker image to a registry Before deploying your Spring Boot application to Kubernetes, you will need to build your Docker image and push it to a registry. You can follow the steps outlined in the previous articles to create a Dockerfile and build your Docker image. Once your image is built, you can push it to a Docker registry like Docker Hub or Google Container Registry.
  2. Create a Kubernetes deployment A deployment in Kubernetes is responsible for creating and managing replicas of a particular pod. To create a deployment for your Spring Boot application, create a new file called deployment.yaml and add the following YAML configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-boot-app
spec:
selector:
matchLabels:
app: spring-boot-app
replicas: 2
template:
metadata:
labels:
app: spring-boot-app
spec:
containers:
- name: spring-boot-app
image: ragul05/docker-spring-demo:v1.0
ports:
- containerPort: 8090

This configuration creates a deployment called spring-boot-app with two replicas. The template section specifies the configuration for the pods that will be created by the deployment. In this case, the pod will contain a single container called spring-boot-app that uses the Docker image you pushed to the registry in my case i pushed my image to DockerHub.

3. Create a Kubernetes service A service in Kubernetes is responsible for providing network access to a set of pods. To create a service for your Spring Boot application, create a new file called service.yaml and add the following YAML configuration:

apiVersion: v1
kind: Service
metadata:
name: spring-boot-app-service
spec:
selector:
app: spring-boot-app
ports:
- protocol: TCP
port: 80
targetPort: 8090
type: LoadBalancer

This configuration creates a service called spring-boot-app-service that targets the pods created by the spring-boot-app deployment. The ports section specifies that port 80 on the service should map to port 8090 on the container. The type section specifies that the service should be exposed externally as a LoadBalancer.

4. Before start deploy you need to start minikube for that run below command:

minikube start --driver=docker --force

5. Deploy your application to Kubernetes To deploy your Spring Boot application to Kubernetes, run the following commands in your terminal:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

This will create the deployment and service resources in your Kubernetes cluster. You can check the status of your deployment by running the following command:

kubectl get deployments

You should see the spring-boot-app deployment with two replicas like below.

6. Test your application Once your deployment and service are up and running, you can test your application by opening a web browser and navigating to the external IP address of the service. You can retrieve the external IP address by running the following command:

kubectl get services

You should see the spring-boot-app-service service with an external IP address. If everything is working correctly, you should see the expected output from your Spring Boot application.

7. Now check your minikube ip by running following command:

minikube ip

8. Now put your minikbe ip and find port number in your service in my case it’s look like this http://192.168.49.2:32614/

In conclusion, Kubernetes is a powerful platform for deploying and managing containerized applications. By creating a deployment and service for your Spring Boot application, you can easily manage replicas and provide network access.

https://github.com/rragul/docker-demo

--

--