In Kubernetes, liveness and readiness probes are mechanisms to determine the health of your application. The kubelet uses these probes to know when to restart a container (liveness) and when a container is ready to start accepting traffic (readiness).
Probes Overview
-
Liveness Probe: Checks if the application is alive or dead. If the probe fails, Kubernetes will restart the container.
-
Readiness Probe: Checks if the application is ready to accept requests. If the probe fails, Kubernetes will stop sending traffic to the pod until it passes.
Configuring Liveness Probes
You can define a liveness probe in your Pod configuration YAML file.
Example using an HTTP GET request:
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-app-container
image: my-app-image
livenessProbe:
httpGet:
path: /healthz
port: 8080
Example using a command:
livenessProbe:
exec:
command:
- cat
- /app/is_alive
To apply, run:
kubectl apply -f my-app-pod.yaml
Configuring Readiness Probes
Readiness probes are configured similarly to liveness probes.
Example:
readinessProbe:
httpGet:
path: /ready
port: 8080
To apply, run:
kubectl apply -f my-app-pod.yaml
Troubleshooting Probe Failures
- Check the Pod events:
kubectl describe pod my-app
- Check the kubelet logs on the node where the pod is running:
journalctl -u kubelet
Considerations and Best Practices
-
Timing: Be cautious with the
initialDelaySeconds
andtimeoutSeconds
settings to give your application enough time to start. -
Endpoints: Choose meaningful endpoints for HTTP probes. Don’t use heavy endpoints that could slow down your system.
-
Failure Threshold: Set an appropriate failure threshold before the kubelet takes action.
-
Resource Consumption: Probes should be lightweight and not consume significant CPU and memory.
Complete Exercise
Setup
- Create a YAML file named
probe-exercise.yaml
for a Pod with both readiness and liveness probes.
Steps
- Apply the YAML file:
kubectl apply -f probe-exercise.yaml
- Check the Pod status:
kubectl get pod probe-exercise
- Trigger a failure artificially and observe the Pod behavior.
Troubleshooting
- If the Pod is not starting as expected, describe the Pod to see events.
Solution
Check if the Pod restarts when the liveness probe fails and becomes ready when the readiness probe passes.