Kubernetes Readiness Probe Basics
What is a Readiness Probe?
A Kubernetes readiness probe is a critical mechanism for determining whether a container is ready to receive traffic. It helps ensure that only healthy containers are included in service load balancing, preventing potentially faulty containers from receiving client requests.
Core Concepts of Readiness Probes
Readiness probes evaluate the container's ability to process requests by periodically checking the application's health status. Unlike liveness probes, readiness probes specifically focus on determining if a container is prepared to handle incoming traffic.
graph LR
A[Container Startup] --> B{Readiness Probe}
B -->|Successful| C[Ready to Receive Traffic]
B -->|Failed| D[Container Not Serving Requests]
Probe Configuration Types
Probe Type |
Description |
Common Use Cases |
HTTP Probe |
Sends HTTP GET request |
Web applications, REST APIs |
TCP Probe |
Checks TCP socket connection |
Database connections, network services |
Exec Probe |
Runs command inside container |
Custom health check scripts |
Sample Readiness Probe Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-application
spec:
template:
spec:
containers:
- name: app
image: myapp:latest
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
failureThreshold: 3
Practical Implementation Example
In this Ubuntu 22.04 example, we'll demonstrate a simple readiness probe configuration for a Python Flask application:
from flask import Flask, jsonify
import time
app = Flask(__name__)
startup_time = time.time()
@app.route('/health')
def health_check():
## Simulate application warm-up
if time.time() - startup_time < 30:
return jsonify({"status": "initializing"}), 503
return jsonify({"status": "healthy"}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
This example illustrates a readiness probe that prevents traffic until the application has been running for at least 30 seconds, ensuring a complete startup sequence before serving requests.