Search Tutorials


Spring Boot + Kubernetes Tutorial - Deploy Multiple Microservices Using ClusterIP Service | JavaInUse



Spring Boot + Kubernetes Tutorial - Deploy Multiple Microservices Using ClusterIP Service

In previous tutorial we had created a service of type nodeport to access the kuberenetes deployed spring boot microservice named exployee-producer.
The REST endpoints exposed by the employee producer microservice we consumed by an external client using nodeport service.
kubernetes NodePort architecture
In previous docker tutorial we had created docker images for the employee-producer and employee-consumer microservices.
sprcloud_1-5
In this tutorial we will be creating the docker images for both the employee-producer and employee-consumer microservices. Then we will be deploying these images on minikube. Also in another previous tutorial we saw the different service types including clusterip which is used by internal cluster. In this tutorial we will be consuming the employee-producer microservice programatically using another microservice named employee-consumer using clusterip service type.
kubernetes NodePort architecture

Implementation

Start Minikube
minikube start --force

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

Deploy employee-producer application to kubernetes pod.

In previous tutorial, we had implemented examples for deploying spring boot applications to docker. In this tutorial series we had covered the basics of docker. We will be taking reference of the spring boot microservices to docker tutorial that we had implemented previously. We will be downloading the spring boot example we had implemented for this tutorial. The docker file used to create the docker image is as follows -
From openjdk:8
copy ./target/employee-producer-0.0.1-SNAPSHOT.jar employee-producer-0.0.1-SNAPSHOT.jar
CMD ["java","-jar","employee-producer-0.0.1-SNAPSHOT.jar"]
Import the spring boot project in eclipse. Build it using maven install, so that we have the jar file inside the target folder.
Spring boot Kubernetes example
Open the ubuntu terminal that we had previously installed using WSL. Next we will navigate to the location where we have the spring boot project.
Ubuntu Spring boot Kubernetes example
Next we will build an image with the name employee-producer-kubernetes.
docker image build -t employee-producer-kubernetes .

Spring boot Kubernetes example create docker image
If now list the available docker images, we can see that the new image has been created.
Spring boot Kubernetes list images
Next we will run the above image as a container. Also we will be publishing the docker port 8080 to centos port 8080.
docker container run --name producer -p 8080:8080 -d employee-producer-kubernetes

docker-container-running2
If now list the available docker containers, we can see that the new container has been created.
Spring boot Kubernetes list containers
Here we have started the container with name as producer. Now using the following command check the logs
docker container logs producer

docker-container-running-logs
Now using curl we could test the application running in docker as follows-
curl localhost:8080/employee

Spring boot Kubernetes curl example

Pass Docker Image to Kubernetes

The docker image can be passed to minikube in 2 ways-