Browse topics

Horizontal Pod autoscaling

Version
1.37
Since
1.23
Review
Draft

Summary#

A HorizontalPodAutoscaler (HPA) changes a workload's replica count between minReplicas and maxReplicas to approach a metric target. CPU utilization is measured against the containers' CPU requests. Resource metrics come from the metrics.k8s.io API, usually provided by Metrics Server.

Syntax#

sh
kubectl autoscale deployment NAME --min=2 --max=5 --cpu=60%
kubectl describe hpa NAME
yaml
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  minReplicas: 2
  maxReplicas: 5
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 60

Example#

This example needs a working resource metrics API. It starts at the minimum of one replica, then makes that Pod generate CPU load and waits for the HPA to scale it to two.

sh
(
  set -eu
  ns="ref-hpa-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT

  kubectl apply -n "$ns" -f - <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      terminationGracePeriodSeconds: 1
      containers:
        - name: app
          image: busybox:1.37
          command:
            - sh
            - -c
            - |
              while [ ! -f /tmp/load ]; do sleep 1; done
              while :; do :; done
          resources:
            requests:
              cpu: 20m
            limits:
              cpu: 100m
EOF
  kubectl rollout status -n "$ns" deployment/web --timeout=120s >/dev/null
  kubectl autoscale -n "$ns" deployment web --min=1 --max=2 --cpu=60%
  kubectl wait -n "$ns" --for=condition=ScalingActive hpa/web --timeout=180s >/dev/null
  kubectl get -n "$ns" deployment web -o jsonpath='replicas={.status.readyReplicas}{"\n"}'

  kubectl exec -n "$ns" deployment/web -- touch /tmp/load
  kubectl wait -n "$ns" --for=jsonpath='{.spec.replicas}'=2 deployment/web --timeout=180s >/dev/null
  kubectl rollout status -n "$ns" deployment/web --timeout=120s >/dev/null
  kubectl get -n "$ns" deployment web -o jsonpath='replicas={.status.readyReplicas}{"\n"}'
)

Expected output#

Text
deployment.apps/web created
horizontalpodautoscaler.autoscaling/web autoscaled
replicas=1
replicas=2

Common mistakes#

A FailedGetResourceMetric event means that the autoscaler could not read a resource metric, for example because the cluster has no metrics API or a container has no CPU request.

Leaving spec.replicas in a Deployment manifest lets each apply override the autoscaler's chosen count. Remove it using the documented migration steps, because dropping it carelessly can briefly reset the Deployment to one replica.

For a Pod-wide CPU utilization target, set a CPU request on every container. If utilization is unknown, inspect kubectl describe hpa NAME and check that kubectl top pods returns metrics for the workload.

Version applicability#

The autoscaling/v2 API is available since Kubernetes 1.23. The example uses kubectl 1.37. In 1.37, per-direction tolerance and metrics.k8s.io/v1 are stable. Scaling to zero is beta and enabled by default in 1.37, only for object or external metrics.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0