Search Tutorials


Spring Boot + Kubernetes Tutorial - Deploy to Minikube Pods | JavaInUse



Spring Boot + Kubernetes Tutorial - Deploy to Minikube Pods

In previous tutorial we created a docker image for spring boot application. In this tutorial we will be passing the image to the single node kubernetes server i.e minikube which will then deploy it as a container using pods. Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

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.
Microserservices Orchestration using 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:latest
If 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 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)

spring boot + minikube - 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

spring boot + minikube -kubectl docker image ls
Let us now create the pod configuration file named pod.yml file we discussed earlier.
spring boot + minikube tutorial-pod.yml
We will now deploy the pod.yml to minikube using kubectl
kubectl apply -f pod.yml

spring boot + minikube tutorial- deploy pod
We can check the pod status using following command
kubectl get pods

spring boot + minikube tutorial- check pod status
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

spring boot + minikube tutorial- describe pod
Also 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 to access the deployed service.
curl 192.168.49.2:8080

spring boot + minikube tutorial- cannot reach ip
We get an exception that it cannot connect to port.
spring boot + minikube tutorial- exception
To connect correctly to the minikube cluster we will need to implement services which we will do in next tutorial. For this tutorial to test the application, what we can do is using the exec command go inside the pod and then test the application from within the pod. This exec command is similar to the docker exec command that we have used in previous docker tutorials.
spring boot + minikube tutorial- kubectl exec connect
We will first list all the contents of the pod using the exec command.
kubectl exec boot-jar -- ls

spring boot + minikube tutorial- kubectl exec 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

spring boot + minikube tutorial- kubectl exec sh