Liveness and Readiness Probes are what separate a basic deployment from a truly robust and reliable application. Do you know the secret to high availability and resilience in Kubernetes? It isn’t just about running containers, but probing what’s happening inside them.
How can the orchestrator tell if your application is running correctly or ready to serve traffic? The answer lies in probes, the essential health check mechanism of Kubernetes.
Liveness probes check if an app is alive (needs a restart if it fails). Readiness probes check if the app is ready to serve traffic (receives and answers requests).
In this article, we’ll dive into the function of each one and see how to configure them in your YAML manifest.
1. Liveness Probe: Your Application’s Sentinel
The Liveness Probe is responsible for determining whether your container is alive. It ensures that if your application enters an unrecoverable failure state (like a deadlock or memory error), Kubernetes can intervene. If the Liveness Probe fails, the kubelet restarts the container.
When to use it?
Use the Liveness Probe to detect internal application failures that do not stop the application but render it unusable. For example, an Apache server that’s running but can’t process requests.
Configuration Example (Liveness with Command)
You can configure Liveness to execute a command inside the container. If the command returns exit code 0, the container is considered healthy. Any other value indicates failure. In this example, we are using a simple Apache web server in its latest version.
YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-html-deployment
labels:
app: app-html
spec:
replicas: 3
selector:
matchLabels:
app: app-html
template:
metadata:
labels:
app: app-html
spec:
containers:
- name: app-html
image: httpd:latest
ports:
- containerPort: 80
# --- PROBING SECTION STARTS HERE ---
livenessProbe:
httpGet:
path:
port: 80
initialDelaySeconds: 5 # Waits 5 seconds before first check
periodSeconds: 10 # Checks it every 10 seconds
failureThreshold: 3 # After three failures, restarts the container
2. Readiness Probe: The Intelligent Traffic Manager
The Readiness Probe is crucial for traffic management. It determines if the container is ready to accept requests. This prevents traffic from being routed to an application that has just started but is still loading data, warming up a cache, or connecting to an external database. If the Readiness Probe fails, Kubernetes removes the Pod from the Service’s endpoints. Traffic is not being sent to it until the probe succeeds again.
When to use it?
Use it to control when a Pod should start receiving traffic. This is especially important during rollouts to ensure new versions only serve requests when they are 100% operational.
Configuration Example (Readiness with HTTP)
The most common method is via HTTP, where Kubernetes makes requests to a specific endpoint. A status code between 200 and 399 is considered a success.
YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-html-deployment
labels:
app: app-html
spec:
replicas: 3
selector:
matchLabels:
app: app-html
template:
metadata:
labels:
app: app-html
spec:
containers:
- name: app-html
image: httpd:latest
ports:
- containerPort: 80
# --- PROBING SECTION STARTS HERE ---
livenessProbe:
httpGet:
path:
port: 80
initialDelaySeconds: 5
periodSeconds: 10
failureThreshold: 3
# --- READINESS PROBE IS ADDED HERE ---
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 1 # Waits 1 second for the first check
periodSeconds: 5 # Checks every 5 seconds
successThreshold: 1 # A single success is enough
# --- PROBES SECTION ENDS HERE ---
The difference between Kubernetes Readiness vs Liveness Probe
The separation between Liveness and Readiness probes often causes initial confusion, but their roles are fundamentally distinct and never interchangeable. The Liveness probe continuously checks whether the application is healthy and functional once it is up and running. In contrast, the Readiness probe manages external client experience and prevents service degradation.
They operate as two complementary mechanisms focused on different aspects of availability. The following table summarizes the core purpose and resulting action of each probe, which are the most important distinctions.
Feature | Liveness Probe (livenessProbe) | Readiness Probe (readinessProbe) |
Primary Goal | Container Health/Aliveness | Application Availability/Serviceability |
Probe Question | “Is this application still running and able to make progress?” (i.e., not deadlocked) | “Is this application ready to handle incoming network traffic?” |
Action on Failure | Restarts the container. The Kubelet kills the container, and the pod’s restartPolicy takes effect (usually restarting the container). | Stops sending traffic. The Pod’s IP address is removed from the associated Kubernetes Service Endpoints. |
Impact on Traffic | None, initially. If it fails and the container is restarted, the pod temporarily loses traffic while restarting. | Immediately halts the flow of new requests to the pod. |
Typical Use Case | Detecting terminal failure or deadlocks (app is running but unresponsive). E.g., a process running out of memory and getting stuck. | Detecting temporary unavailability. E.g., when the application is starting up, loading large files, warming caches, or temporarily losing a required database connection. |
Restart/Removal | Restarts the container to self-heal. | Removes the pod from service rotation to prevent client errors. |
Configuration Path | Defined under spec.containers[].livenessProbe | Defined under spec.containers[].readinessProbe |
Best Practice Endpoint | Check internal state only (e.g., is the web server process running?). Should not depend on external services (like databases). | Check external dependencies and internal readiness (e.g., is the database reachable? Has the cache been populated?). |
It is highly recommended to use both probes for most applications:
- Liveness keeps the container healthy by forcing a restart when it’s critically broken.
- Readiness ensures zero-downtime traffic management by pulling the container out of rotation when it’s temporarily unable to serve requests, preventing end-user errors.
Conclusion
Probes are not a luxury; they are a fundamental requirement for reliably operating workloads in Kubernetes.
- Use the Liveness Probe to restart containers in a deadlock.
- Use the Readiness Probe to control containers ready for traffic.
By combining them intelligently, you ensure that your users always interact with healthy and operational instances, maximizing the stability of your environment.
Need help implementing these practices at scale? Work with DevOps engineers who specialize in building production-grade Kubernetes environments.