Learnitweb

Deploying a Spring Boot Application in localhost with Docker Desktop and helm

1. Create a Spring Boot Application

Use Spring Initializr to generate a new Spring Boot project with the following dependencies:

  • Spring Web

Create a new Spring Boot project and add a simple REST controller.

package com.example.helloworld;

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

@RestController
@RequestMapping("/api")
public class HelloWorldController {

    @GetMapping("/hello")
    public String helloWorld() {
        return "Hello, World!";
    }
}

2. Update application.properties (Optional)

You can set the server port in src/main/resources/application.properties:

server.port=8080

3. Build the Application

Run the following command to package the application:

mvn clean package

4. Create a Docker Image

Create a Dockerfile in the root of your project:

FROM openjdk:17-jdk-slim
COPY target/helloworld-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

5. Build and Tag the Image

Run the following command in your project directory:

docker build -t helloworld:latest .

6. Create a Helm Chart

Run the following command:

helm create helloworld

This will create a directory structure like:

helloworld/
  ├── charts/
  ├── templates/
  │   ├── deployment.yaml
  │   ├── service.yaml
  │   ├── ingress.yaml
  ├── values.yaml
  ├── Chart.yaml

7. Modify values.yaml

replicaCount: 1

image:
  repository: helloworld
  tag: latest
  pullPolicy: Never  # Since it's a local image

service:
  type: NodePort
  port: 8080

ingress:
  enabled: false  # Set to true if you want to enable Ingress
  annotations: {}
  hosts:
    - host: helloworld.local
      paths:
        - path: /
          pathType: ImplementationSpecific
  tls: []
  
autoscaling:
  enabled: false  # Set to true if you want Horizontal Pod Autoscaler (HPA)
  minReplicas: 1
  maxReplicas: 5
  targetCPUUtilizationPercentage: 80

8. Update deployment.yml

Modify helloworld/templates/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloworld
  template:
    metadata:
      labels:
        app: helloworld
    spec:
      containers:
        - name: helloworld
          image: helloworld:latest
          imagePullPolicy: Never
          ports:
            - containerPort: 8080

9. Update service.yaml

Modify helloworld/templates/service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: helloworld
spec:
  type: NodePort
  selector:
    app: helloworld
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30007  # Expose on port 30007

10. Install the Helm Chart

Deploy the application using:

helm install helloworld ./helloworld

11. Access the Application

Open your browser and go to:

http://localhost:30007/api/hello

You should see:

Hello, World!