Sub topic 10
Designing liveness and readiness endpoints
This chapter assumes you know Pods, Deployments, and Services, and that you have read the earlier chapters on probe mechanisms, timing parameters, and the readiness, liveness, and startup probes. It covers what your application's health endpoints should check, and what they should leave out.
Two endpoints, two questions
Liveness and readiness answer different questions, so give them separate endpoints.
- Liveness asks: "Is this process stuck in a way that only a restart can fix?"
- Readiness asks: "Can this instance serve a request right now?"
A failed liveness probe makes the kubelet restart the container. A failed readiness probe only removes the Pod from Service endpoints. The restart is the more disruptive action, so the liveness check should fail only for faults that a restart can repair.
| Endpoint | Should check | Should not check |
|---|---|---|
| `/healthz` (liveness) | The process can respond. The main loop or worker threads are not deadlocked. | Databases, caches, or other services. |
| `/ready` (readiness) | Warm-up is done. Required local resources are usable. The instance is not overloaded or draining. | Anything that fails for every replica at once, unless you want all of them out of rotation. |
Keep liveness shallow
A liveness endpoint can be as simple as returning 200 from the request handler. If the handler responds, the HTTP server and its threads are working.
Do not check dependencies in the liveness probe. Suppose the database goes down and every replica's /healthz fails because it queries the database. The kubelet restarts all replicas. Restarting does not bring the database back. It adds load from reconnecting clients and turns a dependency outage into a full application outage.
Make readiness reflect ability to serve
Readiness is the right place for checks that depend on state. Typical signals:
- Caches are loaded and connection pools are established.
- The instance is not shutting down.
- A local queue is not full.
Be careful with shared dependencies. If readiness checks the database and the database fails, every Pod becomes unready, and the Service has no endpoints. Clients get connection errors instead of the application's own error handling. Decide deliberately. For a service that cannot do anything useful without its database, failing readiness may be acceptable. For one that can serve cached or degraded responses, keep the dependency out of the probe and report degraded state through metrics.
Example: separate handlers
This Python server uses only the standard library. It reports ready only after warm-up finishes and stops reporting ready when it receives SIGTERM.
import signal
import threading
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
ready = threading.Event()
shutting_down = threading.Event()
def warm_up():
time.sleep(5) # stand-in for loading caches
ready.set()
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/healthz":
self.send_response(200)
elif self.path == "/ready":
ok = ready.is_set() and not shutting_down.is_set()
self.send_response(200 if ok else 503)
else:
self.send_response(200)
self.end_headers()
def on_sigterm(signum, frame):
shutting_down.set()
signal.signal(signal.SIGTERM, on_sigterm)
threading.Thread(target=warm_up, daemon=True).start()
HTTPServer(("", 8080), Handler).serve_forever()/healthz never looks at ready. A slow warm-up cannot trigger a restart. It only keeps the Pod out of rotation.
Design rules
- Return a success status (200 to 399) when healthy and a failing status such as 503 otherwise.
- Respond fast and avoid heavy work. Probes that run a slow query add load and hit
timeoutSeconds. - Do not require authentication on probe paths, because the kubelet sends no credentials unless you configure headers.
- Bound every internal check with its own timeout, shorter than the probe's
timeoutSeconds.
Exercise
Your /healthz handler queries the database. The database has a 2-minute outage. What happens to your Pods, and how would you change the design?
Show answer
After failureThreshold consecutive failures, the kubelet restarts every container, even though restarting cannot fix the database. Make /healthz check only that the process responds. If the application cannot serve without the database, check it in /ready, so Pods leave rotation without being restarted.