Run & Deploy Spring Boot Application on Kubernetes
Deploy spring boot application on Kubernetes
Deploying a Spring Boot application on Kubernetes involves several essential prerequisites and a step-by-step process to ensure a smooth setup. In this guide, we’ll walk you through the necessary steps, from setting up the environment to deploying and managing your application. The prerequisites for this process include Minikube, Java, Maven, and Docker.
Prerequisites:
- Minikube: A tool that runs a single-node Kubernetes cluster locally.
- Java: Required for running Spring Boot applications.
- Maven: A build automation tool used primarily for Java projects.
- Docker: A platform for developing, shipping, and running applications in containers.
Step 1: Create a Dockerfile for Your Spring Boot Application
First you need to create a Dockerfile in our spring boot application.
FROM openjdk:8
LABEL authors="chanukadinuwan"
EXPOSE 8080
ADD target/springboot-k8s-demo.jar springboot-k8s-demo.jar
ENTRYPOINT ["java", "-jar", "springboot-k8s-demo.jar"]
This Dockerfile sets up an OpenJDK 8 environment, labels the author, exposes port 8080, adds a JAR file to the image, and defines the entry point to run the JAR file using Java.
Step 2: Start Minikube
Then you need to start Minikube in your machine.
minikube start
Step 3: Build the Docker Image
Then we need to create image from our Dockerfile. For that you need to run this command.
docker build -t username/springboot-k8s-demo:latest .
This command builds a Docker image named `username/springboot-k8s-demo` with the `latest` tag using the Dockerfile in the current directory. Here username replace from your docker username.
Step 4: Verify the Docker Image
Then run this command.
docker images
Now you can see your image file and other image files like this.
Step 5: Create Kubernetes Deployment YAML
Now we need to create yaml file for kubernetes deployment. For that I’m using this yaml file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-boot-k8s
spec:
selector:
matchLabels:
app: spring-boot-k8s
replicas: 3
template:
metadata:
labels:
app: spring-boot-k8s
spec:
containers:
- name: spring-boot-k8s
image: <yourDockerUsername>/springboot-k8s-demo:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
This Kubernetes YAML configuration defines a Deployment for a Spring Boot application. Here’s a point-by-point explanation:
1. apiVersion: apps/v1:
— Specifies the API version used to create the Deployment resource.
2. kind: Deployment:
— Indicates that the resource being defined is a Deployment.
3. metadata:
— Contains metadata for the Deployment.
4. name: spring-boot-k8s:
— Sets the name of the Deployment to `spring-boot-k8s`.
5. spec:
— Defines the desired state of the Deployment.
6. selector:
— Specifies how to identify the Pods managed by the Deployment.
7. matchLabels:
— Uses a label selector to match Pods with the label `app: spring-boot-k8s`.
8. replicas: 3:
— Defines the desired number of Pod replicas (3 in this case).
9. template:
— Specifies the Pod template used to create new Pods.
10. metadata:
— Contains metadata for the Pod template.
11. labels:
— Assigns the label `app: spring-boot-k8s` to the Pods.
12. spec:
— Describes the containers and their configurations within the Pod.
13. containers:
— Lists the containers to be run in each Pod.
14. name: spring-boot-k8s:
— Names the container `spring-boot-k8s`.
15. image: <yourDockerUsername>/springboot-k8s-demo:latest:
— Specifies the Docker image to use for the container.
16. imagePullPolicy: IfNotPresent:
— Ensures the image is pulled only if it’s not present locally.
17. ports:
— Lists the ports to be exposed by the container.
18. containerPort: 8080:
— Exposes port 8080 inside the container.
Step 6: Apply the Deployment Configuration
Then you need to run this command:
kubectl apply -f k8s-deployment.yaml
Here I specified my project’s YAML file name. Instead of that, use whatever name you have chosen for your Kubernetes configuration YAML file.
Step 7: Verify the Deployment
kubectl get deployments
Here you can see deployment happened correctly. Now run:
kubectl get pods
Then It will list down all the pods here.
Step 8: View Logs
Now choose one and run this command:
kubectl logs <pod_name>
Now lets create k8s service object. For that let’s create new yaml file in our project. I named it as k8-service.
Step 9: Create Kubernetes Service YAML
Then need I’m going to use below content in that yaml file.
apiVersion: v1
kind: Service
metadata:
name: springboot-k8s-svc
spec:
selector:
app: spring-k8s-service
ports:
- protocol: "TCP"
port: 8080
targetPort: 8080
type: NodePort
This is a Kubernetes Service configuration YAML file for a service named `springboot-k8s-svc`. It uses API version `v1` and is of kind `Service`. The metadata specifies the name of the service. In the specification (`spec`), the selector matches pods labeled with `app: spring-k8s-service`. The service listens on port `8080` for TCP traffic and forwards requests to port `8080` on the target pods. The service type is `NodePort`, exposing the service on a static port on each node in the cluster.
Step 10: Apply the Service Configuration
Then again I’m going to apply this:
kubectl apply -f k8s-service.yaml
Now run this:
kubectl get svc
Step 11: Get the Service IP
Now we need to get IP. For that you need to run:
kubectl get nodes -o wide
Step 12: Access the Application
Now you can call your get ip using that ip address and port. It will work.
Step 13: Monitor the Components
To check status of the components. Run this command:
minikube dashboard
Deploying a Spring Boot application on Kubernetes involves creating a Docker image, starting Minikube, defining deployment and service YAML files, and using `kubectl` commands to deploy and manage the application. This process ensures scalability, high availability, and simplified management of containerized applications in a microservices architecture.