patterntypescriptexpressMajor
Health and Readiness Endpoints for Kubernetes
Viewed 0 times
health checkliveness probereadiness probeKubernetes503startup probe
kubernetes
Problem
Without health check endpoints, Kubernetes can't distinguish a starting server from a crashed one, routing traffic to containers that aren't ready or restarts them prematurely.
Solution
Implement separate liveness and readiness endpoints that check different conditions.
// Liveness: is the process alive? (simple — should almost never fail)
app.get('/health/live', (req, res) => {
res.status(200).json({ status: 'ok' });
});
// Readiness: can the process handle traffic? (check dependencies)
app.get('/health/ready', async (req, res) => {
const checks = await Promise.allSettled([
db.query('SELECT 1'), // database
cache.ping(), // redis
queue.isConnected(), // message queue
]);
const failed = checks
.map((c, i) => ({ name: ['db', 'cache', 'queue'][i], result: c }))
.filter(c => c.result.status === 'rejected');
if (failed.length > 0) {
return res.status(503).json({
status: 'not-ready',
failures: failed.map(f => f.name),
});
}
res.status(200).json({ status: 'ready' });
});Why
Liveness probes restart stuck processes. Readiness probes prevent traffic from reaching healthy-but-not-ready pods (e.g., during startup or DB connection loss). They serve different purposes.
Gotchas
- Never check external dependencies in liveness probes — a DB outage would restart all your pods in a loop.
- Readiness check failures stop traffic routing but don't restart the pod.
- Add a startup probe for slow-starting applications to avoid liveness probe false positives during init.
Revisions (0)
No revisions yet.