Probes in Kubernetes are essential for monitoring the health of containers and ensuring that your application is running as expected. Kubernetes supports three types of probes: liveness, readiness, and startup probes.
Understanding Probes
Types of Probes:
Liveness Probe: Determines if a container is still running. If the liveness probe fails, Kubernetes will restart the container.
Readiness Probe: Indicates if a container is ready to accept traffic. If the readiness probe fails, the container is removed from the service's endpoints.
Startup Probe: Used to check if the application within the container has started. Useful for applications that take a long time to initialize.
Example: Adding Probes to a Deployment
Here is an example of a deployment for a notes app with probes configured:
Deployment YAML with Probes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: notes-app
spec:
replicas: 1
selector:
matchLabels:
app: notes-app
template:
metadata:
labels:
app: notes-app
spec:
containers:
- name: notes-app
image: imkiran13/notes-app
ports:
- containerPort: 8000
livenessProbe:
httpGet:
path: /
port: 8000
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 8000
initialDelaySeconds: 5
periodSeconds: 10
Explanation:
Liveness Probe: Makes an HTTP GET request to
/
on port 8000. If the endpoint fails, Kubernetes restarts the container.Readiness Probe: Checks the
/
endpoint to ensure the container is ready to handle traffic. If this fails, Kubernetes temporarily removes the pod from the service's endpoints.
Apply the Deployment:
kubectl apply -f django-deployment.yml -n nginx
Service Configuration
The notes app is exposed using a ClusterIP service:
apiVersion: v1
kind: Service
metadata:
name: notes-app-service
spec:
selector:
app: notes-app
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: ClusterIP
Apply the Service:
kubectl apply -f django-service.yml -n nginx
This configuration exposes the Django app to the outside world using a ClusterIP service, providing external access.
Then, check the services running in the nginx
namespace:
kubectl get services -n nginx
Testing the Service
To access the notes app via the service, you can use port forwarding:
sudo -E kubectl port-forward service/notes-app-service -n nginx 80:80 --address=0.0.0.0
You can now access the app using the forwarded port on your local machine.
Step 6: Check the probes
kubectl describe pod notes-app-5b8d65d69-xwntk -n nginx
Key Points:
Probes are critical for ensuring the stability and reliability of your application.
Liveness probes help recover from deadlocks.
Readiness probes prevent traffic from being routed to unready containers.
Startup probes are ideal for applications with a long initialization time.
Using probes effectively improves the resilience and performance of your Kubernetes applications.