Pod Lifecycle: Init Containers, Probes & Termination
In this tutorial, you'll learn about Pod Lifecycle: Init Containers, Probes & Termination. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Every Kubernetes pod goes through a lifecycle from pending to running to terminated, with init containers running setup tasks, probes checking health, and hooks handling graceful shutdown.
What You'll Learn
This tutorial covers pod phases, init containers, startup and liveness probes, readiness probes, container lifecycle hooks, and graceful termination.
Why It Matters
Misconfigured probes cause cascading failures in production. Init containers that fail block deployments. Without proper termination handling, applications lose data during rolling updates or node drains.
Real-World Use
Netflix uses sophisticated liveness and readiness probes in their Spinnaker-managed deployments to ensure zero-downtime canary deployments, with init containers warming cache before the main application starts.
Pod Phases
A pod moves through five phases: Pending, Running, Succeeded, Failed, and Unknown.
# Watch pod phase transitions
kubectl get pods -w
# Examine pod status details
kubectl describe pod my-app
The describe output shows the phase, conditions, container states, and recent events.
Init Containers
Init containers run before the application containers start. They perform setup tasks like database migrations or permissions configuration.
apiVersion: v1
kind: Pod
metadata:
name: app-with-init
spec:
initContainers:
- name: db-migrate
image: migrate/migrate:latest
command: ['migrate', 'up']
env:
- name: DB_URL
value: "postgres://db:5432/app"
containers:
- name: app
image: myapp:1.0
Init containers run sequentially. If one fails, the pod restarts the entire init sequence.
# Check init container status
kubectl get pods -o wide
# View init container logs
kubectl logs app-with-init -c db-migrate
Container Probes
Kubernetes supports three probe types: startup, readiness, and liveness.
Startup Probe
Startup probes check if the application has started. They are ideal for slow-starting containers.
startupProbe:
httpGet:
path: /health/startup
port: 8080
initialDelaySeconds: 0
periodSeconds: 10
failureThreshold: 30
Liveness Probe
Liveness probes check if the container is still running. If they fail, the kubelet restarts the container.
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
failureThreshold: 3
Readiness Probe
Readiness probes check if the container is ready to serve traffic. Failed probes remove the pod from service endpoints.
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 3
periodSeconds: 5
failureThreshold: 2
Container Lifecycle Hooks
Kubernetes provides postStart and preStop hooks for lifecycle events.
preStop Hook for Graceful Shutdown
The preStop hook runs before the container terminates, allowing graceful shutdown.
containers:
- name: app
image: myapp:1.0
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 10 && /usr/local/bin/drain-connections"]
Termination Grace Period
The terminationGracePeriodSeconds controls how long Kubernetes waits after sending SIGTERM before sending SIGKILL.
spec:
terminationGracePeriodSeconds: 60
Graceful Termination Flow
When a pod is deleted, Kubernetes sends SIGTERM, waits the grace period, then sends SIGKILL.
# Delete pod gracefully
kubectl delete pod my-app
# Delete pod immediately
kubectl delete pod my-app --grace-period=0 --force
Practice Questions
What is the difference between liveness and readiness probes? Liveness probes restart unhealthy containers. Readiness probes control traffic routing to pods.
When would you use a startup probe instead of increasing initial delay? Startup probes are better for applications with variable startup times, like Java applications or legacy services.
What happens if an init container fails? The pod restarts all init containers from the beginning until they all succeed.
What is the purpose of the preStop hook? It runs custom cleanup commands before the container receives SIGTERM, enabling graceful shutdown.
How does terminationGracePeriodSeconds affect shutdown? It sets the time between SIGTERM and SIGKILL, giving the application time to clean up connections.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro