Sub topic 17
Autoscaling a Deployment with the HPA
This chapter assumes you can write a Deployment manifest, scale it with kubectl scale, and read ReplicaSet and Pod output. It also assumes the cluster runs the Metrics Server, which supplies CPU and memory figures.
How the HPA relates to manual scaling
Earlier you changed the replica count by hand. A HorizontalPodAutoscaler (HPA) automates that edit. It is a controller that watches a metric, computes a desired replica count, and writes it to the Deployment's spec.replicas. The Deployment then updates its current ReplicaSet, and the ReplicaSet creates or deletes Pods. No new ReplicaSet appears, because only the replica count changes and the Pod template does not.
Creating an HPA
The HPA targets a Deployment by name and compares usage to the Pod's resource requests. The target Pods must therefore declare resources.requests.cpu. Without a request, CPU utilization cannot be calculated.
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: 70Apply it and watch it:
kubectl apply -f hpa.yaml
kubectl get hpa web
kubectl describe hpa webThe controller roughly follows this formula:
desiredReplicas = ceil(currentReplicas × currentMetric / targetMetric)
For example, 3 Pods averaging 140% of their CPU request against a 70% target give ceil(3 × 140 / 70), which is 6 replicas. The result is clamped between minReplicas and maxReplicas. By default, scale-down waits for a stabilization window of five minutes, so brief dips do not remove Pods.
Interaction with the Deployment manifest
If the HPA owns the replica count, do not also set a fixed replicas value in the manifest you re-apply. Each kubectl apply would reset the count to that value, and the HPA would then have to correct it again. Remove spec.replicas from the manifest once the HPA manages it.
HPA scaling also works during a rolling update. The Deployment spreads the new count across the old and new ReplicaSets in proportion, so a scale event during a rollout is safe.
Common errors
| Symptom | Likely cause |
|---|---|
| `TARGETS` shows `<unknown>/70%` | Metrics Server is missing, or the Pods have no CPU request |
| Replicas never exceed `minReplicas` | Usage stays below the target, or the request is set too high |
| Replicas jump back after `kubectl apply` | The manifest still sets `spec.replicas` |
| Replicas stop at a ceiling | `maxReplicas` is reached, or the cluster has no room for more Pods |
Exercise
A Deployment has 4 replicas, and the HPA targets 50% average CPU utilization. The Pods currently average 100% of their CPU request. How many replicas does the HPA request, assuming maxReplicas is 10?
Show answer
ceil(4 × 100 / 50) = 8 replicas. This is below maxReplicas, so the Deployment is scaled to 8 and the existing ReplicaSet gains four Pods.
Exercise
After you add an HPA, kubectl get hpa shows <unknown>/70% in the TARGETS column. Name two things to check.
Show answer
Check that the Metrics Server is running in the cluster, and that the Deployment's containers set resources.requests.cpu. Utilization is a percentage of the request, so it cannot be calculated without one.