By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Readiness and liveness probes are Kubernetes’ way of answering two critical questions about your containers: 1. Is this container ready to serve traffic? (Readiness Probe) 2. Is this container still alive and functioning correctly? (Liveness Probe)
Why this matters in production:- Without probes, Kubernetes assumes your container is healthy as soon as it starts. If your app crashes or hangs, Kubernetes won’t know—it’ll keep sending traffic to a broken pod, causing cascading failures.- With probes, Kubernetes can: - Stop sending traffic to a pod that’s not ready (e.g., still initializing, waiting for a database connection). - Automatically restart a pod that’s stuck in a deadlock or infinite loop. - Improve uptime by failing fast and recovering automatically.
Real-world scenario:You’re deploying a microservice that depends on a database. The database takes 30 seconds to initialize, but your app starts in 5 seconds. Without a readiness probe, Kubernetes will send traffic to your app before it can connect to the database, causing 500 errors. With a readiness probe, Kubernetes waits until the app signals it’s ready before routing traffic.
httpGet
/health
tcpSocket
exec
cat /tmp/healthy
0
initialDelaySeconds
periodSeconds
10
timeoutSeconds
1
successThreshold
2
3
failureThreshold
Always
OnFailure
Never
200 OK
/healthz
/ready
kubectl
Save this as app.py:
app.py
from flask import Flask import time app = Flask(__name__) # Simulate a slow startup (e.g., waiting for DB) startup_time = time.time() is_ready = False @app.route('/') def home(): return "Hello, Kubernetes!" @app.route('/healthz') def healthz(): # Liveness probe: Always return 200 if the app is running return "OK", 200 @app.route('/ready') def ready(): # Readiness probe: Return 200 only after 10 seconds (simulate DB connection) if time.time() - startup_time > 10: return "Ready", 200 else: return "Not Ready", 503 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
FROM python:3.9-slim WORKDIR /app COPY app.py .RUN pip install flask CMD ["python", "app.py"]
docker build -t yourusername/flask-probes:latest .docker push yourusername/flask-probes:latest
Save this as pod.yaml:
pod.yaml
apiVersion: v1 kind: Pod metadata: name: flask-app spec: containers: - name: flask-app image: yourusername/flask-probes:latest ports: - containerPort: 5000 livenessProbe: httpGet: path: /healthz port: 5000 initialDelaySeconds: 5 periodSeconds: 5 readinessProbe: httpGet: path: /ready port: 5000 initialDelaySeconds: 5 periodSeconds: 5
kubectl apply -f pod.yaml kubectl get pods -w
Expected output:
NAME READY STATUS RESTARTS AGE flask-app 0/1 Running 0 5s flask-app 0/1 Running 0 10s flask-app 1/1 Running 0 15s # Ready after 10s
bash kubectl describe pod flask-app | grep -A 10 "Liveness"
If the probe fails, Kubernetes will restart the pod.
Check readiness probe: bash kubectl describe pod flask-app | grep -A 10 "Readiness"
bash kubectl describe pod flask-app | grep -A 10 "Readiness"
If the probe fails, the pod won’t receive traffic (check with kubectl get endpoints).
kubectl get endpoints
Force a failure (for testing):
500
startupProbe
ping
kubectl logs -p <pod>
promql kube_pod_container_status_last_terminated_reason{reason="Error"} # Liveness failures kube_pod_status_ready{condition="false"} # Readiness failures
failureThreshold: 3
initialDelaySeconds: 30
"A pod is crashing repeatedly. What’s the most likely cause?"
kubectl describe pod
Probe type selection:
"Which probe type should you use for a database connection check?"
Configuration traps:
Challenge:Deploy a pod with a liveness probe that fails after 20 seconds (simulating a crash). Observe how Kubernetes restarts the pod. Then, add a readiness probe that succeeds after 10 seconds and verify traffic is only sent after readiness.
Solution:1. Modify app.py to fail the liveness probe after 20s: python @app.route('/healthz') def healthz(): if time.time() - startup_time > 20: return "Dead", 500 return "OK", 200 2. Deploy the pod with both probes (as in Step 4).3. Watch the pod restart: bash kubectl get pods -w 4. Verify readiness: bash kubectl describe pod flask-app | grep -A 5 "Readiness"
python @app.route('/healthz') def healthz(): if time.time() - startup_time > 20: return "Dead", 500 return "OK", 200
bash kubectl get pods -w
bash kubectl describe pod flask-app | grep -A 5 "Readiness"
Why it works:- The liveness probe fails after 20s, triggering a restart.- The readiness probe succeeds after 10s, allowing traffic.
kubectl describe pod <pod>
kubectl describe pod flask-app
kubectl logs -p flask-app
path: /healthz, port: 5000
port: 3306
command: ["cat", "/tmp/healthy"]
initialDelaySeconds: 10
periodSeconds: 5
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.