HPA YAML
- Docker & Kubernetes course
- Advanced
- Lesson 38/70
- 4 min
Learn the HPA YAML structure.
HPA YAML
An autoscaling/v2 HPA defines the target object with scaleTargetRef, the bounds with minReplicas/maxReplicas, and the list of criteria with metrics:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
Multiple metrics
An HPA can take several metrics; a separate replica count is calculated for each and the largest is chosen. Memory, custom application metrics (requests/second) and external metrics (queue length) can be used too:
metrics:
- type: Resource
resource:
name: cpu
target: {type: Utilization, averageUtilization: 70}
- type: Resource
resource:
name: memory
target: {type: AverageValue, averageValue: 400Mi}Practice Write the HPA YAML
Write an HPA YAML: autoscaling/v2; name api; target apps/v1 Deployment api; minReplicas: 2, maxReplicas: 10; a Utilization target of averageUtilization: 70 for CPU.
Starter code
# buraya yazSample solution
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70Expected output
horizontalpodautoscaler.autoscaling/api created
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When using an HPA, what is recommended for the replicas field in the Deployment YAML?
- Removing it; otherwise each apply overwrites the HPA's value Correct answer
- Writing a high value
- Writing 0
- Writing the same as maxReplicas
Explanation: The HPA now owns the desired replica count.
2 Specify the object to scale.
spec:
____:
apiVersion: apps/v1
kind: Deployment
name: api
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
Explanation: scaleTargetRef is the target object's type and name.
3 Match.
| Term | Meaning |
|---|---|
autoscaling/v2 |
HPA API version |
scaleTargetRef |
Target Deployment |
metrics |
Metric list |
multiple metrics |
The largest result is chosen |