CodeBlanks Logo Icon CodeBlanks

What is an HPA?

Learn autoscaling with a HorizontalPodAutoscaler.

What is an HPA?

A HorizontalPodAutoscaler (HPA) automatically changes a Deployment's (or StatefulSet's) replica count with load. At intervals (15 s by default) it looks at the pods' metrics and calculates the replica count needed to hit the target:

desired = ceil( current_replicas × current_value / target_value )

# 4 replicas, average CPU 90%, target 60%
ceil(4 × 90 / 60) = 6 replicas

"Horizontal" scaling changes the pod count; "vertical" scaling (VPA) changes the resources per pod.

An HPA by command

A quick HPA is created with kubectl autoscale. kubectl get hpa shows the current and target values side by side:

$ kubectl autoscale deployment api --cpu-percent=70 --min=2 --max=10
horizontalpodautoscaler.autoscaling/api autoscaled
$ kubectl get hpa
NAME   REFERENCE        TARGETS       MINPODS   MAXPODS   REPLICAS   AGE
api    Deployment/api   cpu: 35%/70%  2         10        2          1m

Practice Autoscale

Write two commands: create an HPA for the api deployment with a 70% CPU target, at least 2 and at most 10 replicas; list the HPAs.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl autoscale deployment api --cpu-percent=70 --min=2 --max=10
kubectl get hpa

Expected output

horizontalpodautoscaler.autoscaling/api autoscaled
NAME   REFERENCE        TARGETS        MINPODS   MAXPODS   REPLICAS   AGE
api    Deployment/api   cpu: 35%/70%   2         10        2          30s

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 With 3 replicas, average CPU 20% and a 50% target, how many replicas does the HPA want (assuming minReplicas 1)?

  • 6
  • 2 Correct answer
  • 3
  • 1

Explanation: ceil(3 × 20 / 50) = ceil(1.2) = 2.

2 Create autoscaling for the deployment.

Question
kubectl ____ deployment api --cpu-percent=70 --min=2 --max=10
Answer
kubectl autoscale deployment api --cpu-percent=70 --min=2 --max=10

Explanation: autoscale creates an HPA object.

3 Match.

Term Meaning
HPA Changes the pod count
VPA Changes pod resources
TARGETS 35%/70% Current/target
15 sn Evaluation interval

All lessons in Advanced