CKA HorizontalPodAutoscaler — CPU target and scale-down stabilization
HPA points at a Deployment. Stabilization lives under behavior.scaleDown.
<!-- hal:authoritative:yaml -->
On the exam, HPA points at a Deployment, names a CPU utilization target, and bounds the replica range. Stabilization lives under behavior.scaleDown.
§I - Frame
k8s_day_counter 22 is even, so CKA-emphasis. Recent Cert days spent CKS upgrade/drain (09-27), CKA StorageClass (09-24), CKS ServiceAccount hardening (09-21), and CKA Gateway (09-18). Tonight is Bootcamp Q5: create HorizontalPodAutoscaler apache-server in namespace autoscale, targeting Deployment apache-deployment, 50% CPU, min 1, max 4, scale-down stabilization 30 seconds.
Ops today colors scale-out with EKS Fargate profiles: every new pod HPA creates must still match a profile selector to land on Fargate. The exam stem does not mention Fargate. Memorize the HPA object.
§II - Objective map
| Need | Mechanism |
|---|---|
| Point at a workload | spec.scaleTargetRef (apps/v1, Deployment, name) |
| CPU target | metrics[].type: Resource with resource.name: cpu and averageUtilization |
| Replica floor/ceiling | minReplicas, maxReplicas |
| Slow the downscale | behavior.scaleDown.stabilizationWindowSeconds |
| API version | Prefer autoscaling/v2 (metrics array + behavior) |
Metrics-server (or an equivalent metrics API) must be present for Resource metrics. On the exam cluster it usually is. Without it the HPA stays unable to compute replicas.
§III - Q5 drill pattern (exam core)
- Confirm the Deployment exists:
kubectl get deploy -n autoscale apache-deployment - Apply the HPA (SolutionNotes shape):
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: apache-server
namespace: autoscale
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: apache-deployment
minReplicas: 1
maxReplicas: 4
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
behavior:
scaleDown:
stabilizationWindowSeconds: 30
- Check:
kubectl get hpa -n autoscale apache-servershows TARGETS and REPLICAS once metrics arrive. - Optional shorthand (may omit behavior):
kubectl autoscale deploy apache-deployment -n autoscale --cpu-percent=50 --min=1 --max=4 --name=apache-serverthen patchbehaviorif the stem requires the 30s window.
Q5 wants the stabilization window. If you use kubectl autoscale, verify the window is present afterward.
§IV - Discriminators
Utilization vs averageValue. averageUtilization: 50 means 50% of each pod's requested CPU. The Deployment must set CPU requests or utilization math is meaningless.
minReplicas default. If you omit minReplicas, the API defaults to 1. Still write it when the stem names it.
behavior is v2. autoscaling/v1 has no behavior field. Use v2 when the stem mentions stabilization, scale-up policies, or select policies.
Scale-down stabilization. During the window, HPA remembers recent desired replica recommendations and picks the highest, which delays flapping down. Thirty seconds is short; production often uses longer. Write what the stem says.
§V - Exam traps
- Writing a v1 HPA and wondering where behavior went. Use
autoscaling/v2. - Putting stabilization under scaleUp. Q5 asks scaleDown.
- Targeting a Pod or ReplicaSet instead of the Deployment. scaleTargetRef must match the stem's kind and name.
- Setting averageUtilization without CPU requests on the pod template. HPA may sit unknown or mis-scale.
- Answering with Gateway, StorageClass, or NetworkPolicy YAML. Wrong domain for Q5.
§VI - Study drill
- Run Bootcamp Q5 LabSetUp if present; apply SolutionNotes cold.
- Run
validate.bash; fix until name, namespace, target, CPU 50, min 1, max 4, and stabilization 30 all pass. - Flash three lines: scaleTargetRef / Resource CPU utilization / behavior.scaleDown window.
- Optional Ops adjacency: say whether a new replica in
batchwith labelapp=etlwould match today's Fargate profile selectors (not on the exam).
Success: Q5 validate exit 0; you can write v2 metrics and behavior without looking up field paths more than once.
§VII - Close instruction
File Q5 as muscle memory. Pair: Ops EKS Fargate coverage census; Dev async kube-rs LabelSelector census. Maghrib owns quiz.html later.
Related
- Bootcamp: CKA Q5 HPA
- Ops: EKS Fargate profiles
- Dev: async kube-rs Fargate census
- Prior CKA: StorageClass (09-24)
- Prior CKA: workloads and scheduling (08-16)