Skip to content

Spring Boot Deployment, Kubernetes, Health Checks, and CI/CD

Deploying Spring Boot Applications in Kubernetes

Section titled “Deploying Spring Boot Applications in Kubernetes”
Terminal window
mvn clean package

This generates:

target/app.jar
FROM eclipse-temurin:17-jre
WORKDIR /app
COPY target/app.jar app.jar
ENTRYPOINT ["java","-jar","/app/app.jar"]

Build and push:

Terminal window
docker build -t myorg/springboot-app:1.0 .
docker push myorg/springboot-app:1.0
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-app
spec:
replicas: 3
selector:
matchLabels:
app: springboot-app
template:
metadata:
labels:
app: springboot-app
spec:
containers:
- name: springboot-app
image: myorg/springboot-app:1.0
ports:
- containerPort: 8080
apiVersion: v1
kind: Service
metadata:
name: springboot-service
spec:
selector:
app: springboot-app
ports:
- port: 80
targetPort: 8080
type: LoadBalancer

Deploy:

Terminal window
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
flowchart TD
    A[Spring Boot App] --> B[Docker Image]
    B --> C[Container Registry]
    C --> D[Kubernetes Deployment]
    D --> E[Pods]
    E --> F[Service / LoadBalancer]

Best Practices for Containerizing Spring Boot Applications

Section titled “Best Practices for Containerizing Spring Boot Applications”

Prefer:

eclipse-temurin:17-jre

The official openjdk Docker Hub images are unmaintained — use eclipse-temurin (Adoptium) instead.

Terminal window
mvn spring-boot:build-image

Layers:

dependencies
spring-boot-loader
snapshot-dependencies
application

Benefits:

  • Faster Docker builds
  • Better layer caching

Use ConfigMaps, Secrets, and environment variables.

env:
- name: SPRING_PROFILES_ACTIVE
value: prod
env:
- name: JAVA_OPTS
value: "-Xms512m -Xmx512m"
RUN addgroup spring && adduser spring -G spring
USER spring
management.endpoints.web.exposure.include=health,info,metrics

Spring Boot Actuator endpoint:

/actuator/health

Configuration:

containers:
- name: springboot-app
image: myorg/springboot-app
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 20
periodSeconds: 5

Health check flow:

flowchart TD
    K[Kubernetes] --> H["/actuator/health"]
    H -->|Healthy| R[Keep Pod Running]
    H -->|Unhealthy| S[Restart Pod]
Feature Liveness Probe Readiness Probe
Purpose Check if app is alive Check if app is ready to receive traffic
Failure Action Restart pod Remove pod from Service
Typical Use Deadlock, crash DB unavailable, cache warming

Checks whether the application should be restarted.

Examples:

  • Thread deadlock
  • Memory leak
  • Application freeze

Checks whether the application can receive traffic.

Examples:

  • Database unavailable
  • Kafka not ready
  • Cache warming

Interview answer: Liveness determines whether a container should be restarted, while readiness determines whether it should receive traffic.

strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1

Deployment flow:

flowchart LR
    A[v1 Pod] --> B[v2 Pod Created]
    B --> C[v1 Pod Removed]
    C --> D[Repeat Until Complete]
  • Blue = current version
  • Green = new version
  • Switch traffic instantly
  • 90% traffic -> old version
  • 10% traffic -> new version
  • Gradually increase traffic

Pipeline:

flowchart LR
    A[Git] --> B[Build]
    B --> C[Test]
    C --> D[Docker Build]
    D --> E[Push Image]
    E --> F[Deploy to Kubernetes]

Stages:

  1. Checkout source
  2. Build application
  3. Run tests
  4. Build Docker image
  5. Push image
  6. Deploy to Kubernetes

Commands:

Terminal window
mvn clean package
mvn test
docker build -t myorg/app:1.0 .
docker push myorg/app:1.0
kubectl apply -f deployment.yaml

or

Terminal window
helm upgrade
name: Spring Boot CI/CD
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: mvn clean package
- name: Build Docker
run: docker build -t myapp .
- name: Deploy
run: kubectl apply -f k8s/
flowchart TD
    Dev[Developer] --> Git[GitHub / GitLab]
    Git --> CI[CI Pipeline]
    CI --> Img[Docker Image]
    Img --> Reg[Container Registry]
    Reg --> CD[CD Pipeline]
    CD --> K8s[Kubernetes]
    K8s --> Pods[Pods]
    Pods --> Svc[Service / Ingress]
  1. What is Helm in Kubernetes?
  2. How do you manage secrets in Kubernetes?
  3. How do you scale Spring Boot applications in Kubernetes?
  4. How do you monitor Kubernetes microservices?
  5. What is Horizontal Pod Autoscaler (HPA)?
  6. What is a Service Mesh (Istio)?
  • Package Spring Boot applications as Docker images and deploy them using Kubernetes Deployments and Services.
  • Use lightweight base images, layered JARs, externalized configuration, non-root users, and Actuator endpoints for production-ready containers.
  • Configure both liveness and readiness probes to improve reliability and traffic management.
  • Use rolling updates, blue-green, or canary deployments to achieve zero downtime.
  • Automate build, test, image creation, and Kubernetes deployment using a CI/CD pipeline.