Spring Boot + Kubernetes Tutorial - Deploy to Minikube Pods
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
Pods Overview
In a previous tutorial, we saw what is kubernetes and the need for it. Here we also discussed kubernetes architecture including pods. Kubernetes does not allow containers to run on their own. Instead containers run inside a construct called Pods. We can have multiple containers running inside a pod. This makes Pods the fundamental building block in Kubernetes.
Define Pods
In the kubernetes documentation they have given sample pod file. We will be making use of this sample file as a reference. In previous tutorial we have pushed an image to dockerhub. We will be writing a pod configuration file as follows. It gets the image from dockerhub and runs the container in a pod.apiVersion: v1 kind: Pod metadata: name: boot-jar spec: containers: - name: boot-jar-container image: javainuse/employee-producer-kubernetes:latestIf suppose instead of a dockerhub image we want to tell minikube to deploy a locally built image then we should use the imagePullPolicy specification as IfNotPresent. It will tell kubernetes to first check if the image is present locally. IfNotPresent: the image is pulled only if it is not already present locally.
apiVersion: v1 kind: Pod metadata: name: boot-jar spec: containers: - name: boot-jar-container image: javainuse/employee-producer-kubernetes:latest imagePullPolicy: IfNotPresent
Deploy Pods
Let us now use the above configuration file to deploy employee producer to minikube. Start Minikubeminikube start --force

If we list the docker images, we can see the images we created previously
docker image ls

To use docker with minikube we will need to switch the docker context to using minikube.
eval $(minikube docker-env)

Now all our Docker commands will be executed in the Minikube VM If we now list the docker images, we cannot see the image - employee-producer because we are now using minikube context.
docker image ls


We will now deploy the pod.yml to minikube using kubectl
kubectl apply -f pod.yml

We can check the pod status using following command
kubectl get pods

We can see that the pod has started successfully.
Next we can have the details of the pod running using the following command
kubectl describe pod boot-jar

kubectl logs boot-jar

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

We will now make use of the minikube ip to access the deployed service.
curl 192.168.49.2:8080

We get an exception that it cannot connect to port.


We will first list all the contents of the pod using the exec command.
kubectl exec boot-jar -- ls

Next using the shell terminal we will run the curl command from inside the pod.
kubectl -it exec boot-jar -- sh curl localhost:8080/employee
