Autoscale a workload
Scale a Deployment's replicas with a HorizontalPodAutoscaler and read a sizing recommendation from a VerticalPodAutoscaler.
What you need
kubectlsigned in to the cluster.- A Ready node that can pull
nginx:1.27andbusybox:1.36. The node must show its CPU and memory usage inkubectl top nodes. The node agent reports that usage: see the Node agent reference.
1. Create a Deployment and an HPA
This HPA adds or removes pods to keep the average CPU use at 70% of each container’s CPU request:
kubectl apply -f - <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
resources:
requests:
cpu: 100m
memory: 128Mi
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
YAML
kubectl rollout status deployment/web
kubectl get hpa web
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
web Deployment/web cpu: 0%/70% 2 10 2 48s
TARGETS shows cpu: <n>%/70%. With no load, REPLICAS stays at 2. If
kubectl describe hpa web shows ScalingActive=False, its message says why.
See Horizontal Pod Autoscaler.
2. Add load
Put a Service in front of the pods. Then start a pod that runs three loops, and each loop requests the page without a pause:
kubectl expose deployment web --port 80
kubectl run load --image=busybox:1.36 --restart=Never -- \
/bin/sh -c 'for i in 1 2 3; do (while true; do wget -q -O /dev/null http://web; done) & done; wait'
kubectl get hpa web --watch
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
web Deployment/web cpu: 0%/70% 2 10 2 55s
web Deployment/web cpu: 127%/70% 2 10 2 55s
web Deployment/web cpu: 62%/70% 2 10 4 55s
Within a minute, REPLICAS rises above 2. Press Ctrl-C to stop watching, then
stop the load:
kubectl delete pod load
Five minutes after the load stops, REPLICAS returns to 2. The HPA waits that
long before it removes pods, so a short drop in load does not shrink the
Deployment. To change how long it waits, set
spec.behavior.scaleDown.stabilizationWindowSeconds.
3. Read a VPA recommendation
With updateMode: "Off", the VPA records a recommendation and does not change
the pods:
kubectl apply -f - <<'YAML'
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: web-sizing
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: web
updatePolicy:
updateMode: "Off"
YAML
kubectl describe vpa web-sizing
Once the VPA has collected usage samples from the workload, its status contains a recommendation for each container:
Recommendation:
Container Recommendations:
Container Name: web
Lower Bound:
Cpu: 1m
Memory: 9699678
Target:
Cpu: 31m
Memory: 262144000
Uncapped Target:
Cpu: 31m
Memory: 262144000
Upper Bound:
Cpu: 130189m
Memory: 1100915346449
Target is the request that the VPA would set. Memory is in bytes. If the
status shows RecommendationProvided=False, the condition’s message says why.
See
Vertical Pod Autoscaler.
Clean up
kubectl delete vpa web-sizing
kubectl delete hpa web
kubectl delete service web
kubectl delete deployment web
Next
- Set up a node pool: add nodes when replicas need more capacity.
- Scale an idle workload to zero: stop every pod once its Service has had no traffic for a set idle time, and start one on the next connection.