How to Configure Readiness Probe in Kubernetes for Healthy Pods
In Kubernetes, configure a
readinessProbe in your pod's container spec to check if the application is ready to accept traffic. This probe can use HTTP, TCP, or command checks and controls when the pod is marked ready by the kubelet.Syntax
The readinessProbe is defined inside the container spec in a pod manifest. It includes the probe type (httpGet, tcpSocket, or exec) and parameters like initialDelaySeconds, periodSeconds, and timeoutSeconds.
Key parts explained:
- httpGet: Checks an HTTP endpoint.
- tcpSocket: Checks if a TCP port is open.
- exec: Runs a command inside the container.
- initialDelaySeconds: Wait time before starting probes.
- periodSeconds: How often to run the probe.
- timeoutSeconds: Time to wait for probe response.
yaml
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
timeoutSeconds: 2Example
This example shows a pod with a readiness probe that checks the /ready HTTP endpoint on port 80. The probe starts 10 seconds after the container starts and runs every 5 seconds.
yaml
apiVersion: v1
kind: Pod
metadata:
name: readiness-example
spec:
containers:
- name: webserver
image: nginx:1.23
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 10
periodSeconds: 5
timeoutSeconds: 3Output
Pod 'readiness-example' will be marked Ready only after the /ready endpoint returns success.
Common Pitfalls
Common mistakes when configuring readiness probes include:
- Setting
initialDelaySecondstoo low, causing probes to fail before the app is ready. - Using the wrong port or path that the app does not serve.
- Not matching the probe type to the app's readiness check method.
- Confusing readiness probes with liveness probes; readiness controls traffic routing, liveness controls restarts.
yaml
readinessProbe:
httpGet:
path: /wrongpath
port: 8080
initialDelaySeconds: 0
periodSeconds: 5
# Corrected version:
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10Quick Reference
| Field | Description | Example |
|---|---|---|
| httpGet | HTTP GET request to check readiness | path: /healthz, port: 8080 |
| tcpSocket | TCP check on a port | port: 3306 |
| exec | Run command inside container | command: ["cat", "/tmp/ready"] |
| initialDelaySeconds | Wait before first probe | 10 |
| periodSeconds | Time between probes | 5 |
| timeoutSeconds | Probe timeout duration | 3 |
Key Takeaways
Configure readinessProbe in the container spec to control pod readiness.
Use httpGet, tcpSocket, or exec probes depending on your app's readiness check.
Set initialDelaySeconds to avoid premature probe failures.
Readiness probes control traffic routing; they do not restart containers.
Test your probe endpoints or commands to ensure they reflect true readiness.