Kubernetes health probes are essential for maintaining the reliability and availability of your applications. In this hands-on guide, we’ll explore how to implement liveness and readiness probes in Kubernetes, using practical examples.
Understanding Kubernetes Health Probes
Health probes in Kubernetes serve three main purposes:
- Liveness Probes: Determine if a container is running. If it fails, Kubernetes restarts the container.
- Readiness Probes: Check if a container is ready to serve traffic. If it fails, Kubernetes removes the pod from service load balancers.
- Startup Probes: Used for legacy applications that might require extra startup time.
Let’s dive into implementing these probes with hands-on examples.
Example 1: Liveness Probe with Command Execution
First, let’s create a pod with a liveness probe that executes a command:
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
spec:
containers:
- name: liveness
image: registry.k8s.io/busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
Save this as liveness-exec-pod.yaml
and apply it:
kubectl apply -f liveness-exec-pod.yaml
This pod creates a file /tmp/healthy
, removes it after 30 seconds, and then sleeps. The liveness probe checks for the existence of this file every 5 seconds, starting 5 seconds after the container starts.
To observe the pod’s behavior:
kubectl get pod liveness-exec-pod --watch
You should see the pod restart after the file is removed and the liveness probe fails.
Example 2: Liveness and Readiness Probes with HTTP Checks
Now, let’s create a pod with both liveness and readiness probes that perform HTTP checks:
apiVersion: v1
kind: Pod
metadata:
name: http-probe-pod
spec:
containers:
- name: liveness
image: registry.k8s.io/e2e-test-images/agnhost:2.40
args:
- liveness
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
Save this as http-probe-pod.yaml
and apply it:
kubectl apply -f http-probe-pod.yaml
This pod runs a container that exposes a /healthz
endpoint on port 8080. Both the liveness and readiness probes check this endpoint every 10 seconds, starting 5 seconds after the container starts.
To check the status of the probes:
kubectl describe pod http-probe-pod
Look for the Liveness
and Readiness
sections in the output to see the probe statuses.
Key Takeaways
-
Improved Reliability: Health probes help Kubernetes automatically recover from application failures.
-
Fine-Grained Control: You can customize probes based on your application’s specific health check requirements.
-
Different Types for Different Needs:
- Use liveness probes to detect and restart stuck processes.
- Use readiness probes to control when a pod is ready to receive traffic.
- Use startup probes for applications with longer initialization times.
-
Multiple Check Methods: Probes can use HTTP requests, TCP socket checks, or command executions, allowing flexibility in how you define “healthy” for your application.
-
Tuning is Important: Adjust
initialDelaySeconds
,periodSeconds
, and other parameters to match your application’s behavior and avoid unnecessary restarts or traffic routing issues.
Conclusion
Health probes are a powerful feature in Kubernetes that enhance the reliability and self-healing capabilities of your applications. By implementing liveness and readiness probes, you ensure that Kubernetes can effectively manage your application’s lifecycle and traffic routing.
As you continue to work with Kubernetes, experiment with different types of probes and settings to find the optimal configuration for your specific applications. Remember, well-configured health probes are key to building robust, highly available systems in Kubernetes.
Happy Kuberneting!