Docker & Kubernetes
Scaling with HPA
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.
Exercise 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.
Show example solution
helm upgrade --install magaza ./magaza -n magaza -f values-prod.yaml
kubectl get hpa -n magaza
kubectl top pods -n magaza -l app=api
Test yourself
-
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
Show answer
The replica count briefly drops to 1, then the HPA raises it again — This sudden drop causes an outage under load.
-
Negate the condition.
{{- if ____ .Values.api.autoscaling.enabled }}
replicas: {{ .Values.api.replicas }}
{{- end }}
Show answer
{{- if not .Values.api.autoscaling.enabled }}
replicas: {{ .Values.api.replicas }}
{{- end }}
-
Match.
- 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
Next lesson: Putting it together - production