K8s by Example: Liveness Probes
| Liveness probes detect broken containers. If the probe fails consecutively (failureThreshold times), Kubernetes kills and restarts the container. Use for: deadlocks, memory leaks, unrecoverable errors. |
| pod-liveness.yaml | |
| HTTP probe is the most common choice. Use it when your app has an HTTP endpoint. Healthy if status is 200-399. | |
| pod-liveness-headers.yaml | |
| HTTP probes can include custom headers for authentication or routing. Use | |
| pod-liveness-tcp.yaml | |
| TCP probe checks if the port accepts connections. Use for non-HTTP services (databases, Redis, gRPC without health checks). Limitation: it only confirms the port is open, not that the service is healthy. Prefer HTTP or exec when possible. | |
| pod-liveness-exec.yaml | |
| Exec probe runs a command inside the container. Healthy if exit code is 0. Use for custom health logic (checking files, running CLI tools like | |
| pod-startup-probe.yaml | |
| Startup probes replace liveness during container startup. Use for slow-starting apps instead of long initialDelaySeconds. Liveness probe only starts after startup probe succeeds. This example allows up to 5 minutes (30 × 10s) for startup. | |
| pod-probe-params.yaml | |
| Probe parameters: | |
| pod-liveness-best.yaml | |
| Common mistakes: (1) Too aggressive settings cause restart loops. Increase timeoutSeconds for slow endpoints. (2) Checking dependencies like databases or external APIs. If the DB is down, restarting won’t help and causes cascading failures. (3) Using liveness for apps that crash cleanly. Kubernetes already restarts crashed containers. Only use liveness for silent failures (deadlocks, hangs). | |
| terminal | |
| Debug probe failures with events and logs. Container restarts increment the RESTARTS counter. Check probe endpoint directly with kubectl exec or port-forward. | |