Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
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:
1apiVersion: v12kind: Pod3metadata:4 name: liveness-exec-pod5spec:6 containers:7 - name: liveness8 image: registry.k8s.io/busybox9 args:10 - /bin/sh11 - -c12 - touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 60013 livenessProbe:14 exec:15 command:16 - cat17 - /tmp/healthy18 initialDelaySeconds: 519 periodSeconds: 5Save this as liveness-exec-pod.yaml and apply it:
1kubectl apply -f liveness-exec-pod.yamlThis 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:
1kubectl get pod liveness-exec-pod --watchYou 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:
1apiVersion: v12kind: Pod3metadata:4 name: http-probe-pod5spec:6 containers:7 - name: liveness8 image: registry.k8s.io/e2e-test-images/agnhost:2.409 args:10 - liveness11 ports:12 - containerPort: 808013 livenessProbe:14 httpGet:15 path: /healthz16 port: 808017 initialDelaySeconds: 518 periodSeconds: 1019 readinessProbe:20 httpGet:21 path: /healthz22 port: 808023 initialDelaySeconds: 524 periodSeconds: 10Save this as http-probe-pod.yaml and apply it:
1kubectl apply -f http-probe-pod.yamlThis 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:
1kubectl describe pod http-probe-podLook 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!