Deploying a Spring Boot app

Kubernetes · lesson 15 of 32 · 5 min read

Take a Spring Boot jar all the way to a probed, configured, right-sized Deployment on a real cluster.

Open this lesson in the learning hub

Key points

  • Build with mvn spring-boot:build-image (Buildpacks) or a layered Dockerfile. Do not copy a fat jar into a raw JDK image.
  • Configure through env vars. SPRING_DATASOURCE_URL binds to spring.datasource.url with no extra code.
  • Wire the probes to the actuator liveness and readiness groups. Spring Boot enables them automatically on Kubernetes.
  • Set server.shutdown=graceful and a terminationGracePeriodSeconds longer than your slowest request.
  • Kubernetes sends SIGTERM and removes endpoints in parallel, so a short preStop sleep prevents dropped requests.
  • Always set requests, limits and MaxRAMPercentage, or a busy JVM gets OOMKilled at the worst possible time.

Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: orders
spec:
  replicas: 3
  selector:
    matchLabels:
      app: orders
  template:
    metadata:
      labels:
        app: orders
    spec:
      terminationGracePeriodSeconds: 45
      containers:
        - name: app
          image: ghcr.io/acme/orders:1.4.2   # never :latest
          ports:
            - containerPort: 8080
          envFrom:
            - configMapRef:
                name: orders-config
            - secretRef:
                name: orders-secrets
          env:
            - name: JAVA_TOOL_OPTIONS
              value: "-XX:MaxRAMPercentage=75"
          lifecycle:
            preStop:
              exec:
                command: ["sh", "-c", "sleep 5"]
          readinessProbe:
            httpGet:
              path: /actuator/health/readiness
              port: 8080
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /actuator/health/liveness
              port: 8080
            periodSeconds: 10
          resources:
            requests:
              cpu: "250m"
              memory: "768Mi"
            limits:
              memory: "768Mi"

A production Spring Boot pod is a pinned image, honest probes, graceful shutdown and real limits.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Kubernetes course, and every lesson in it is listed on the Kubernetes contents page.