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
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:
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.
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
As our image is successfully loaded in Kubernetes/Minikube cluster. Let's deploy our application.
Recommended by LinkedIn
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
But in order to test this out, the consumer needs to ssh into the cluster.
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.
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.
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
We discussed in detail, how to:
For repository link or any doubts/suggestions: - You can DM me. Thanks, Happy Learning :)
Nice doc. Good job. Let’s discuss on K8 offline.
Thanks for sharing Pranav Sinha