Sub topic 6
Readiness probes: controlling traffic
This chapter assumes you know Pods, Deployments, and Services, can read YAML, and can use kubectl. It also assumes you know that the kubelet runs probes on each node. Here you will learn what a readiness probe does, what it does not do, and how to configure it.
What a readiness probe decides
A readiness probe answers one question: should this Pod receive traffic right now? The kubelet runs the probe for the whole life of the container, not only at startup. The result sets the Pod's Ready condition.
- If the probe succeeds, the Pod is ready. Its IP address is listed as ready in the EndpointSlices of every matching Service.
- If the probe fails
failureThresholdtimes in a row, the Pod becomes not ready. The endpoint controller marks its address as not ready, and the Service stops routing to it. - If the probe starts passing again, the Pod rejoins the Service.
A failing readiness probe never restarts the container. Restarts are the job of the liveness probe, covered in a later chapter.
Configuring a readiness probe
You set the probe on the container. Any mechanism works: httpGet, tcpSocket, exec, or grpc.
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 2
successThreshold: 1
failureThreshold: 3With these values, the kubelet calls /ready every 5 seconds and waits up to 2 seconds for an answer. Three consecutive failures take the Pod out of rotation, so removal takes about 15 seconds. One success puts it back.
The defaults are periodSeconds: 10, timeoutSeconds: 1, successThreshold: 1, and failureThreshold: 3. A readiness probe may set successThreshold above 1. This makes a recovering Pod prove itself several times before it gets traffic again.
An httpGet probe passes on any status code from 200 to 399. A tcpSocket probe passes if the connection opens. That only proves something is listening, not that the application can do useful work.
What the probe affects
Without a readiness probe, a container counts as ready as soon as it starts. Traffic can then reach an application that is still loading its configuration or warming a cache. With a probe, the Pod is not ready until the first success.
Readiness also affects other parts of the cluster:
- Rolling updates. A Deployment counts a new Pod as available only after it is ready. The rollout waits, so a bad version stalls instead of replacing healthy Pods.
- Endpoint membership. Services list a Pod as a ready endpoint only while its
Readycondition is true. - Temporary overload. An application can report not ready while it is busy or waiting for a dependency. It then sheds traffic without a restart.
Observing readiness
Use these commands to see the effect of a probe.
kubectl get pods -l app=web
kubectl describe pod -l app=web
kubectl get endpointslices -l kubernetes.io/service-name=web -o yamlThe READY column shows how many containers in each Pod are ready. In describe, look for Readiness probe failed events. In the EndpointSlice, each endpoint has a conditions.ready field that mirrors the Pod's state.
Key points
- Readiness removes a Pod from Service routing and never restarts it.
- The probe runs continuously, so a Pod can go in and out of rotation many times.
- Tune
periodSecondsandfailureThresholdto set how fast a Pod is removed and restored. - Keep the check about this Pod's ability to serve, not about every dependency it uses.
Exercise
A Pod's readiness probe uses periodSeconds: 5 and failureThreshold: 3. The application starts returning 500 from /ready. Roughly how long until the Service stops sending it traffic, and does the container restart?
Show answer
About 15 seconds, because three consecutive failures at 5-second intervals are needed. The container does not restart. The Pod becomes not ready and its address is removed from the Service endpoints. It rejoins once the probe succeeds again.