Health checks in Kubernetes are like regular “wellness tests” for your application. They help Kubernetes know if your app is healthy (working as expected) and ready (ready to take user requests).
There are two main types of health checks:
- Liveness Probe
Checks if your app is still running. If it’s “dead” (e.g., stuck or crashed), Kubernetes restarts the container. - Readiness Probe
Checks if your app is ready to handle traffic. If it’s not ready (e.g., still starting up or loading data), Kubernetes temporarily stops sending traffic to it.
Why Use Health Checks?
Without health checks:
- Your app might crash or hang without anyone noticing.
- Users might get errors because traffic is sent to an unready app.
With health checks:
- Kubernetes ensures your app is reliable and responding properly.
How to Implement Health Checks?
Kubernetes uses probes
to define these health checks. You can add them in your app’s Deployment YAML file.
Here’s an example for a simple web app:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: netflix-container
image: johnojabo1/netflix:latest
ports:
- containerPort: 8080
livenessProbe: # Check if the app is running
httpGet: # Use an HTTP request for the check
path: /healthz # Your app should respond to this endpoint
port: 8080 # The port your app listens on
initialDelaySeconds: 5 # Wait 5 seconds before the first check
periodSeconds: 10 # Check every 10 seconds
readinessProbe: # Check if the app is ready
httpGet:
path: /readyz # Your app should respond to this endpoint
port: 8080
initialDelaySeconds: 10 # Wait 10 seconds before checking readiness
periodSeconds: 5 # Check every 5 seconds
- Deploy the App with Probes:
Apply the YAML file to Kubernetes:
kubectl apply -f deployment.yaml
2. Test It:
Check the status of your app pods:
kubectl get pods
Pods with health checks will automatically restart or stop receiving traffic when unhealthy.
In Simple Words:
- Liveness: “Am I alive?”
- Readiness: “Am I ready to serve users?”
- Kubernetes checks these regularly to keep your app stable and reliable.