Search Tutorials


Spring Boot + Kubernetes Tutorial - Service Hello World Example | JavaInUse



Spring Boot + Kubernetes Tutorial - Service Hello World Example

In a previous tutorial we had deployed spring boot application to kubernetes pods. We were not able to access the deployed application using curl.
spring boot + minikube tutorial- exception
In this tutorial we will be developing a Nodeport service using which the external client will be able to access the deployed application.
kubernetes NodePort architecture

Video

This tutorial is explained in the below Youtube Video.

Spring Boot + Kubernetes Tutorial

What is Kubernetes? Need for it? Install Ubuntu on Windows using WSL Installing Kubectl, Minikube and Docker on Ubuntu Create Docker Image Deploy to Minikube Pods Difference between ClusterIP, NodePort and LoadBalancer Service Service Hello World Example

Implementation

In previous tutorial we had created the nodeport service configuration file as follows-
apiVersion: v1
kind: Service
metadata:
  name: employee-producer-service
spec:
  selector:
    app: boot-jar
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 30080
  type: NodePort
The above configuration file will create a service with selector tag - app: boot-jar. So the pod to which this service will be redirecting the request to should have the same tag. Also the external client will call this service using the nodeport 30080. This request will then be forwarded to service port 80 and then finally to target port 8080.
spring boot kubernetes NodePort
Start Minikube
minikube start --force

spring boot + minikube tutorial- minikube start
If we list the docker images, we can see the images we created previously
docker image ls

spring boot + minikube - docker image ls
To use docker with minikube we will need to switch the docker context to using minikube.
eval $(minikube docker-env)
Let us now deploy the service we had created before-
kubectl apply -f service.yml

kubectl apply -f service
In previous tutorial we had created a pod.yml for deploying spring boot application to kupernetes pods. For this we will be modifying the pod.yml to have the same label as the service.
kubernetes same label as the service
apiVersion: v1
kind: Pod
metadata:
  name: boot-jar
  labels:
    app: boot-jar
spec:
  containers:
  - name: boot-jar-container
    image: javainuse/employee-producer-kubernetes:latest
Delete existing pod named boot-jar that we had created earlier
kubectl delete pod boot-jar

kubectl delete pod
Next we will again create the pod as follows -
kubectl apply -f pod.yml

kubectl apply -f pod.yml
We can check the pod logs as follows
kubectl logs boot-jar

spring boot + minikube tutorial- logs of pod
As the pod is showing that the application has started successfully, we will test the application running in pod. For this we first need to get the minikube ip address as follows -
minikube ip

spring boot + minikube tutorial- minikube ip
We will now make use of the minikube ip and the node port to access the service.
curl 192.168.49.2:30080/employee

kubectl curl