Kubernetes is a powerful container orchestration platform that automates the deployment, scaling, and management of containerized applications. To ensure that applications run smoothly, Kubernetes provides readiness and liveness probes, which help monitor the health of containers. In this blog, we will explore these probes, how they work, and how to define them in YAML.
What Are Readiness and Liveness Probes?
Readiness Probes
A readiness probe determines when a container is ready to accept traffic. If the probe fails, Kubernetes will not route traffic to that container until it becomes healthy again. This is useful when an application needs some startup time before handling requests.
Liveness Probes
A liveness probe checks whether a container is still running correctly. If the probe fails, Kubernetes will restart the container. This helps recover applications that might have entered an unhealthy state due to deadlocks, memory leaks, or other failures.
How Kubernetes Uses Probes
Kubernetes periodically performs health checks on running containers using the probes defined in the Pod specification. These probes can be implemented in multiple ways:
- HTTP Requests (
httpGet
): Kubernetes sends an HTTP request to a specific endpoint inside the container. - Command Execution (
exec
): A shell command is executed within the container, and the exit status determines success or failure. - TCP Socket (
tcpSocket
): Kubernetes checks if a specified port inside the container is open.
For this blog, we focus on the most common approach: HTTP-based probes (httpGet
).
Understanding YAML Definitions for Probes
Basic Structure
The following YAML snippet shows a Pod definition with both readiness and liveness probes:
apiVersion: v1
kind: Pod
metadata:
name: my-app-pod
spec:
containers:
- name: my-app
image: my-app-image:latest
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 10
periodSeconds: 15
How It Works
httpGet
: Kubernetes sends an HTTP request tohttp://localhost:8080/healthz
inside the container.- Readiness Probe:
- Starts checking after 5 seconds (
initialDelaySeconds
). - Runs every 10 seconds (
periodSeconds
). - If it fails, Kubernetes removes the container from the service’s load balancing until it recovers.
- Starts checking after 5 seconds (
- Liveness Probe:
- Starts checking after 10 seconds.
- Runs every 15 seconds.
- If it fails, Kubernetes restarts the container.
How Kubernetes Determines the Target Container
Kubernetes runs probes inside the container(s) within a Pod.
- In single-container Pods, the probe automatically applies to that one container.
- In multi-container Pods, each container must have its own health check defined.
Example for multiple containers:
containers:
- name: web-app
image: web-app-image:latest
livenessProbe:
httpGet:
path: /healthz
port: 8080
- name: worker
image: worker-image:latest
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
In this case:
- The web-app container is checked via HTTP on port 8080.
- The worker container is checked by executing a shell command (
cat /tmp/healthy
).