Introduction
In Kubernetes, keeping your application healthy and available is very important. Kubernetes provides built-in features called liveness probes and readiness probes to monitor the health of your containers.
A liveness probe checks whether your application is still running properly. If it fails, Kubernetes will restart the container automatically.
A readiness probe checks whether your application is ready to receive traffic. If it fails, Kubernetes will temporarily stop sending requests to that container.
In simple terms:
Liveness Probe = "Is the app alive?"
Readiness Probe = "Is the app ready to serve users?"
These probes are essential for building reliable, scalable, and production-ready applications in Kubernetes.
Detailed Explanation with Examples
Liveness Probe
A liveness probe ensures your application is not stuck or crashed internally.
Example scenario:
Your app is running but stuck in an infinite loop
It is not responding to requests
Kubernetes will detect this using a liveness probe and restart the container.
Readiness Probe
A readiness probe ensures your application is fully ready before handling traffic.
Example scenario:
Your app is starting and loading data
It is not ready yet
Kubernetes will wait until the readiness probe passes before sending traffic.
Types of Probes in Kubernetes
Kubernetes supports three main types of probes:
HTTP Probe
Checks an HTTP endpoint of your application.
TCP Probe
Checks if a specific port is open.
Command (Exec) Probe
Runs a command inside the container to check health.
Step-by-Step Configuration
Step 1: Create a Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app-image
Step 2: Add Liveness Probe
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 10
periodSeconds: 5
Explanation:
initialDelaySeconds: Wait before first check
periodSeconds: How often to check
Step 3: Add Readiness Probe
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
Explanation:
Kubernetes sends traffic only when this probe succeeds
Step 4: Full Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app-image
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 10
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
Join the conversation! Your thoughts help the community grow.