Sub topic 12
Deploying to Kubernetes
This chapter shows how a Spring Boot container image runs on Kubernetes. It assumes you have built an image (see the Dockerfile chapter), know what a pod is, and have kubectl access to a cluster. The chapter ties together four earlier topics: environment configuration, secrets, health probes, and graceful shutdown.
The working model
You describe the desired state in YAML, and Kubernetes makes the cluster match it. Two objects cover most needs:
- A Deployment keeps a set of identical pods running and replaces them gradually during a release.
- A Service gives those pods one stable address and sends traffic only to pods that report ready.
Configuration and secrets
Non-secret settings go in a ConfigMap. Secrets go in a Secret. Both are injected as environment variables, which Spring Boot maps onto properties. The ${DB_URL} and ${DB_PASSWORD} placeholders from application.properties are then filled in at run time.
apiVersion: v1
kind: ConfigMap
metadata:
name: myapp-config
data:
DB_URL: jdbc:postgresql://db:5432/app
SPRING_PROFILES_ACTIVE: prod
---
apiVersion: v1
kind: Secret
metadata:
name: myapp-secret
type: Opaque
stringData:
DB_PASSWORD: change-meDo not commit a real Secret manifest to Git. Create it from your CI system or a secrets manager, and restrict who can read it.
The Deployment
The manifest below uses the probes and shutdown settings from the Actuator and graceful shutdown chapters.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
terminationGracePeriodSeconds: 45
containers:
- name: myapp
image: myorg/myapp:1.0.0
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: myapp-config
- secretRef:
name: myapp-secret
resources:
requests:
cpu: "500m"
memory: 768Mi
limits:
memory: 768Mi
startupProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
failureThreshold: 30
periodSeconds: 2
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
periodSeconds: 10
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
selector:
app: myapp
ports:
- port: 80
targetPort: 8080Each part has a job:
- Readiness probe. Kubernetes sends traffic to a pod only while this probe passes. Spring reports
OUT_OF_SERVICEon this endpoint once shutdown starts, so traffic drains away. - Liveness probe. If this fails repeatedly, Kubernetes restarts the container. Keep it limited to the application's own state. Do not make it depend on the database, or a database outage will restart every pod.
- Startup probe. It holds off the other probes while a slow application starts. Here it allows up to 60 seconds (30 tries at 2-second intervals).
- Grace period. After SIGTERM, Kubernetes waits
terminationGracePeriodSecondsbefore sending SIGKILL. Set it abovespring.lifecycle.timeout-per-shutdown-phase(30s), here 45s, so Spring can finish. - Memory limit. Setting the limit equal to the request gives the pod predictable memory. The JVM reads the container limit and sizes its heap from it (see the JVM sizing chapter).
Common errors
| Symptom | Likely cause |
|---|---|
| Pod restarts in a loop before it is ready | Liveness probe starts too early. Add a startup probe. |
| `OOMKilled` in `kubectl describe pod` | Heap plus non-heap memory exceeds the memory limit. Raise the limit or lower the heap share. |
| Probe returns 404 | Probes are not enabled. Set `management.endpoint.health.probes.enabled=true`. Inside Kubernetes Spring enables them automatically. |
| Errors during a release | Pod killed before it drained. Check the grace period and `server.shutdown=graceful`. |
| `CreateContainerConfigError` | The ConfigMap or Secret named in `envFrom` does not exist. |
Applying and checking
kubectl apply -f myapp.yaml
kubectl rollout status deployment/myapp
kubectl logs deployment/myappTo roll back a bad release, run kubectl rollout undo deployment/myapp.
Exercise
A new version of your app takes 90 seconds to start, and Kubernetes keeps restarting it. Which probe settings would you change, and why?
Show answer
Increase the startup probe's failureThreshold (or periodSeconds) so that threshold times period exceeds 90 seconds, for example 60 tries at 2 seconds. The liveness probe only begins after the startup probe succeeds, so it no longer kills the pod during startup.
Exercise
Why should terminationGracePeriodSeconds be larger than spring.lifecycle.timeout-per-shutdown-phase?
Show answer
Kubernetes sends SIGKILL when the grace period ends. If it is shorter than Spring's shutdown timeout, the pod can be killed while Spring is still finishing in-flight requests. A larger value lets graceful shutdown complete.