Sub topic 8
Staying available while scaling: probes and PodDisruptionBudgets
This chapter assumes you can apply manifests with kubectl, know how a Deployment manages Pods, and have read the earlier chapters on replicas and the Horizontal Pod Autoscaler (HPA). Scaling changes how many Pods run. It does not guarantee that the running Pods can serve traffic. Two mechanisms close that gap: probes and PodDisruptionBudgets.
Why scaling can hurt availability
- Scale-up: a new Pod is added to the Service as soon as it is Ready. If Kubernetes counts it Ready too early, requests reach a process that is still starting.
- Scale-down and node removal: Pods are terminated. If too many go at once, or the Pod is killed mid-request, users see errors.
Readiness probes gate traffic
A readiness probe tells Kubernetes when a Pod can receive traffic. Until it succeeds, the Pod is not an endpoint of its Service. A new replica from the HPA therefore gets no requests until it is warm.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
terminationGracePeriodSeconds: 30
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
resources:
requests:
cpu: 200m
memory: 128Mi
limits:
memory: 256Mi
readinessProbe:
httpGet:
path: /
port: 80
periodSeconds: 5
failureThreshold: 3
lifecycle:
preStop:
exec:
command: ["sleep", "5"]The probe types have different jobs:
| Probe | Question it answers | Effect on failure |
|---|---|---|
| Readiness | Can this Pod take requests now? | Removed from Service endpoints |
| Liveness | Is the process stuck? | Container restarted |
| Startup | Has a slow app finished starting? | Holds off the other probes until it passes |
Keep liveness checks cheap and independent of downstream services. A liveness probe that fails under load causes restarts, which reduces capacity exactly when you need more of it.
Graceful shutdown on scale-down
When a Pod is removed, it is marked Terminating, and the endpoint removal races with the SIGTERM sent to the container. The preStop sleep above gives load balancers time to stop sending traffic. The app should then finish in-flight requests within terminationGracePeriodSeconds. After that period, the container is killed.
PodDisruptionBudgets limit voluntary evictions
A PodDisruptionBudget (PDB) sets how many Pods of an application may be unavailable during voluntary disruptions. These include kubectl drain and node removal by the cluster autoscaler.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web
spec:
minAvailable: 2
selector:
matchLabels:
app: webYou can use maxUnavailable instead of minAvailable. It adapts better when the HPA changes the replica count.
kubectl get pdb webThe ALLOWED DISRUPTIONS column shows how many Pods can be evicted right now.
What a PDB does not cover
- The HPA and
kubectl scale: reducing replicas deletes Pods directly and ignores PDBs. Use graceful shutdown to make that safe. - Involuntary failures: a node crash or out-of-memory kill is not budgeted.
- Blocking drains: with 2 replicas and
minAvailable: 2, allowed disruptions is 0. Nodes holding those Pods can never be drained, and the cluster autoscaler cannot remove them.
Rollouts
The Deployment's rolling update also depends on readiness. It only continues once new Pods are Ready, so a correct readiness probe protects releases as well as scaling.
Exercise
A Deployment has 3 replicas and a PodDisruptionBudget with minAvailable: 3. You run kubectl drain on a node that hosts one of the Pods. What happens, and how do you fix it?
Show answer
The eviction is refused because allowed disruptions is 0, so the drain blocks and keeps retrying. Lower minAvailable to 2 (or use maxUnavailable: 1) so one Pod can be evicted while two stay available.