Scaling with HPA
- Docker & Kubernetes course
- Advanced
- Lesson 64/70
- 4 min
Write autoscaling with an HPA.
Scaling with an HPA
The API is scaled with an HPA when load rises. While the HPA is on, the Deployment template must not write the replicas field; otherwise every helm upgrade resets the replica count:
# templates/api-deployment.yaml
spec:
{{- if not .Values.api.autoscaling.enabled }}
replicas: {{ .Values.api.replicas }}
{{- end }}
# templates/api-hpa.yaml
{{- if .Values.api.autoscaling.enabled }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api
spec:
scaleTargetRef: {apiVersion: apps/v1, kind: Deployment, name: api}
minReplicas: {{ .Values.api.autoscaling.min }}
maxReplicas: {{ .Values.api.autoscaling.max }}
metrics:
- type: Resource
resource:
name: cpu
target: {type: Utilization, averageUtilization: {{ .Values.api.autoscaling.cpu }}}
{{- end }}Resource requests are required
For the HPA to calculate the CPU percentage, the API container must have requests.cpu. The requests come from the values too:
resources:
{{- toYaml .Values.api.resources | nindent 12 }}
# values.yaml
api:
resources:
requests: {cpu: 200m, memory: 256Mi}
limits: {memory: 512Mi}toYaml and nindent place a block of values into the template with the right indentation.
Practice Upgrade and watch the HPA
Write three commands: upgrade ./magaza as the magaza release in the magaza namespace, installing it if missing, with values-prod.yaml; list the HPAs in magaza; show the resource usage of the pods labelled app=api in magaza.
Starter code
# buraya yazSample solution
helm upgrade --install magaza ./magaza -n magaza -f values-prod.yaml
kubectl get hpa -n magaza
kubectl top pods -n magaza -l app=apiExpected output
Release "magaza" has been upgraded. Happy Helming! NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS api Deployment/api cpu: 41%/70% 2 10 2 NAME CPU(cores) MEMORY(bytes) api-7f8c9d6b5-abcde 82m 201Mi api-7f8c9d6b5-fghij 79m 198Mi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 After the HPA scaled to 6 replicas, what happens on a helm upgrade with a template writing replicas: 1?
- Nothing happens
- The HPA is deleted
- The upgrade fails
- The replica count briefly drops to 1, then the HPA raises it again Correct answer
Explanation: This sudden drop causes an outage under load.
2 Negate the condition.
{{- if ____ .Values.api.autoscaling.enabled }}
replicas: {{ .Values.api.replicas }}
{{- end }}
{{- if not .Values.api.autoscaling.enabled }}
replicas: {{ .Values.api.replicas }}
{{- end }}
Explanation: In Go templates, negation is the not function.
3 Match.
| Term | Meaning |
|---|---|
if not autoscaling |
replicas only when the HPA is off |
toYaml |
Turn a value into YAML |
nindent 12 |
Newline and indent |
requests.cpu |
For the HPA calculation |