Kubernetes cookbook · Intermediate
Right-size requests with VPA recommendations
Requests were guessed at launch; find better values from real usage without risking restarts.
Approach
Install the Vertical Pod Autoscaler and create a VPA in recommendation-only mode (updateMode: "Off"). After a
representative period, read the recommended requests and apply them through your normal deployment process.
Solution
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata: {name: api}
spec:
targetRef: {apiVersion: apps/v1, kind: Deployment, name: api}
updatePolicy: {updateMode: "Off"}
resourcePolicy:
containerPolicies:
- containerName: api
minAllowed: {cpu: 50m, memory: 128Mi}
maxAllowed: {cpu: "2", memory: 4Gi}
kubectl get vpa api -o jsonpath='{.status.recommendation.containerRecommendations[0].target}'
Pitfalls
- Letting VPA update pods automatically while an HPA scales on the same CPU or memory metric; they fight.
- Reading recommendations after a quiet weekend; observe peaks too.
Variations
- Goldilocks to view VPA recommendations for every workload in a dashboard.
- In-place pod resize to change requests without restarting pods where supported.
Further reading: Vertical Pod Autoscaling
Reference: https://kubernetes.io/docs/concepts/workloads/autoscaling/vertical-pod-autoscale/
More Kubernetes recipes
- Autoscale a Deployment on CPU and memory
- 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