Sub topic 14
Combining Mechanisms and Best Practices
This chapter assumes you know how resource requests and limits work, how to write an autoscaling/v2 HorizontalPodAutoscaler (HPA), and what a PodDisruptionBudget (PDB) does. It also assumes you know the roles of the Vertical Pod Autoscaler (VPA) and the cluster autoscaler. Here you will see how they fit together and which settings cause trouble when combined.
How the layers depend on each other
Each mechanism changes a different thing, and each relies on the one below it. The HPA changes the number of Pods. If the new Pods do not fit on existing nodes, they stay Pending, and the cluster autoscaler adds nodes. Every step reads the Pods' resource requests, so wrong requests make every layer decide badly.
| Mechanism | What it changes | Reacts to | Depends on |
|---|---|---|---|
| HPA | Replica count | Utilization or other metrics | Requests, Metrics Server |
| VPA | Pod requests | Observed usage | Metrics and history |
| Cluster autoscaler | Node count | Pending Pods, idle nodes | Requests |
| PDB | Allowed evictions | Voluntary disruptions | Replica count |
Rules for combining them
- Set requests first. CPU utilization targets are a percentage of the request. A request that is too low makes the HPA scale out too early. A request that is too high wastes nodes.
- Do not let the HPA and VPA act on the same metric. If both use CPU or memory, they fight. Run the VPA with
updateMode: "Off"to get recommendations, then apply them yourself. Or let the HPA scale on a custom metric while the VPA manages requests. - Remove
spec.replicasfrom the manifest when an HPA owns the Deployment. Otherwise eachkubectl applyresets the replica count to the file's value. - Keep
minReplicasat 2 or more for services that must stay up, and keep the PDB looser than that minimum. - Cap
maxReplicasat what the cluster can actually hold. Check the cluster autoscaler's maximum node count and your cloud quotas. - Scale up fast and down slowly. Use a
behaviorblock with a longscaleDownstabilization window to avoid flapping. - Use readiness probes. A new Pod receives traffic only after it passes its readiness check. Slow-starting apps need a startup probe too.
The following pair of manifests follows these rules. The PDB allows one Pod to be evicted at a time when there are at least two replicas.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleDown:
stabilizationWindowSeconds: 300
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web
spec:
minAvailable: 2
selector:
matchLabels:
app: webA practical order of work
- Measure real usage and set requests, using VPA recommendations if you have them.
- Add readiness probes and a PDB.
- Add an HPA with a conservative target and a
behaviorblock. - Enable node scaling and confirm that Pending Pods trigger new nodes.
- Load-test the whole chain and watch
kubectl get hpa webandkubectl get nodeswhile it runs.
Exercise
A Deployment has an HPA with minReplicas 2 and a PDB with minAvailable 2. The cluster autoscaler never removes the nodes running these Pods, even when the nodes are mostly idle. Why, and what is the fix?
Show answer
With only 2 replicas, the PDB allows zero voluntary evictions, so the autoscaler cannot drain those nodes. Lower the PDB to minAvailable 1, or raise minReplicas to 3 so that one Pod can be evicted while two stay available.