Lab 3.4 - Configuring Health Checks
Objectives
By the end of this lab, you will be able to:
- Configure Liveness Probes
- Configure Readiness Probes
- Use Startup Probes
- Observe the behavior of probes
Exercise 1: Liveness Probe
Step 1: Create a Pod with Liveness Probe
Create pod-liveness.yaml:
apiVersion: v1
kind: Pod
metadata:
name: liveness-pod
spec:
containers:
- name: nginx
image: nginx:1.20
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 5
failureThreshold: 3
Step 2: Apply and Observe
kubectl apply -f pod-liveness.yaml
# Observe events
kubectl describe pod liveness-pod
Exercise 2: Readiness Probe
Step 1: Create a Pod with Readiness Probe
Create pod-readiness.yaml:
apiVersion: v1
kind: Pod
metadata:
name: readiness-pod
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.20
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
Step 2: Create a Service
Create service.yaml:
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
ports:
- port: 80
Step 3: Observe
# View Endpoints
kubectl get endpoints web-service
# The Pod appears in Endpoints only when the readiness probe succeeds
Exercise 3: Startup Probe
Step 1: Create a Pod with Startup Probe
Create pod-startup.yaml:
apiVersion: v1
kind: Pod
metadata:
name: startup-pod
spec:
containers:
- name: slow-app
image: busybox
command: ['sh', '-c', 'sleep 30 && echo "Ready" && sleep 3600']
startupProbe:
exec:
command: ['sh', '-c', 'echo "started"']
failureThreshold: 30
periodSeconds: 2
livenessProbe:
exec:
command: ['sh', '-c', 'echo "alive"']
periodSeconds: 10
Step 2: Observe
kubectl apply -f pod-startup.yaml
# Observe startup
kubectl get pod startup-pod -w
# Liveness/readiness probes wait for startup to succeed
Exercise 4: Failing Probe
Step 1: Create a Pod with a failing probe
Create pod-failing.yaml:
apiVersion: v1
kind: Pod
metadata:
name: failing-pod
spec:
containers:
- name: nginx
image: nginx:1.20
livenessProbe:
httpGet:
path: /nonexistent
port: 80
initialDelaySeconds: 5
periodSeconds: 5
failureThreshold: 3
Step 2: Observe the restart
kubectl apply -f pod-failing.yaml
# Observe restarts
kubectl get pod failing-pod -w
# View events
kubectl describe pod failing-pod
Cleanup
kubectl delete pod liveness-pod readiness-pod startup-pod failing-pod
kubectl delete service web-service
Reflection Questions
- What is the difference between liveness and readiness?
- When to use a startup probe?
- What happens if a liveness probe fails?
- Why are readiness probes important for Services?
Next Steps
Quiz Module 3: Validate your knowledge
Module 4: Services and Networking
Lab created on: December 2024