
Liveness Is Not Readiness: Health Checks That Don't Restart Your Whole Fleet
- كاتب موثّق
محتويات المقال
/health endpoint to its API that runs a quick query against the database, and wires it up as the liveness probe: if the API can't reach its data, it isn't healthy, so restart it. One evening the database fails over to a replica and is unreachable for a few minutes. Every API Pod fails the probe at the same moment, and every one is restarted, then restarted again. When the database is back, the API is still busy starting up. The incident review asks why Kubernetes made a short database blip so much worse.
The common misconception
/health endpoint that checks everything it depends on is the best liveness probe. The deeper the check, the more problems it catches.
What actually happens
Three probes, three different actions
failureThreshold failures in a row, 3 by default, the container is restarted. "determine when a container is ready to accept traffic." When one fails, the docs say the kubelet "continues running the container that failed checks" and sets the Pod's Ready condition to false. A startup probe holds back the other two until a slow app has finished starting.
Restarts don't fix a dependency
Readiness takes a Pod out of rotation
ready: false. The EndpointSlice docs explain that the serving condition "maps to the Pod's Ready condition" and tells proxies whether the endpoint "should be used as a target for Service traffic." The container keeps its state, connections and warm caches, and gets traffic again as soon as the probe passes. The probe docs describe the pattern: "The liveness probe passes when the app itself is healthy, but the readiness probe additionally checks that each required back-end service is available."
Watching both on a cluster
--context and -n are left out for space. The api Deployment runs 2 replicas of busybox:1.37, serving two small CGI scripts with httpd: /cgi-bin/health returns 200 only if wget reaches http://db/ within 1 second, otherwise 503, and /cgi-bin/live always returns 200. db is an nginx:1.29.8-alpine Deployment that we scaled to zero to fake an outage. These are the probe sections of the two versions we applied:
livenessProbe: # one deep check decides restarts httpGet: path: /cgi-bin/health port: 8080 periodSeconds: 5 timeoutSeconds: 2livenessProbe: # shallow: can the process answer? httpGet: path: /cgi-bin/live port: 8080 periodSeconds: 5 timeoutSeconds: 2readinessProbe: # deep: can this Pod serve requests? httpGet: path: /cgi-bin/health port: 8080 periodSeconds: 5 timeoutSeconds: 2kubectl scale deployment db --replicas=0kubectl get pods -l app=api # 2.5 minutes later# NAME READY STATUS RESTARTS AGE# api-76656748dc-j4t48 1/1 Running 3 (17s ago) 2m54s# api-76656748dc-qx4km 1/1 Running 3 (17s ago) 2m53skubectl scale deployment db --replicas=0kubectl get pods -l app=api # 1 minute later# NAME READY STATUS RESTARTS AGE# api-56fdc7bc88-kdwdh 0/1 Running 0 7m8s# api-56fdc7bc88-ml7r5 0/1 Running 0 7m8skubectl get endpointslices -l kubernetes.io/service-name=api# NAME ADDRESSTYPE PORTS ENDPOINTS AGE# api-d82hm IPv4 8080 10.244.0.21,10.244.0.22 11mkubectl get endpointslice api-d82hm -o yaml | grep "ready:"# ready: false# ready: falsekubectl scale deployment db --replicas=1 # both 1/1 within 7 s1/1 ready the whole time, so the Service kept sending them requests between restarts. In the second tab, both Pods went 0/1 with zero restarts. Note that the plain kubectl get endpointslices table still lists both IPs; only the conditions show they're out of rotation. When the database returned, both were back in rotation seven seconds later, never having restarted.
How to apply it in your project
Split the endpoint in two Expose /live, which does no I/O and only proves the process answers, and/ready, which checks initialization and the dependencies a request can't do without. Give each dependency check its own short timeout.Audit your liveness probes Run kubectl describe deploy | grep -E "^Name:|Liveness:|Readiness:"to list each Deployment's probes with their paths and timings. Any liveness endpoint that talks to a database, a cache or another service belongs in a readiness probe instead.Use a startup probe for slow starts If startup is slow, don't stretch the liveness delay. Add a startup probe on the same cheap endpoint with a high failureThreshold, so liveness only starts once the app is up.Treat synchronized restarts as a probe bug If every replica's RESTARTS count climbs together, check the events for "Liveness probe failed". The cause is almost always outside the Pods, and the probe is turning it into an outage.
Key takeaways
Liveness decides restarts; readiness decides traffic. They answer different questions and need different checks. A liveness probe that checks a shared dependency fails on every replica at once and restarts the whole fleet. A failed readiness probe keeps the container running and marks its endpoint not ready until it passes again. Keep liveness shallow, put required dependencies in readiness, and give every check a short timeout. If all replicas fail readiness together, the Service has nothing to route to, so check only what you can't work without.
التعليقات (0)