Master java skills

Setup Kubernetes

Setup Minikube

Install Kubectl

There are several methods of installing kubectl. The first one is to use curl. Run the following command to download the latest kubectl version

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl

For windows

Download the latest 1.28 patch release: kubectl 1.28.1.

Then create a new folder in C drive under a choice of your folder, name it ‘kube’ and copy  kubectl.exe inside it, as shown in the picture.

Set environment variable

Go to Advance System settings>Environment Variables and edit “path” by adding the path to your kubectl.exe

In this given example the value of environment variable will be D:\swdtools\softwares\kubectl

Open command prompt and test

The above screenshot confirms that kubectl is downloaded and recognized.

Install Docker

Follow the link

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

Install Minikube

Download minikube from the below link

https://github.com/kubernetes/minikube/releases/tag/v1.31.2

Choose the correct version as shown in the below screenshot

The other way of downloading it is explained below

Open powershell and run the following command

New-Item -Path 'c:\' -Name 'minikube' -ItemType Directory -Force
Invoke-WebRequest -OutFile 'c:\minikube\minikube.exe' -Uri 'https://github.com/kubernetes/minikube/releases/latest/download/minikube-windows-amd64.exe' -UseBasicParsing

Once it is downloaded, keep it under the same directory as shown below

Deploy spring boot application using Kubernetes

Create the docker file under the root directory of the spring boot project

Dockerfile

FROM openjdk:18

WORKDIR /app

COPY ./target/drink-boot-app-0.0.1-SNAPSHOT.jar /app

EXPOSE 8080

CMD ["java", "-jar", "drink-boot-app-0.0.1-SNAPSHOT.jar"]

Start minikube

minikube start

Check status

if you are seeing below message,

If you see above message, run the below command

Again run minikube status

Open powershell and run the below command

minikube docker-env
minikube docker-env | Invoke-Expression

Run the below command

docker images

Create a docker image

Go to the root directory of your spring boot project

docker build -t drink-app-test:latest .

Deployment on Kubernetes

Create kubernetes deployment file

create deployment.yaml file under the root directory of the spring boot project

apiVersion: apps/v1
kind: Deployment # Kubernetes resource kind we are creating
metadata:
  name: spring-boot-k8s
spec:
  selector:
    matchLabels:
      app: spring-boot-k8s
  replicas: 2 # Number of replicas that will be created for this deployment
  template:
    metadata:
      labels:
        app: spring-boot-k8s
    spec:
      containers:
        - name: spring-boot-k8s
          image: drink-app-test:latest
# Image that will be used to containers in the cluster
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080 
# The port that the container is running on in the cluster

Now, run the below command

kubectl apply -f deployment.yaml

Run the below command

Run the below command