Lab 2 - Probes¶
20 min Beginner
Problem¶
The energy-shield service has two problems in production.
Container health issues¶
After running for a short while, the application goes into an unhealthy state and starts answering requests with errors. The process doesn't crash, so the container keeps running and OpenShift never restarts it.
The application has an internal health endpoint that reports whether it's healthy: /healthz on port 8080.
- Add a liveness probe that checks
/healthzon port8080. If the endpoint returns an error, the cluster restarts the container.
Container startup issues¶
New Pods take a few seconds after startup before they can serve requests, and some users hit errors during that window.
- Add a readiness probe that checks
/healthzon port8080, so the Pod only receives traffic when it's ready. - On both probes, set an initial delay of 5 seconds and check every 5 seconds.
Setup¶
Here is the Pod manifest. Add the probes, then create the Pod in the cluster:
apiVersion: v1
kind: Pod
metadata:
name: energy-shield-service
spec:
containers:
- name: energy-shield
image: registry.k8s.io/e2e-test-images/agnhost:2.56
args: ["liveness"] # (1)!
ports:
- containerPort: 8080
- The
livenessmode of this test image simulates our buggy service./healthzreturns200 OKfor about 10 seconds, then returns500forever, while the process keeps running.
Verification¶
Watch the Pod for about a minute:
With working probes you should see:
READYshows0/1for the first few seconds, then1/1once the readiness probe passes.- About 20 seconds in, the app goes unhealthy.
READYdrops back to0/1(readiness probe), and a few seconds later theRESTARTScount goes up (liveness probe). - The cycle repeats. Each restart gives the app a fresh start, and it only gets traffic while it's healthy.
Check the events to see the probes in action:
Look for Liveness probe failed: HTTP probe failed with statuscode: 500 followed by Container energy-shield failed liveness probe, will be restarted.
Think about it
Without the liveness probe, how long would this Pod keep serving errors? Try it: delete the Pod, recreate it without probes, and watch the RESTARTS column.