Liveness restarts crashed services; readiness removes unhealthy pods from load balancer traffic.
Health Checks and Readiness Probes
@app.get("/health")
def liveness():
"""Liveness: is the service running?"""
return {"status": "alive", "service": "user-service"}
@app.get("/ready")
def readiness():
"""Readiness: is service ready for traffic?"""
try:
db.execute("SELECT 1")
redis_client.ping()
return {"status": "ready"}
except Exception as e:
return {"status":"not ready","error":str(e)}, 503
# Kubernetes probes
livenessProbe:
httpGet: {path: /health, port: 8000}
initialDelaySeconds: 10
periodSeconds: 30
failureThreshold: 3
readinessProbe:
httpGet: {path: /ready, port: 8000}
periodSeconds: 10
failureThreshold: 3