Why does a database check in livenessProbe crash healthy pods?
Question
A Deployment runs 20 API pods behind a Kubernetes Service. Both probes call /health, which runs SELECT 1 on PostgreSQL. Each pod takes 70 seconds to boot and warm its local cache.
livenessProbe:
httpGet: { path: /health, port: 8080 }
periodSeconds: 10
timeoutSeconds: 2
failureThreshold: 3
readinessProbe:
httpGet: { path: /health, port: 8080 }
periodSeconds: 10
timeoutSeconds: 2
failureThreshold: 3PostgreSQL fails over for 45 seconds.
- What happens to all 20 pods during the failover, and why do they enter
CrashLoopBackOffafterward? - How should you split
/livez,/readyz, andstartupProbe?
Show hints (3)
- "CHECK FAILURE WINDOW"
Multiply
periodSeconds(10) byfailureThreshold(3). Compare that 30-second window against both the 45-second failover and the 70-second boot time. - "SEPARATE TWO ACTIONS"
livenessProbetellskubeletto kill and restart the container.readinessProbeonly removes the Pod IP from Service endpoints. - "PROTECT SLOW BOOT"
Without a
startupProbe,livenessProbestarts right away (initialDelaySecondsdefaults to 0) and kills any container still warming up after 30 seconds.
Show checked explanation
During the 45-second failover, every pod fails livenessProbe three times within 30 seconds, so kubelet kills and restarts all 20 healthy pods at once. Because boot takes 70 seconds with no startupProbe, liveness kills them again at 30 seconds, causing CrashLoopBackOff. Keep /livez process-only, move dependency checks to /readyz, and add startupProbe.
def livez(alive): return 200 if alive else 500 def readyz(alive, db_ok): return 200 if (alive and db_ok) else 503 def sim(pods, ticks, thresh, probe_fn): fails = sum(1 for _ in range(ticks) if probe_fn() >= 400) return pods if fails >= thresh else 0 ticks = 45 // 10 # 4 probe ticks during a 45s DB failover bad = sim(20, ticks, 3, lambda: readyz(True, db_ok=False)) good = sim(20, ticks, 3, lambda: livez(True)) print(f"db in liveness: restarts={bad} (ticks={ticks})") print(f"split /livez: restarts={good} readyz={readyz(True, False)}")
db in liveness: restarts=20 (ticks=4) split /livez: restarts=0 readyz=503
Probe state math and Python 3.12 endpoint simulation run on CPython 3.12.10 (Windows x64) and checked against Kubernetes probe documentation. Checked by an editor other than the author.
Walkthrough
Kubernetes treats HTTP status codes from 200 to 399 as success and 400 or above as failure. With periodSeconds: 10 and failureThreshold: 3, three consecutive failures happen within 30 seconds (3 * 10 = 30). During a 45-second database failover, kubelet runs 4 probe checks (45 // 10 = 4), so all 20 pods cross the threshold and get restarted together.
Restarting an API pod cannot fix a database failover. Worse, 20 pods restarting at the same time flood the recovering database with cold connections and cache rebuilds. Because initialDelaySeconds defaults to 0 and no startupProbe exists, livenessProbe kills each restarting pod again after 30 seconds while it still needs 70 seconds to boot. That traps the fleet in CrashLoopBackOff even after PostgreSQL recovers.
Each Kubernetes probe answers a different question:
| Probe | Question it answers | Action on failure |
|---|---|---|
startupProbe | Has the app finished booting? | Holds off liveness and readiness; restarts after startup budget |
livenessProbe | Is this process wedged or deadlocked? | kubelet kills and restarts the container |
readinessProbe | Should this pod receive traffic now? | Removes the Pod IP from Service endpoints; no restart |
Fix the deployment by giving slow boots a 120-second startupProbe (12 * 10 = 120 > 70), keeping /livez free of external calls, and checking readiness on /readyz:
startupProbe:
httpGet: { path: /livez, port: 8080 }
periodSeconds: 10
failureThreshold: 12
livenessProbe:
httpGet: { path: /livez, port: 8080 }
periodSeconds: 10
timeoutSeconds: 2
failureThreshold: 3
readinessProbe:
httpGet: { path: /readyz, port: 8080 }
periodSeconds: 5
timeoutSeconds: 2
failureThreshold: 2Now a 45-second failover causes 0 container restarts. Pods stay warm and resume serving traffic as soon as /readyz returns 200 again.
Common mistakes
- Pointing
livenessProbeandreadinessProbeat the same dependency-checking/healthendpoint. One downstream hiccup turns into a fleet-wide restart storm. - Setting a huge
initialDelaySeconds(like 90 seconds) instead ofstartupProbe. Every healthy rolling deploy then waits 90 seconds even when a pod boots in 15 seconds. - Leaving
timeoutSecondsat the 1-second default while/readyzruns a slow query. Brief GC pauses then fail probes falsely. - Failing
/readyzwhen an optional dependency (like recommendations) is slow. That pulls healthy pods out of rotation for a non-critical feature.
Follow-up questions
- "What happens if every pod fails readinessProbe at once during a database blip?" The Service drops to zero ready endpoints, so callers get fast failures at ingress instead of hanging sockets, and pods avoid a restart storm. For optional dependencies, serve degraded responses instead of failing
/readyz. - "What should a good /livez endpoint actually check?" Check only local process health: can the HTTP server accept requests, is the main event loop responsive, and are critical worker threads not deadlocked? Keep it fast, in-memory, and free of network calls.
- "Why does Kubernetes require successThreshold to be 1 on liveness and startup probes?" Once a container starts or passes a liveness check, it is considered alive immediately. Only
readinessProbesupportssuccessThreshold > 1so a recovering pod must pass several checks before taking live traffic.
What a strong answer covers
Computes the 30-second kill window against the 45-second failover and 70-second boot time. Explains why database checks in livenessProbe trigger fleet-wide restarts and CrashLoopBackOff, then cleanly separates startupProbe, /livez, and /readyz.
Open in interactive practice deck →