Mastering Kubernetes Probes: Liveness, Readiness, and Startup Explained
Kubernetes probes exist to ensure your applications run smoothly in production. They help detect when applications become unhealthy and can automatically take corrective actions. Liveness probes are particularly important; they monitor long-running applications that might transition to a broken state, requiring a restart. Readiness probes, on the other hand, check if a container is ready to accept traffic, preventing requests from being sent to containers that aren't fully initialized. Startup probes are also available to check if an application has started successfully, which is useful for applications with long initialization times.
The kubelet performs health checks based on the defined probes. For liveness probes, if the command or HTTP request succeeds, the container is considered alive. If it fails, the kubelet kills and restarts the container. For instance, you can configure a liveness probe to execute a command like cat /tmp/healthy every 5 seconds after an initial delay of 5 seconds. If this command returns a non-zero value, the kubelet will restart the container. Key parameters include initialDelaySeconds, which determines how long to wait after the container starts before initiating the probe, and periodSeconds, which defines how often to perform the probe.
In production, understanding the nuances of these probes is vital. For example, in Kubernetes v1.13 and later, local HTTP proxy settings do not affect HTTP liveness probes, which can lead to unexpected behavior if you're relying on those settings. Always test your configurations to ensure they behave as expected under load and during failure scenarios.
Key takeaways
- →Configure liveness probes to automatically restart containers in broken states.
- →Use readiness probes to prevent traffic from reaching uninitialized containers.
- →Set `initialDelaySeconds` to manage when probes start checking container health.
- →Monitor the behavior of probes in production to catch unexpected issues.
- →Be aware that local HTTP proxy settings do not affect HTTP liveness probes in Kubernetes v1.13 and later.
Why it matters
Using liveness and readiness probes effectively can significantly reduce downtime and improve the reliability of your applications in production. They automate recovery processes, allowing your team to focus on other critical tasks.
Code examples
apiVersion:v1kind:Podmetadata:labels:test:livenessname:liveness-execspec:containers:-name:livenessimage:registry.k8s.io/busybox:1.27.2args:- /bin/sh- -c- touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600livenessProbe:exec:command:- cat- /tmp/healthyinitialDelaySeconds:5periodSeconds:5apiVersion:v1kind:Podmetadata:labels:test:livenessname:liveness-httpspec:containers:-name:livenessimage:registry.k8s.io/e2e-test-images/agnhost:2.40args:- livenesslivenessProbe:httpGet:path:/healthzport:8080httpHeaders:-name:Custom-Headervalue:AwesomeinitialDelaySeconds:3periodSeconds:3apiVersion:v1kind:Podmetadata:name:goproxylabels:app:goproxyspec:containers:-name:goproxyimage:registry.k8s.io/goproxy:0.1ports:-containerPort:8080readinessProbe:tcpSocket:port:8080initialDelaySeconds:15periodSeconds:10livenessProbe:tcpSocket:port:8080initialDelaySeconds:15periodSeconds:10When NOT to use this
The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.
Want the complete reference?
Read official docsMastering Kubernetes CronJobs: Scheduling One-Time Jobs with Precision
Kubernetes CronJobs are essential for automating one-time jobs on a repeating schedule. With the right configuration, you can ensure your tasks run smoothly and on time. Learn how to leverage the .spec.schedule and .spec.jobTemplate fields effectively.
Mastering Kubernetes Jobs: The Key to One-Off Task Management
Kubernetes Jobs are your go-to solution for executing one-off tasks that need to run to completion. With the ability to specify parallelism and manage retries, they offer a robust framework for handling transient workloads effectively.
Mastering DaemonSets: Ensuring Node-Local Facilities in Kubernetes
DaemonSets are crucial for deploying node-local services across your Kubernetes cluster. They ensure that every eligible node runs a specific Pod, which is essential for monitoring, logging, and other local tasks. Understanding how to configure them effectively can save you from unexpected scheduling issues.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.