Spring Boot Deployment, Kubernetes, Health Checks, and CI/CD
Deploying Spring Boot Applications in Kubernetes
Section titled “Deploying Spring Boot Applications in Kubernetes”Build the Application
Section titled “Build the Application”mvn clean packageThis generates:
target/app.jarCreate a Docker Image
Section titled “Create a Docker Image”FROM eclipse-temurin:17-jre
WORKDIR /appCOPY target/app.jar app.jar
ENTRYPOINT ["java","-jar","/app/app.jar"]Build and push:
docker build -t myorg/springboot-app:1.0 .docker push myorg/springboot-app:1.0Kubernetes Deployment
Section titled “Kubernetes Deployment”apiVersion: apps/v1kind: Deploymentmetadata: name: springboot-appspec: 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: 8080Service
Section titled “Service”apiVersion: v1kind: Servicemetadata: name: springboot-servicespec: selector: app: springboot-app ports: - port: 80 targetPort: 8080 type: LoadBalancerDeploy:
kubectl apply -f deployment.yamlkubectl apply -f service.yamlArchitecture
Section titled “Architecture”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”Use Small Base Images
Section titled “Use Small Base Images”Prefer:
eclipse-temurin:17-jreThe official openjdk Docker Hub images are unmaintained — use eclipse-temurin (Adoptium) instead.
Use Layered JARs
Section titled “Use Layered JARs”mvn spring-boot:build-imageLayers:
dependenciesspring-boot-loadersnapshot-dependenciesapplicationBenefits:
- Faster Docker builds
- Better layer caching
Externalize Configuration
Section titled “Externalize Configuration”Use ConfigMaps, Secrets, and environment variables.
env:- name: SPRING_PROFILES_ACTIVE value: prodConfigure JVM for Containers
Section titled “Configure JVM for Containers”env:- name: JAVA_OPTS value: "-Xms512m -Xmx512m"Run as Non-root
Section titled “Run as Non-root”RUN addgroup spring && adduser spring -G springUSER springEnable Spring Boot Actuator
Section titled “Enable Spring Boot Actuator”management.endpoints.web.exposure.include=health,info,metricsHealth Checks in Kubernetes
Section titled “Health Checks in Kubernetes”Spring Boot Actuator endpoint:
/actuator/healthConfiguration:
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: 5Health check flow:
flowchart TD
K[Kubernetes] --> H["/actuator/health"]
H -->|Healthy| R[Keep Pod Running]
H -->|Unhealthy| S[Restart Pod]
Readiness vs. Liveness Probe
Section titled “Readiness vs. Liveness Probe”| 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 |
Liveness Probe
Section titled “Liveness Probe”Checks whether the application should be restarted.
Examples:
- Thread deadlock
- Memory leak
- Application freeze
Readiness Probe
Section titled “Readiness Probe”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.
Zero-Downtime Deployment
Section titled “Zero-Downtime Deployment”Rolling Update Strategy
Section titled “Rolling Update Strategy”strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 0 maxSurge: 1Deployment flow:
flowchart LR
A[v1 Pod] --> B[v2 Pod Created]
B --> C[v1 Pod Removed]
C --> D[Repeat Until Complete]
Other Strategies
Section titled “Other Strategies”Blue-Green Deployment
Section titled “Blue-Green Deployment”- Blue = current version
- Green = new version
- Switch traffic instantly
Canary Deployment
Section titled “Canary Deployment”- 90% traffic -> old version
- 10% traffic -> new version
- Gradually increase traffic
CI/CD for Spring Boot Applications
Section titled “CI/CD for Spring Boot Applications”Pipeline:
flowchart LR
A[Git] --> B[Build]
B --> C[Test]
C --> D[Docker Build]
D --> E[Push Image]
E --> F[Deploy to Kubernetes]
Stages:
- Checkout source
- Build application
- Run tests
- Build Docker image
- Push image
- Deploy to Kubernetes
Commands:
mvn clean packagemvn testdocker build -t myorg/app:1.0 .docker push myorg/app:1.0kubectl apply -f deployment.yamlor
helm upgradeGitHub Actions Example
Section titled “GitHub Actions Example”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/Production Architecture
Section titled “Production Architecture”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]
Common Interview Follow-up Questions
Section titled “Common Interview Follow-up Questions”- What is Helm in Kubernetes?
- How do you manage secrets in Kubernetes?
- How do you scale Spring Boot applications in Kubernetes?
- How do you monitor Kubernetes microservices?
- What is Horizontal Pod Autoscaler (HPA)?
- What is a Service Mesh (Istio)?
Key Takeaways
Section titled “Key Takeaways”- 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.