Horizontal Pod Autoscaling

Kubernetes · lesson 12 of 32 · 4 min read

Add and remove pods automatically from CPU, memory or custom metrics, and learn what an HPA cannot do.

Open this lesson in the learning hub

Key points

  • An HPA rewrites replicas on your Deployment to chase a target metric. Use the stable autoscaling/v2 API.
  • 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 unknown and 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 replicas in 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.