Sub topic 10
Right-sizing Pods with the Vertical Pod Autoscaler
This chapter assumes you know how resource requests and limits work, how Metrics Server supplies CPU and memory data, and how to apply manifests with kubectl. The Vertical Pod Autoscaler (VPA) changes the size of each Pod instead of the number of Pods.
What the VPA does
The VPA watches the CPU and memory that containers actually use. It then recommends better values for their resource requests. Depending on its mode, it can also apply those values.
The VPA is not part of core Kubernetes. You install it separately. It adds a custom resource definition (CRD) named VerticalPodAutoscaler and three components:
- The recommender reads usage history and computes suggested requests.
- The updater evicts Pods whose requests differ too much from the recommendation.
- The admission controller sets the new requests when a replacement Pod is created.
Start in recommendation-only mode
The safest way to begin is with updateMode: "Off". The VPA calculates recommendations but never touches your Pods.
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: web
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: web
updatePolicy:
updateMode: "Off"Apply it, let the workload run under realistic traffic for a while, then read the result:
kubectl describe vpa webThe Recommendation section lists values for each container:
- Lower Bound is the minimum the VPA considers safe.
- Target is the value it would set.
- Upper Bound is the value above which extra resources are probably wasted.
- Uncapped Target is the target before any limits from a
resourcePolicyare applied.
Use the Target as a starting point when you edit the requests in your Deployment.
Update modes
| Mode | Behavior |
|---|---|
| `Off` | Only computes recommendations. |
| `Initial` | Sets requests when Pods are created. It never evicts running Pods. |
| `Recreate` | Also evicts running Pods so they restart with new requests. |
| `Auto` | Currently behaves like `Recreate`. It may use in-place resizing in future versions. |
In Recreate and Auto modes, a changed request means a Pod restart, because the traditional approach cannot resize a running Pod. Restarts cause brief capacity loss. Pair these modes with a PodDisruptionBudget and at least two replicas.
Limit what the VPA can choose
A resourcePolicy sets boundaries so a bad recommendation cannot starve or flood a node:
spec:
resourcePolicy:
containerPolicies:
- containerName: web
minAllowed:
cpu: 100m
memory: 64Mi
maxAllowed:
cpu: "1"
memory: 512MiDo not combine VPA and HPA on the same metric
The HPA computes utilization as usage divided by requests. If the VPA changes the requests, the HPA's utilization figure shifts, and the two controllers can fight each other. Avoid running an HPA on CPU or memory together with a VPA that manages the same resource in an automatic mode.
Two safe patterns exist:
- Use the VPA in
Offmode for recommendations, and keep the HPA for scaling. - Let the HPA scale on a custom or external metric, such as requests per second, while the VPA manages CPU and memory.
Common errors
- No recommendations appear. Check that Metrics Server works with
kubectl top pods, and wait several minutes for data to build up. - Pods restart unexpectedly. The VPA is in
RecreateorAutomode and evicted them. Switch toInitialorOffif restarts are not acceptable. - Recommendations look too low. The VPA bases them on observed usage. A quiet period produces small numbers, so observe across peak traffic.
Exercise
A Deployment uses an HPA with a 70% CPU utilization target. You want the VPA to help with sizing, and you do not want the two to conflict. Which update mode should you choose, and why?
Show answer
Use updateMode: "Off". The VPA then only reports recommendations, so it does not change the CPU requests that the HPA uses as the denominator of its utilization calculation. You apply the recommended values yourself in the Deployment, as a deliberate change.