Kubernetes

Deploying a simple Spring Boot application to Kubernetes cluster

Steps for deploying a simple Spring Boot program to Kubernetes using Minikube:

  1. Create a simple Spring boot starter project and add the following classes:

class KubernetesTestApplication

package com.techiepitch;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class KubernetestestApplication {

       public static void main(String[] args) {

              SpringApplication.run(KubernetestestApplication.class, args);

       }

}

class TestController

package com.techiepitch.controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class TestController {

       @GetMapping(“/hello/{name}”)

       public String getHello(@PathVariable String name) {

              return “Hello ” + name;

       }

}

dockerfile

Create a file named dockerfile in the project folder and add the following content:

FROM adoptopenjdk/openjdk11

WORKDIR /opt

ENV PORT 8080

EXPOSE 8080

COPY target/*.jar /opt/kubernetesapp.jar

ENTRYPOINT exec java $JAVA_OPTS -jar kubernetesapp.jar

  • Install the following software:
  • Install VirtualBox

https://www.virtualbox.org/wiki/Downloads

  • Install Docker for Windows

https://docs.docker.com/desktop/windows/install/

  • Install Kubectl

https://kubernetes.io/docs/tasks/tools/

  • Install Minikube

https://learnk8s.io/installing-docker-kubernetes-windows

  • Containerize the above app using following Docker commands:

Docker build command for creating the container image:

In the above command, k8s-test is the name of the container image.

We have put “.” because dockerfile is in the current directory.

Run the command docker images to list the container images

  • Use docker network command to create a new network:
  • Docker run command to run the container image
  • Till now, we have created and run a docker image. We need to upload this container image to a container registry like DockerHub. For that you need to create a Docker ID. To create a Docker ID, go to https://hub.docker.com/signup
  • Once you have the Docker ID, run the command docker login for logging to Docker hub:
  • Before uploading image, rename image using format username/imagename:tag using docker tag command:
  • Use the command docker push for uploading the container image to Docker Hub
  1. Use the command docker run to run the container image using the image tag from docker repository
  1. Use the command Docker ps to list the container processes
  1. Next, we need to create a kubernetes cluster in Minikube.

Check the status of minikube using the command minikube status:

If the minikube is not in running state, use the command minikube start. This command starts the virtual machine and kubernetes cluster.

  1. Steps for defining a deployment and service:

Create a new directory

  1. Create a yaml file containing configuration for deployment and service as shown below.

k8s-test.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: k8s-test

spec:

  replicas: 1

  selector:

    matchLabels:

      app: k8s-test

  template:

    metadata:

      labels:

        app: k8s-test

    spec:

      containers:

        – name: k8s-test

          image: techiepitch/k8s-test:1.0.0

          ports:

            – containerPort: 8080

          imagePullPolicy: Always

—        

apiVersion: v1

kind: Service

metadata:

  name: k8s-test

spec:

  selector:

    app: k8s-test

  ports:

    – port: 80

      targetPort: 8080

  type: LoadBalancer

  1. Run kubectl apply command for applying the above deployment and service configuration to the kubernetes cluster or in other words, deploying the application to kubernetes cluster.
  1. Run kubectl get pods to check the current status of all pods.
  1. Run minikube service command to check the URL of the container application:
  1. Kubectl scale command to set replicas.