CodeBlanks Logo Icon CodeBlanks

Summary - HPA

Combine YAML, CPU threshold and limits to write an HPA.

Summary: HPA

In this group:

  • An HPA calculates the replica count with ceil(current × value / target) and clips it to the min–max range.
  • kubectl autoscale or autoscaling/v2 YAML: scaleTargetRef, minReplicas, maxReplicas, metrics.
  • The CPU percentage is relative to requests.cpu; without a request, <unknown>.
  • behavior adjusts the scaling speed and the scale-down stabilization window.
  • metrics-server is needed; for custom metrics the Prometheus Adapter or KEDA is used.
kubectl autoscale deploy api --cpu-percent=70 --min=2 --max=10
kubectl get hpa -w

Practice CPU and memory metrics

Write an HPA's metrics section (starting with metrics:): for CPU type: Utilization, averageUtilization: 70; for memory type: AverageValue, averageValue: 400Mi.

Starter code

terminal
# buraya yaz

Sample solution

terminal
metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: AverageValue
        averageValue: 400Mi

Expected output

horizontalpodautoscaler.autoscaling/api configured
NAME   TARGETS                          REPLICAS
api    cpu: 42%/70%, memory: 210Mi/400Mi   2

Questions and answers

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

1 The HPA's TARGETS column shows cpu: <unknown>/70%. What are the two most likely causes?

  • metrics-server is missing or requests.cpu isn't written in the containers Correct answer
  • maxReplicas is too low
  • The image is wrong
  • The namespace is wrong

Explanation: Check metrics-server with kubectl top pods, and the error with describe hpa.

2 Give an average-value target for memory.

Question
        target:
          type: ____
          averageValue: 400Mi
Answer
        target:
          type: AverageValue
          averageValue: 400Mi

Explanation: AverageValue is an absolute value, Utilization a percentage target.

3 Match.

Term Meaning
HPA Horizontal
VPA Vertical
metrics-server Metric source
behavior Speed setting

All lessons in Advanced