Summary - HPA
- Docker & Kubernetes course
- Advanced
- Lesson 42/70
- 5 min
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 autoscaleorautoscaling/v2YAML:scaleTargetRef,minReplicas,maxReplicas,metrics.- The CPU percentage is relative to
requests.cpu; without a request,<unknown>. behavioradjusts 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 -wPractice 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
# buraya yazSample solution
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: 400MiExpected 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.
target:
type: ____
averageValue: 400Mi
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 |