Kubernetes probes are essential for maintaining healthy applications. Liveness probes restart unhealthy pods, while readiness probes control traffic routing. Here's how to configure them properly.
Understanding Kubernetes Probes
Three Types of Probes
| Probe | Purpose | Failure Action |
|---|---|---|
| Liveness | Is the container alive? | Restart container |
| Readiness | Can it handle traffic? | Remove from service |
| Startup | Has it started? | Block other probes |
When to Use Each
- Liveness: Detect deadlocks, infinite loops, stuck processes
- Readiness: Wait for dependencies, handle graceful degradation
- Startup: Allow slow-starting applications time to initialize
Configuring Liveness Probes
HTTP Liveness Probe
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: app
image: my-app:latest
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 15
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
successThreshold: 1
TCP Liveness Probe
For services without HTTP endpoints:
livenessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 30
periodSeconds: 10
Command Liveness Probe
For custom health checks:
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
Configuring Readiness Probes
HTTP Readiness Probe
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 3
Different Endpoints for Different Probes
spec:
containers:
- name: app
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /healthz # Basic alive check
port: 8080
readinessProbe:
httpGet:
path: /ready # Full dependency check
port: 8080
Startup Probes for Slow Applications
startupProbe:
httpGet:
path: /healthz
port: 8080
failureThreshold: 30
periodSeconds: 10
# Allows up to 5 minutes (30 * 10s) for startup
Implementing Health Endpoints
Liveness Endpoint (Simple)
// Go
func livenessHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
Readiness Endpoint (With Dependency Checks)
func readinessHandler(w http.ResponseWriter, r *http.Request) {
// Check database connection
if err := db.Ping(); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
// Check Redis connection
if err := redis.Ping().Err(); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}
Python Flask Example
from flask import Flask
import psycopg2
app = Flask(__name__)
@app.route('/healthz')
def liveness():
return 'OK', 200
@app.route('/ready')
def readiness():
try:
conn = psycopg2.connect(DATABASE_URL)
conn.close()
return 'Ready', 200
except:
return 'Not Ready', 503
Probe Parameters Explained
| Parameter | Description | Recommended |
|---|---|---|
initialDelaySeconds | Wait before first probe | App startup time |
periodSeconds | Time between probes | 10s |
timeoutSeconds | Max probe duration | 3-5s |
successThreshold | Successes to be healthy | 1 |
failureThreshold | Failures to be unhealthy | 3 |
Common Mistakes to Avoid
1. Too Aggressive Probes
# BAD: Will kill pods during normal load spikes
livenessProbe:
timeoutSeconds: 1
failureThreshold: 1
periodSeconds: 1
2. Checking External Dependencies in Liveness
// BAD: Don't check external services in liveness
func livenessHandler(w http.ResponseWriter, r *http.Request) {
if err := externalAPI.Ping(); err != nil {
w.WriteHeader(503) // This will restart the pod!
return
}
}
3. Same Endpoint for Both Probes
The liveness endpoint should be simpler than readiness.
External Monitoring
Monitor Kubernetes Services
In addition to probes, monitor your exposed services:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
Set up external monitoring for:
- Load balancer endpoint
- Ingress endpoints
- Critical API routes
Probe Configuration Checklist
- Liveness probe configured
- Readiness probe configured
- Startup probe for slow apps
- Appropriate timeouts set
- Failure thresholds tuned
- Health endpoints implemented
- External dependencies not in liveness
- External monitoring configured
- Alerting set up for pod restarts
- Dashboards showing pod health
WizStatus monitors your Kubernetes service endpoints externally. Combined with internal probes, you get complete visibility into your cluster health with instant alerts.