Sub topic 5
Creating a Horizontal Pod Autoscaler
This chapter assumes you can apply manifests with kubectl and that you know Deployments. It also assumes the web Deployment from the earlier chapter, which sets a CPU request of 200m, and a working Metrics Server.
What the HPA does
A Horizontal Pod Autoscaler (HPA) is a controller that changes the replicas field of a scalable resource, such as a Deployment. By default it checks metrics every 15 seconds. It compares the current value with your target and writes a new replica count.
Writing the manifest
Use the autoscaling/v2 API. It supports several metrics and the behavior block, which a later chapter covers.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70The fields work like this:
scaleTargetRefnames the object to scale. It must be in the same namespace as the HPA.minReplicasandmaxReplicasbound the result. The HPA never goes outside this range.type: Utilizationmeans a percentage of the Pods' CPU requests, averaged across all Pods. With a200mrequest and a 70% target, the HPA aims for about140mof CPU per Pod.
Utilization is calculated against requests, not limits. A container with no CPU request has no baseline, so the HPA cannot compute a percentage for it.
You can also create a similar HPA from the command line:
kubectl autoscale deployment web --cpu-percent=70 --min=2 --max=10A manifest is easier to review and keep in version control.
Applying and watching it
kubectl apply -f hpa.yaml
kubectl get hpa web --watchThe TARGETS column shows current/target, for example 35%/70%. REPLICAS shows the current count. Generate load and you will see the percentage rise, then REPLICAS increase.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `TARGETS` shows `<unknown>/70%` | Metrics Server is missing or unhealthy | Check that `kubectl top pods` returns data |
| `<unknown>` and an event about missing request | A container has no CPU request | Add `resources.requests.cpu` to every container in the Pod |
| Replicas reset after each `kubectl apply` | The Deployment manifest still sets `spec.replicas` | Remove `replicas` from the manifest so the HPA owns it |
| Replicas stop below the expected count | `maxReplicas` reached, or new Pods are `Pending` | Raise the maximum, or check node capacity |
The replicas conflict catches many teams. Applying a manifest with replicas: 2 sets the count back to 2, and the HPA then has to scale up again.
To see why an HPA is not acting, read its conditions and events:
kubectl describe hpa webLook at AbleToScale, ScalingActive and ScalingLimited. ScalingActive: False usually points to a metrics problem. ScalingLimited: True means the HPA wanted to go beyond minReplicas or maxReplicas.
Exercise
Your HPA for the web Deployment shows <unknown>/70% in the TARGETS column, and kubectl top pods prints "Metrics API not available". What is the most likely cause, and what do you check first?
Show answer
The Metrics Server is not installed or not working, so the HPA has no CPU data. Check that the Metrics Server Deployment is running (usually in kube-system) and that its APIService is available. If kubectl top pods works but the HPA still shows unknown, check that every container has a CPU request.