Autoscaling: HPA, VPA and nodes

Kubernetes · Advanced · 12 min

Traffic changes; autoscaling keeps latency stable without paying for peak capacity all day.

Three layers of autoscaling work together:

  • HorizontalPodAutoscaler (autoscaling/v2) changes a Deployment's or StatefulSet's replicas to keep a metric near a target. CPU and memory utilisation are measured relative to requests, from metrics-server. Custom or external metrics (queue length, requests per second) come through an adapter such as Prometheus Adapter or KEDA, which can also scale to zero.
  • VerticalPodAutoscaler (an add-on) recommends or applies better requests based on observed usage. Don't let VPA and HPA both act on CPU for the same workload.
  • Node autoscaling: the Cluster Autoscaler or Karpenter adds nodes when pods are Pending for lack of room and removes underused ones.

HPA details that matter:

  • It needs resource requests; without them utilisation can't be computed.
  • minReplicas/maxReplicas bound it.
  • behavior controls scale speed: scale-down has a 5-minute stabilisation window by default to avoid flapping.
  • Remove replicas from your Deployment manifest (or the next apply fights the HPA).

The formula is roughly desired = ceil(current * currentMetric / target). Scaling is reactive and new pods need time to start, so keep some headroom and fast startup.

Example

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: {name: api}
spec:
  scaleTargetRef: {apiVersion: apps/v1, kind: Deployment, name: api}
  minReplicas: 3
  maxReplicas: 30
  metrics:
    - type: Resource
      resource:
        name: cpu
        target: {type: Utilization, averageUtilization: 70}  # % of requests
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies: [{type: Percent, value: 50, periodSeconds: 60}]
# kubectl get hpa api
# NAME  REFERENCE       TARGETS       MINPODS  MAXPODS  REPLICAS
# api   Deployment/api  cpu: 85%/70%  3        30       4

Remember

  • HPA scales replicas on metrics relative to requests
  • KEDA/adapters for queue- or traffic-based scaling
  • Node autoscalers add nodes when pods are Pending

Watch out for

  • Keeping replicas: 3 in the Deployment manifest so every deploy resets what the HPA chose.

Check yourself

  1. 4 pods average 90% CPU utilisation against a 60% target. Roughly how many replicas will the HPA want?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/

Builds on

Unlocks

Nothing builds on this yet.