Getting started with Kubernetes for deploying a Spring boot application using Minikube

Getting started with Kubernetes for deploying a Spring boot application using Minikube

Previously I’ve containerized a spring boot application using Docker. I’ve chosen to reuse this project for this article. The image is pushed here. Also I’m using Minikube for this article. Today I’m going to deploy the image’s containers in a Kubenetes cluster using Minikube.

Note: This article is for those who have some ideas about Kubernetes and MiniKube. I’m not explaining in depth about those here.

Deployment script

Create a deployment file in the root project. Using this deployment file, I can create the k8s deployment object. I’ve named it my-deployment.yaml. You can choose anything you like.

Search deployments in k8s docs and go to the first result. It should be this page. Copy the content of nginx-deployment.yaml and modify that as our need. Here is the content.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    app: demoapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: demoapp
  template:
    metadata:
      labels:
        app: demoapp
    spec:
      containers:
        - name: app
          image: leeonscoding/demoapp:latest
          ports:
            - containerPort: 8080

Service script

The deployment object will create pods in clusters but the problem is how can we access those pods. Here we need the k8s service object. The service is a proxy which is exposed to external users. Let’s create one. I’ve created the my-service.yaml file. By searching service in k8s docs, I’ve found this page. Put the content of the service yaml and modify that.

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: demoapp
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

Create deployment object

First start the minikube

minikube start

Now create the deployment

kubectl apply -f my-deployment.yaml

Check the deployment object

kubectl get deployments

The output may look like this

NAME      READY   UP-TO-DATE   AVAILABLE   AGE
demoapp   0/3     3            0           58s

Check the pods

$ kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
demoapp-667bc45b8d-5hslq   1/1     Running   0          2m22s
demoapp-667bc45b8d-ct77c   1/1     Running   0          2m22s
demoapp-667bc45b8d-ms8k9   1/1     Running   0          2m22s

Check one pod’s logs

kubectl logs demoapp-667bc45b8d-5hslq

Create the service object

The command is as like as the deployment.

kubectl apply -f my-service.yaml

Check the services

$ kubectl get services
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   8d
my-service   ClusterIP   10.101.168.87   <none>        80/TCP    101s

Dashboard

Execute this command to see the minikube dashboard

 minikube dashboard

This will directly redirect to the dashboard in the default browser. Here we can see the list and status of the k8s objects like deployments, pods, services, replicas etc. visually.

See output

Let’s expose the service and see the output. For this, execute this command.

minikube service my-service

This will redirect to the browser with one of our pod’s result. In terminal, we can also see the URL like this

$ minikube service my-service
|-----------|------------|-------------|---------------------------|
| NAMESPACE |    NAME    | TARGET PORT |            URL            |
|-----------|------------|-------------|---------------------------|
| default   | my-service |        8080 | http://192.168.49.2:30846 |
|-----------|------------|-------------|---------------------------|
* Starting tunnel for service my-service.
|-----------|------------|-------------|------------------------|
| NAMESPACE |    NAME    | TARGET PORT |          URL           |
|-----------|------------|-------------|------------------------|
| default   | my-service |             | http://127.0.0.1:51321 |
|-----------|------------|-------------|------------------------|
* Opening service default/my-service in default browser...
! Because you are using a Docker driver on windows, the terminal needs to be open to run it.
* Stopping tunnel for service my-service.

Our output is

Delete Kubernetes objects

Very simple commands as follows

kubectl delete deployments my-development

kubectl delete services my-service

Conclusion

I hope now you can play with kubernetes commands and deploy any application using minikube. The code of this article is here. I hope you’ve enjoyed this article. Happy coding.