Kubernetes cookbook · Intermediate
Autoscale a Deployment on CPU and memory
Add and remove replicas automatically as load changes, without flapping.
Approach
Set accurate CPU requests, then create an autoscaling/v2 HPA with a utilisation target and behaviour that
scales up quickly and down slowly. Remove replicas from the Deployment you apply.
Solution
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: {name: api}
spec:
scaleTargetRef: {apiVersion: apps/v1, kind: Deployment, name: api}
minReplicas: 3
maxReplicas: 30
metrics:
- type: Resource
resource: {name: cpu, target: {type: Utilization, averageUtilization: 65}}
- type: Resource
resource: {name: memory, target: {type: Utilization, averageUtilization: 80}}
behavior:
scaleUp:
stabilizationWindowSeconds: 0
policies: [{type: Percent, value: 100, periodSeconds: 30}]
scaleDown:
stabilizationWindowSeconds: 300
policies: [{type: Pods, value: 2, periodSeconds: 60}]
Pitfalls
- No CPU requests: utilisation can't be computed.
- Requests far above real usage: the HPA never sees high utilisation.
- Memory-based scaling for apps whose memory never shrinks.
Variations
- KEDA for queue length, request rate or schedule-based scaling.
- Cluster autoscaler or Karpenter so new replicas get nodes.
Further reading: Horizontal Pod Autoscaling
Reference: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
More Kubernetes recipes
- Debug a distroless pod with an ephemeral container
- Default-deny network policies that keep DNS working
- Enforce the restricted Pod Security Standard
- Expose a service over HTTPS with automatic certificates
- Give a CI pipeline least-privilege deploy access
- Handle SIGTERM in your application for Kubernetes
- Manage dev, staging and prod with Kustomize overlays
- Restart pods automatically when configuration changes