Chapter 10: Health Checking, Probes & Lifecycle
⚡ Interactive Problems in this Chapter (Click to solve in Playground)
1. Architectural Overview & Control Plane Mechanics
In Kubernetes, Health Checking, Probes & Lifecycle is reconciled through declarative state loops managed by the control plane and node daemons:
flowchart TD
subgraph ContainerBoot["Container Startup Phase"]
BOOT["Container Spawns (PID 1)"]
STARTUP{"Startup Probe<br/><i>failureThreshold: 30, period: 10s</i>"}
BOOT --> STARTUP
end
subgraph ActiveMonitoring["Operational Lifecycle Probes"]
LIVENESS{"Liveness Probe<br/><i>(Is process deadlocked?)</i>"}
READINESS{"Readiness Probe<br/><i>(Can process accept traffic?)</i>"}
end
subgraph EnforcementActions["Kubelet & Networking Actions"]
RESTART["Kubelet kills container<br/><i>(CrashLoopBackOff trigger)</i>"]
ADD_EP["EndpointSlice includes Pod IP<br/><i>(Receives Service Traffic)</i>"]
REMOVE_EP["EndpointSlice removes Pod IP<br/><i>(Traffic Diverted)</i>"]
end
STARTUP -->|Passes| LIVENESS
STARTUP -->|Passes| READINESS
STARTUP -->|Fails 30x| RESTART
LIVENESS -->|Fails threshold| RESTART
LIVENESS -->|Healthy| READINESS
READINESS -->|Success (Ready=True)| ADD_EP
READINESS -->|Failure (Ready=False)| REMOVE_EP
1.1 Architectural Flow & Lifecycle Walkthrough
- Container Process Startup (PID 1): The container runtime launches the container process inside isolated namespaces.
- Phase 1: Startup Probe Evaluation:
kubeletinitiates the configuredstartupProbe(HTTP GET, TCP Socket, gRPC, or Exec).- All
livenessProbeandreadinessProbechecks remain disabled while the startup probe executes. - If the startup probe succeeds within its
failureThreshold * periodSecondsbudget, the container transitions to active operational monitoring. If the budget is exhausted without success,kubeletkills the container and initiates a restart. - Phase 2: Operational Monitoring (Liveness & Readiness Parallel Loops):
- Liveness Probe:
kubeletperiodically executes the liveness check (e.g., HTTP/healthz). If consecutive failures reachfailureThreshold,kubeletterminates the container process withSIGTERM(followed bySIGKILLafterterminationGracePeriodSeconds) and increments the restart counter. - Readiness Probe:
kubeletperiodically executes the readiness check (e.g., HTTP/ready).- Success:
kubeletsets.status.conditions[Type=Ready].status = "True". TheEndpointSlicecontroller includes the Pod IP, enabling it to receive traffic from Kubernetes Services. - Failure:
kubeletsetsReady = False. TheEndpointSlicecontroller immediately removes the Pod IP from active endpoint pools, diverting traffic away from the unready container without killing the process.
- Success:
1.2 Serialization, Protocols & Communication Pathways
- HTTP/1.1 & HTTP/2 Prober Client: Kubelet's internal Go HTTP prober sends standard HTTP requests with configurable headers and timeouts directly to the container IP.
- gRPC Health Checking Protocol (v1): Kubelet invokes the standard
grpc.health.v1.Health/CheckRPC over HTTP/2, evaluating the returnedServingStatusenum (SERVING,NOT_SERVING). - Linux
/bin/sh -cExec RPC: For Exec probes, kubelet issues aExecSyncCRI gRPC call, evaluating the process exit code (0= Success, non-zero = Failure).
1.3 Deep-Dive Component Breakdown
- Kubelet Prober Worker: Dedicated background goroutine pool inside kubelet executing non-blocking probe checks per container according to
periodSecondsandtimeoutSeconds. - EndpointSlice Controller: Kubernetes controller that synchronizes Pod
Readystatus conditions into active load balancer and service routing tables. - Container Lifecycle Handler (
postStart/preStop): Execution hooks run asynchronously (postStart) or blocking before SIGTERM (preStop) to enable graceful connection draining. - Linux Process Signal Manager: Kubelet subsystem dispatching
SIGTERM(15) andSIGKILL(9) to container PID 1.
1.4 Under-The-Hood Mechanics & Failure Modes
- Cascading Failure Loop (Liveness on Overloaded Backends): Configuring a Liveness probe to check deep dependencies (like backend database connectivity) causes overloaded servers to fail liveness checks, prompting kubelet to kill and restart all instances simultaneously, amplifying the outage. Liveness probes must only verify shallow process health.
- Readiness Flapping under Heavy Load: If
timeoutSecondsis set too low (e.g., 1s) and the application experiences transient CPU starvation, readiness probes timeout, dropping the Pod from service endpoints and overloading the remaining healthy replicas. - PID 1 Signal Swallowing: If an application runs under a shell script without
exec(e.g.CMD ["sh", "-c", "node server.js"]), the shell process becomes PID 1 and may swallowSIGTERM, preventing graceful connection shutdown untilterminationGracePeriodSecondsexpires andSIGKILLis issued.
2. Annotated Production YAML Anatomy & Field Reference
Below is a production-grade declarative manifest demonstrating field definitions and operational patterns:
apiVersion: v1
kind: Pod
metadata:
name: robust-lifecycle-service
spec:
containers:
- name: web-app
image: nginx:1.27-alpine
ports:
- containerPort: 8080
startupProbe:
httpGet:
path: /healthz
port: 8080
failureThreshold: 30
periodSeconds: 2
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
timeoutSeconds: 2
failureThreshold: 3
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 2
periodSeconds: 5
successThreshold: 1
failureThreshold: 2
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 10"]
Key Field Schema Reference
| Field | Type | Description |
|---|---|---|
startupProbe |
Object |
Disables liveness/readiness checks until application initialization is complete. Ideal for slow JVM / ML warmups. |
livenessProbe |
Object |
Detects deadlocks or broken states; triggers kubelet container restart upon failure. |
readinessProbe |
Object |
Determines if the container can receive traffic; triggers removal from Service EndpointSlices when failing. |
lifecycle.preStop |
Object |
Executes synchronously before container receives SIGTERM, allowing in-flight requests to drain. |
3. Real-World Architectural Patterns
TCP Socket Readiness & Exec Liveness
apiVersion: v1
kind: Pod
metadata:
name: custom-probes
spec:
containers:
- name: redis
image: redis:7.2-alpine
ports:
- containerPort: 6379
livenessProbe:
exec:
command: ["redis-cli", "ping"]
periodSeconds: 10
readinessProbe:
tcpSocket:
port: 6379
periodSeconds: 5
gRPC Health Checking Protocol Probe
apiVersion: v1
kind: Pod
metadata:
name: grpc-service
spec:
containers:
- name: grpc-app
image: grpc-server:v1
ports:
- containerPort: 50051
livenessProbe:
grpc:
port: 50051
service: "HealthService"
initialDelaySeconds: 10
4. Production Hardening & Operational Governance
- Always include a
preStophook with a briefsleep(e.g. 5–10s) to give kube-proxy / iptables time to propagate endpoint removal before SIGTERM. - Never point liveness probes at downstream dependencies (e.g. database); liveness should test only local container health.
- Use
startupProbewith highfailureThresholdfor slow-booting applications rather than inflatedinitialDelaySecondson liveness probes.
5. Failure Modes & Diagnostic Triage Tree
Container Constantly Restarting (Unhealthy events)
Root Cause: Liveness probe timeout or non-200 HTTP response code.
Diagnostic Triage Sequence:
1. Run kubectl describe pod <name> and inspect Events.
2. Check probe response manually: kubectl exec -it <name> -- wget -qO- http://localhost:8080/healthz.
Pod Running but Service Not Serving Traffic
Root Cause: Readiness probe is failing, causing Pod exclusion from Endpoints.
Diagnostic Triage Sequence:
1. Check endpoint membership: kubectl get endpoints <service-name>
2. Check readiness status in kubectl describe pod <name>.
6. Interactive Practice Matrix
Practice concepts from this chapter directly in the interactive WebAssembly sandbox:
| Exercise ID | Challenge Description | Direct Link | Action |
|---|---|---|---|
health01 |
Liveness Probes | ../playground/index.html?exercise=health01 |
⚡ Solve health01 in Playground → |
health02 |
Readiness Probes | ../playground/index.html?exercise=health02 |
⚡ Solve health02 in Playground → |
health03 |
Startup Probes | ../playground/index.html?exercise=health03 |
⚡ Solve health03 in Playground → |
health04 |
Lifecycle Hooks & Graceful Shutdown | ../playground/index.html?exercise=health04 |
⚡ Solve health04 in Playground → |