My First Spring+Docker+Kubernetes project
I am using Spring Boot to build microservices for a while now but never invested in the idea of following the microservice standard creating Docker images and deploying the service in a Kubernetes Cluster.
In this story, we will learn to do the following in the same order, you can always skip the steps if you are comfortable with it already.
- Building a Simple Rest based microservice using Spring boot.
- Creating the Docker image of the microservice and publishing the docker image to the Docker hub.
- Setting up a Kubernetes cluster in your local environment(Mac is used for the exercise)
- Deploying the microservice into the Kubernetes cluster.
Building a Rest based microservice
The project can be cloned from here https://github.com/shahcoding/spring-kubernetes-docker
- Visit the https://start.spring.io/
- Provide the details as given in the screenshot below and click on `Generate`.
- Unzip the project and open the pom.xml with IDE.
- Set the
server.port=8082
or of your own choice. - Create a Rest Controller as below in the package
com.personal.info.docker.controller
- Run the application and check the endpoint
http://localhost:8082/info
You must see the API response, something like this
{
"name": "Abhishek Shah",
"age": 31,
"occupation": "Software Engineer"
}package com.personal.info.docker.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonalInfo {
@GetMapping(path = "/info")
public ResponseEntity<PersonalInfoDTO> getInfo(){
PersonalInfoDTO info = new PersonalInfoDTO();
info.setAge(31);
info.setName("Abhishek Shah");
info.setOccupation("Software Engineer");
return ResponseEntity.accepted().body(info);
}
}
class PersonalInfoDTO{
private String name;
private int age;
private String occupation;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
}
Creating the Docker Image of the Spring Boot Project
The exercise is performed specific to Mac but in principle should be similar for Windows and Ubuntu.
Download and install the Docker Desktop https://www.docker.com/products/docker-desktop
If you intend to push/publish your docker image for free sign-up at https://hub.docker.com/
If you have cloned the above git project, you will find the file Dockerfile
, else create a file named Dockerfile
in your project given below.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Run the commands
to create the docker image
#building and tagging the docker image.
docker build -t personalinfo:1.0 .
#Optional, if you intend to publish your image
#tagging the docker image(shahcoding is my docker hub account and personalinfo is repo name.docker build -t shahcoding/personalinfo:1.0 .#Pushing the image to the docker hub
docker push shahcoding/personalinfo:1.0
Let us now run the docker image as a container. The port traffic on port 8000 is forwarded to the port 8082 within the container
docker run --publish 8000:8082 --detach --name personalinfo personalinfo:1.0
Check the route in your browser http://localhost:8000/info and you should see something like this in the response:
{
"name": "Abhishek Shah",
"age": 31,
"occupation": "Software Engineer"
}
Once you are satisfied that the container is running just fine, you can delete it docker rm --force personalinfo
Deploying the docker container in Kubernetes Cluster
Assuming by now your docker is up and running and you would now like to deploy the container in Kubernetes. Lets first install Kubernetes in your machine. You can do in multiple ways:
https://kubernetes.io/docs/tasks/tools/install-minikube/
brew install minikube
Or your Docker desktop can do the Kubernetes installation for you:
Open docker desktop, select Preferences
and tick the checkbox for Enable Kubernetes
, and Apply and Restart
Once Kubernetes is installed, you should be able to validate by checking if you have kubectl
command available in your terminal.
Now let us now deploy the container in Kubernetes:
Create a YAML file to create a pod for the container. To learn in detail you have to read Kubernetes documentation: https://kubernetes.io/docs/home/
apiVersion: apps/v1
kind: Deployment
metadata:
name: personalinfo-demo
namespace: default
spec:
replicas: 1
selector:
matchLabels:
personalinfo: web
template:
metadata:
labels:
personalinfo: web
spec:
containers:
- name: personalinfo-site
image: personalinfo:1.0
Run the container in Kubernetes
kubectl apply -f personalinfo.yaml
Check if the container is running by using the commandkubectl get pods
and you will get something like this in response. This means that the container is up and running.
NAME READY STATUS RESTARTS AGEpersonalinfo-demo-6cb99f5cf6-mwrjd 1/1 Running 0 26s
Now we will set up the port-forward from local into the Kubernetes cluster kubectl port-forward {POD NAME from above output} 300001:8082
In my case it was like this:kubectl port-forward personalinfo-demo-6cb99f5cf6-mqrjd 300001:8082
Now go to the browser to see if the service is up and running: http://localhost:30001/info
In case if it does not show the response, you can check the logs within the Kubernetes cluster by running the command:
kubectl logs {POD NAME FROM above output}kubectl logs personalinfo-demo-6cb99f5cf6-mwrjd
To delete the pod kubectl delete -f personalinfo.yaml
I will keep adding more details to the article as I explore more features and keep updating the GitHub repo as well.