Hello world in Kubernetes

Hello world in Kubernetes

Today we are going to learn about how to deploy the spring boot docker image into the Kubernetes cluster. As part of this, we are going to use a simple hello world spring boot application that prints hello world based on /hello-word exposed rest endpoint. First, we will convert the spring boot application to a docker image and then deploy the image to the Kubernetes cluster.

Prerequisites

  1. JDK
  2. Maven
  3. Docker
  4. Minikube or Kubernetes cluster
  5. Basic knowledge of Java, Spring boot, docker, and Kubernetes

As part of the local setup, I am using Docker Desktop on Windows, which comes up with Docker and Kubernetes. It's easy to install on a local machine and use. Refer link to download and install.

I have attached core snippets here but for complete code, reference refer - GitHub

Step-1 Create a spring boot application

1.1 Initialize a spring boot web application from Here. Select maven, java(8), jar and add spring web dependency
1.2 Add rest controller as mentioned below

@RestController
public class HelloWorldController {
    @RequestMapping(value = "hello-world", method= RequestMethod.GET)
    public String sayHello() {
        return "hello world";
    }
}

1.3 Create an executable JAR file

mvn clean install

1.4 Start the embedded tomcat server

java -jar target/hello-world-0.0.1-SNAPSHOT.jar

1.5 Test application from browser or any rest client

http://localhost:8080/hello-world

Step-2 Create a docker image

2.1 Create docker file as below. Here we are using OpenJDK as the base docker image.

FROM openjdk:11

WORKDIR /opt/app

COPY target/hello-world-*.jar /opt/app/app.jar

ADD docker-entrypoint.sh /opt/app/docker-entrypoint.sh

RUN chmod a+rx /opt/app/docker-entrypoint.sh

ENTRYPOINT ["/opt/app/docker-entrypoint.sh"]

2.2 Create docker-entrypoint file as below. In the docker file, we have specified reference of this file as an entry point so when the container starts this file will be executed first and it will start our application.

#!/bin/bash

java -jar /opt/app/app.jar

2.3 Create a docker image from the below command. As per the image, it will perform different steps as mentioned in the docker file.

docker build -t hello-world:1.0.0 .

docker-build-1.0.0.png

2.4 Check images on the Docker registry. From the below command, we can check the list of the images available on our machine.

docker images

2.5 Run docker image as a docker container from the below command.

docker run -p 8081:8080 -d --name=hello-world-1 -t hello-world:1.0.0

2.6 Check container is running or not

docker ps

2.7 Check logs for the running container

docker logs hello-world-1

docker-logs.png

2.8 Test from the below URL in the browser. Here, we need to test with port 8081 because our container is running on port 8080, but the exposed port is 8081.

http://localhost:8081/hello-world

2.9 Stop docker container

docker stop hello-world-1

2.10 Remove docker container

docker rm hello-world-1

Step-3 Create Kubernetes Pod

3.1 Create pod.yaml file as shown below. Here we are pulling the hello-world(1.0.0) image that we have created in the above step.

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
  labels:
    name: hello-world
spec:
  containers:
    - name: hello-world
      image: hello-world:1.0.0
      ports:
        - containerPort: 8080

3.2 Deploy pod in Kubernetes cluster. There are different ways to create pods in Kubernetes but the easiest way to create resources is based on a file. We can define multiple resources in the file as well.

kubectl apply -f .\pod.yaml

3.3 Get the list of the pod. As shown in the image, our pod has been started and it's in a ready state.

kubectl get pod

get-pod.png 3.4 To check logs of a specific pod

kubectl logs hello-world

k8s-logs.png

3.5 Expose pod to outside cluster. We can not access the created pod outside Kubernetes cluster without creating a service. Below command expose pod to 8080 port. As shown image, we can access it by external IP and in our case localhost.

kubectl expose pod hello-world --type=LoadBalancer --port=8080 --target-port=8080

pod-exposed.png

3.6 Test from the below URL in the browser

http://localhost:8080/hello-world

final-output.png

3.7 Delete pod

kubectl delete pod hello-world