Deploy SpringBoot Application to Kubernetes(Minikube) Using Deployment yaml file

In the previous tutorial, you learnt How to Setup a Local Kubernetes Cluster (Minikube) and deploy a SpringBoot application. You also learn how to access the Minikube dashboard in the browser.

In this lesson, you will learn how to deploy Spring Boot application using a deployment file.

  1. Create a Docker Image
  2. Create a Deployment File
  3. Create a Deployment Object using Apply Command
  4. Create and Test the Service

 

1. Create a Docker Image

You first need to have a SpringBoot application. Then you also need to create a docker image of your SpringBoot application.

Step 1 – First use this command to allow Minikube access to our docker repositories:

eval $(minikube docker-env)

 

Step 2 – Use this command to create a docker image.

docker build -t springboot-kubernetes:1.0 .

At this point we have a docker image with name springboot-kubernetes with a tag of 1.0

 

2. Create a Deployment File

Inside the resources directory, create a file named deployment.yaml. The content is as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-kubernetes
spec:
  selector:
    matchLabels:
      app: springboot-kubernetes
  replicas: 3
  template:
    metadata:
      labels:
        app: springboot-kubernetes
    spec:
      containers:
        - name: springboot-kubernetes
          image: springboot-kubernetes:1.0
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080

 

3. Create the Deployment Using the Apply Command

Now that we have created the deployment file, we need to apply it.

Step 1 – Start minikube using the command minikube start.

Step 2 РNavigate to the resources directory containing the deployment

Step 3 РRun the command  below to apply the deployment

kubectl apply -f deployment.yaml

 

Step 4 – You can check the status of the deployment using the command:

kubectl get deployment

 

Step 5 – Use the command below to view the pods and their statuses:

kubectl kubectl get pods

 

Step 6 – Check the status of the pods using the command

 kubectl logs 

 

4. Create and Test the Service

We would need to be able to access the application from outside the Kubernetes cluster. For this, we need to create a service object.

Step 1 – Create a service.yaml file inside the resources directory. This is the content of the service.yaml file

apiVersion: v1
kind: Service
metadata:
  name: springboot-kubernetes-service
spec:
  ports:
    - protocol: "TCP"
      port: 8080        # The port inside the cluster
      targetPort: 8080  # The port exposed by the service
  type: NodePort        # Type of service
  selector:
    app: springboot-kubernetes

 

Step 2 – Apply the service using the command below

kubectl apply -f service.yaml

The service would then be created

You can then use kubectl get services command to check that the services is created.

To access the application, you can can use a combination of the internal-ip and the node port. The internal ip and the minikube ip are the same. To get these details, use the command below:

kubectl get nodes -o wide

 

I do recommend you watch the video on my YouTube Channel for more details.

kindsonthegenius

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

Your email address will not be published.