Horizontal Pod Autoscaling
Add and remove pods automatically from CPU, memory or custom metrics, and learn what an HPA cannot do.
Open this lesson in the learning hubKey points
- An HPA rewrites
replicason your Deployment to chase a target metric. Use the stableautoscaling/v2API. - A CPU target is a percentage of the request, not of the node. No request means no CPU autoscaling.
- Install metrics-server first, or every HPA reports
unknownand nothing scales. - Scale-up is quick, scale-down waits 5 minutes by default to avoid flapping. Tune it under
behavior. - HPA scales pods, not machines. Adding nodes is the job of Cluster Autoscaler or Karpenter.
- Do not keep a hardcoded
replicasin git alongside an HPA. GitOps and the HPA will fight each other.
Example
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: 70 # 70% of the CPU *request*
behavior:
scaleDown:
stabilizationWindowSeconds: 300
HPA scales pods against their requests. Something else has to scale the nodes.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Kubernetes course, and every lesson in it is listed on the Kubernetes contents page.