A container (or a pod in Kubernetes) goes through several phases from creation to termination. Understanding these phases helps you integrate Spring Boot lifecycle hooks with Kubernetes hooks like postStart
, preStop
, and readiness probes.
1️⃣ Kubernetes Pod Lifecycle Phases
A Kubernetes pod (one or more containers) has the following high-level phases:
Phase | Description |
---|---|
Pending | Pod object accepted by Kubernetes, container images being pulled. |
ContainerCreating | Container is being created and initialized. |
Running | Container process has started. Pod may or may not be ready for traffic. |
Succeeded | All containers finished successfully (for jobs). |
Failed | Containers terminated with failure. |
Unknown | Kubernetes cannot determine pod state. |
2️⃣ Container Lifecycle Hooks
Kubernetes allows you to hook into container start and stop events.
a. postStart
Hook
- Runs immediately after container creation, concurrently with the main process.
- Does NOT wait for the app to finish initializing.
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Container started"]
Use cases:
- Create temporary files.
- Log container startup events.
- Lightweight initialization independent of the app.
b. preStop
Hook
- Runs before the container is terminated.
- Kubernetes gives a default termination grace period (usually 30s) to finish tasks.
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "echo Container stopping"]
Use cases:
- Deregister from service discovery.
- Flush caches to external storage.
- Notify other services before shutdown.
3️⃣ Readiness & Liveness Probes
These are checks Kubernetes uses to manage pod traffic and health.
Probe | Purpose | When to use |
---|---|---|
Liveness | Checks if container is alive; restarts if failed | Crash-loop prevention |
Readiness | Checks if container can handle traffic | Control traffic until initialization is done |
Example readiness probe:
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
Tip: Combine Spring Boot ApplicationReadyEvent
with readiness probe so the pod only receives traffic after app initialization.
4️⃣ Container Lifecycle Flow Diagram (Simplified)
Pod scheduled
│
▼
Container image pulled
│
▼
Container created ──▶ postStart hook runs
│
▼
Application starts (e.g., Spring Boot)
│
▼
readinessProbe passes ──▶ Pod marked Ready, receives traffic
│
▼
Container running (serving requests)
│
▼
Shutdown initiated
│
▼
preStop hook runs
│
▼
Termination grace period
│
▼
Container stopped
5️⃣ Best Practices
Don’t put long-running tasks in
postStart
hook
It can delay container readiness and may be killed if it exceeds hook timeout.Use
preStop
to gracefully shutdown services
Ensure Spring Boot shutdown hooks (like@PreDestroy
) are triggered.Combine with Spring Boot lifecycle hooks
-
ApplicationReadyEvent
for app-level initialization. -
@PreDestroy
orContextClosedEvent
for cleanup. - Kubernetes hooks for container-level concerns.
- Always test hooks under pod restart scenarios Ensure idempotency and avoid blocking pod startup.
Top comments (0)