Configure Kubernetes Health Checks
What you'll build
A Kubernetes Deployment with properly configured liveness and readiness probes that ensure the application is healthy and ready before receiving traffic.
Goals
- Understand the difference between liveness and readiness probes
- Configure a liveness probe to enable self-healing
- Configure a readiness probe to control traffic routing
- Test probe behavior by simulating failures
Prerequisites
- A Kubernetes cluster with kubectl
- An application with /health and /ready endpoints
- Basic understanding of Kubernetes Deployments
Steps
- 1
Add a readiness probe
Add a readiness probe to your container spec. This probe checks if the application is ready to serve traffic. The Pod will not receive traffic until this probe succeeds.
CodereadinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 10 failureThreshold: 3Expected result
The Pod does not receive traffic until /ready returns 200.
- 2
Add a liveness probe
Add a liveness probe to check if the application is still healthy. If this probe fails, Kubernetes restarts the container.
CodelivenessProbe: httpGet: path: /health port: 8080 initialDelaySeconds: 15 periodSeconds: 20 failureThreshold: 3Expected result
If /health fails 3 times, Kubernetes restarts the container.
- 3
Deploy and verify
Apply the Deployment and watch the Pods. They should start, wait for readiness, then become ready and start receiving traffic.
Codekubectl apply -f deployment.yaml kubectl get pods -w # watch pod statusExpected result
Pods go from Running to Ready after the readiness probe succeeds.
- 4
Test probe failure
Simulate a failure by making the /health endpoint return an error. Watch Kubernetes restart the container after the liveness probe fails.
Code# Simulate failure (e.g., kubectl exec to break the endpoint) # Then watch Kubernetes restart the Pod kubectl get pods -wExpected result
After 3 failed liveness checks, Kubernetes kills and restarts the container.
- 5
Add a health check endpoint
Add a /health endpoint returning 200 OK for orchestration systems (Kubernetes, load balancers) to probe liveness. Health checks are foundational for production deployment.
Code# app.py (Flask example) from flask import Flask, jsonify app = Flask(__name__) @app.route("/health") def health(): return jsonify({"status": "ok"}), 200 @app.route("/ready") def ready(): # Check DB connection etc. return jsonify({"ready": True}), 200Expected result
curl localhost:5000/health returns {"status":"ok"} with a 200 status code.
Try it yourself
What you learned
You configured liveness and readiness probes for self-healing and safe traffic routing. Probes are essential for reliable Kubernetes deployments — they let the cluster know when to restart or stop routing to your application.
Related Resources
Related Glossary
- Health Check
Health check concept
- Liveness Probe
Kubernetes liveness probe
- Readiness Probe
Kubernetes readiness probe
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
- Health Check
Health check concept
- Liveness Probe
Kubernetes liveness probe
- Readiness Probe
Kubernetes readiness probe
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.