Optimizing pod resources with Vertical Pod Autoscaler¶
Imported from Confluence
Content may be outdated. Verify before following any procedures. View original | Last updated: February 2022
Description¶
Vertical Pod Autoscaler (VPA) - automatically adjusts the cpu and memory reservations for your pods to help "right size" your applications. This adjustment can improve cluster resource utilization and free up cpu and memory for other pods.
VPA consists of 3 components:
- Recommender - it monitors the current and past resource consumption and, based on it, provides recommended values containers' cpu and memory requests.
- Updater - it checks which of the managed pods have correct resources set and, if not, kills them so that they can be recreated by their controllers with the updated requests.
- Admission controller - it sets the correct resource requests on new pods (either just created or recreated by their controller due to Updater's activity).
You can see them running in dedicated pods of kube-system namespace:
~/ kubectl get pod | grep vert
vertical-pod-autoscaler-admission-controller-bfb489499-76nmr 1/1 Running 0 27h
vertical-pod-autoscaler-recommender-6d5dbfcf98-xmm4c 1/1 Running 0 27h
vertical-pod-autoscaler-updater-c56c4c8b7-k5hkc 1/1 Running 0 27h
Info
The VPA itself is already deployed to every EKS cluster and is managed by DevOps-Berlin team.
Adding VPA to your project¶
To use described functionality in your project you need to add VerticalPodAutoscaler resource to your helm chart - below is an example.
Note targetRef and resourcePolicy: the former points to a deployment you'd like to be monitored and scaled by the VPA (myDeployment in the above case).
The latter is an actual scaling policy you'd like to apply to your deployment: for every container ('*') scale between 100m and 1000m of cpu, and 50Mi to 500Mi of memory.
apiVersion: "autoscaling.k8s.io/v1"
kind: VerticalPodAutoscaler
metadata:
name: myApp-vpa
spec:
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: myDeployment
resourcePolicy:
containerPolicies:
- containerName: '*'
minAllowed:
cpu: 100m
memory: 50Mi
maxAllowed:
cpu: 1
memory: 500Mi
controlledResources: ["cpu", "memory"]
Info
In terms of validation quite obviously you can tell that VPA does work, when pods are getting restarted with new resource requests/limits setup (basically different from chart's default ones).
But you can also check logs of VPA components to see recommender throwing recommendations, updater killiing irrelevant pods and admission controller updating limits/requests.
Possible values (resources specifications from autoscaling.k8s.io/v1)¶
Please refer to the following specs when compiling your VerticalPodAutoscaler manifest/template file:
| VerticalPodAutoscaler/spec | |
|---|---|
| targetRef | Reference to the controller that manages the set of Pods for the autoscaler to control, for example, a Deployment or a StatefulSet. You can point a VerticalPodAutoscaler at any controller that has a Scale subresource. Typically, the VerticalPodAutoscaler retrieves the Pod set from the controller's ScaleStatus. For some well known controllers, for example DaemonSet, the VerticalPodAutoscaler retrieves the Pod set from the controller's spec. |
| updatePolicy | Specifies whether recommended updates are applied when a Pod is started and whether recommended updates are applied during the life of a Pod. |
| resourcePolicy | Specifies policies for how CPU and memory requests are adjusted for individual containers. The resource policy can be used to set constraints on the recommendations for individual containers. If not specified, the autoscaler computes recommended resources for all containers in the Pod, without additional constraints. |
| VerticalPodAutoscaler/spec/resourcePolicy/containerPolicies | |
|---|---|
| containerName | The name of the container that the policy applies to. If not specified, the policy serves as the default policy. |
| mode | Specifies whether recommended updates are applied to the container when it is started and whether recommended updates are applied during the life of the container. Possible values are "Off" and "Auto". The default is "Auto" if you don't specify a value. |
| minAllowed | Specifies the minimum CPU request and memory request allowed for the container. By default, there is no minimum applied. |
| maxAllowed | Specifies the maximum CPU request and memory request allowed for the container. By default, there is no maximum applied. |
| ControlledResources | Specifies the type of recommendations that will be computed (and possibly applied) by the VerticalPodAutoscaler. If empty, the default of [ResourceCPU, ResourceMemory] is used. |
Related tickets¶
DEVOPSBLN-2230