Saturday, September 25, 2021

Deploying Spring Boot Application With Docker and Minikube on Windows

 image

Prerequisites:

  1. Spring Boot Application
  2. Docker should be installed.
  3. Minikube should be installed.

1. Spring Boot Application:

Here we have simple application with one controller. Nothing much to discuss.

2. Create Dockerfile in below path.

image

FROM adoptopenjdk/openjdk11:alpine-jre
ADD target/SpringBoot-Docker-Kubernates-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]

Before build Dockerfile check whether minikube is started? type command minikube status you will get the status as below, image

Here it is running. If not you can run the command minikube start.

Normally docker images will not be available for minikube. So run minikube docker-env. This will give below command. image

Run @FOR /f "tokens=*" %i IN ('minikube -p minikube docker-env') DO @%i in command prompt. Now if you run docker images we can get images for minikube. So

Now to build this application type following command. docker build -t springboot-k8s:2.0 . Now our application image is available in the docker images as below,

image

3. Create deployment and service yaml file for Kubernetes

  • Create Deployment yaml file:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-k8s-deploy
spec:
  selector:
    matchLabels:
      app: springboot-k8s
  replicas: 3
  template:
    metadata:
      labels:
        app: springboot-k8s
    spec:
      containers:
        - name: springboot-k8s
          image: springboot-k8s:2.0
          ports:
            - containerPort: 6060
  • Create Service yaml file:
apiVersion: v1
kind: Service
metadata:
  name: springboot-k8s-service
  labels:
    name: springboot-k8s
spec:
  ports:
    - nodePort: 31875
      port: 6060
      targetPort: 6060
      protocol: TCP
  selector:
    app: springboot-k8s
  type: NodePort
  • Run Both Service and Deployment file one by one with below command:
kubectl apply -f deployment.yml
kubectl apply -f service.yml
  • Now if you run below command:
kubectl get all

image

We can see all pods, services and deployments here.

  • Run below command to get the url of minikube to access the services.
minikube service springboot-k8s-service --url

Now we can get the url to access minikube as per below screenshot. image

image

That's all about deployment of spring boot application with Docker and kubernetes (minikube). We'll catch up with other topics in future.


Key Notes:

To access Ubuntu terminal in windows, 

1. we should type kubectl config view in command prompt. Then copy the server ip. 

2. Then we should go to terminal and type vi ~/.kube/config. This will open config file. There we should edit the below highlighted part from the screenshot. Mostly it will be port number.