Sub topic 13
Common pitfalls and anti-patterns
This chapter assumes you can write Deployments and Services, read YAML, and use kubectl. It also assumes you know what liveness, readiness, and startup probes do. Most probe incidents come from a small set of configuration mistakes, and they are easy to avoid once you know them.
Checking dependencies in the liveness probe
A liveness probe answers one question: is this process stuck in a way only a restart can fix? If /healthz queries the database and the database goes down, every Pod fails liveness. The kubelet restarts all of them at once. The restarts do not fix the database, and now the application is also down.
Keep liveness checks local to the process. Put dependency checks, if you want them at all, in the readiness probe. Even there, think before checking a shared dependency. If every replica reports not ready, the Service has no endpoints and every request fails.
Using the same endpoint for liveness and readiness
Liveness and readiness have different consequences. A failed readiness probe removes the Pod from the Service. A failed liveness probe restarts the container. Pointing both at one endpoint means a temporary condition, such as a full work queue, can trigger a restart when removing the Pod from traffic would have been enough. Use separate paths, as the chapter example does with /healthz and /ready.
Replacing a startup probe with a long initial delay
Setting initialDelaySeconds: 120 on a liveness probe delays every check, including after a restart. A fast start still waits 120 seconds before the first check. A start that takes 130 seconds still fails.
A startup probe is a better fit. It allows up to periodSeconds × failureThreshold for startup, then hands off to the other probes. In the earlier example that is 5 × 30 = 150 seconds.
Timeouts and thresholds that are too tight
The default timeoutSeconds is 1. Under CPU throttling or a garbage collection pause, a healthy endpoint can take longer than that to answer. With failureThreshold: 1, one slow response triggers a restart.
- Raise
timeoutSecondsfor endpoints that do real work. - Keep
failureThresholdat 3 or higher for liveness. - Check that CPU limits leave room for the health handler to run.
Probes that cannot fail, or never pass
Two opposite mistakes show up often:
- A handler that always returns 200 even when the application is broken. The probe passes while users see errors.
- An
execprobe that depends on a binary missing from the image, such ascurlin a distroless image. The command fails every time and the container restarts in a loop.
An httpGet probe counts any status from 200 to 399 as success. A redirect to a login page therefore passes, which may not be what you want.
Forgetting that probes share the container's resources
A probe runs while the application serves traffic. An exec probe starts a new process each time. Short intervals on many Pods add load, and a heavy handler can make a slow application slower.
Ignoring shutdown behavior
Readiness fails during shutdown only if your application makes it fail. If the process stops accepting connections immediately on SIGTERM, requests still routed to it fail. Make readiness report not ready first, then drain in-flight requests.
Summary of mistakes and fixes
| Mistake | Symptom | Fix |
|---|---|---|
| Dependency check in liveness | All Pods restart when a database fails | Keep liveness local |
| Shared endpoint | Restarts during temporary load | Separate `/healthz` and `/ready` |
| Large `initialDelaySeconds` | Slow recovery, or still fails | Use a startup probe |
| `timeoutSeconds: 1`, `failureThreshold: 1` | Random restarts under load | Raise both |
| Missing binary in `exec` probe | CrashLoopBackOff | Use `httpGet` or include the tool |
A safer liveness configuration
startupProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 5
failureThreshold: 30
livenessProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 10
timeoutSeconds: 3
failureThreshold: 3Exercise
A liveness probe calls /healthz, which runs a query against a shared database. The database has a 30-second outage. What happens to the Pods, and how would you change the setup?
Show answer
After three failed checks the kubelet restarts every Pod, so the application is down during the outage and for the restart time afterward. Make /healthz check only the process itself. If you want to react to the database, do it in the readiness probe, and consider whether removing all Pods from the Service is acceptable.
Exercise
A container needs up to 3 minutes to start, but usually starts in 10 seconds. Why is initialDelaySeconds: 180 on the liveness probe a poor choice, and what should you use?
Show answer
It delays the first liveness check by 180 seconds on every start, even fast ones, so a stuck container goes unnoticed for that long. Use a startup probe, for example periodSeconds: 5 with failureThreshold: 36, which allows 180 seconds but lets the other probes begin as soon as startup succeeds.