Using the Vertical Pod Autoscaler
Vertical Pod Autoscaler (VPA) is a tool for automatically configuring resources (CPU and RAM) for Kubernetes containers. To fully explore how it works, use it with the Updater component in Recreate mode. In this mode, VPA:
- Creates resource request recommendations for already existing pods.
- Deletes these pods and recreates them with the recommended values.
-
Create a Kubernetes cluster of the latest version, if not done so already.
-
Install and configure
kubectlif you have not already done so. -
Connect to the cluster using
kubectl. -
Install the VPA add-on, if you have not already done so.
-
(Optional) Install the Kube Prometheus Stack add-on, if you have not already done so. You will be able to use it to view VPA dashboards.
When installing the add-on, edit its code by adding metrics for VPA dashboard configuration to the
kube-state-metricscomponent:
to kube-state-metrics
begin
rbac:extraRules:- apiGroups: ["autoscaling.k8s.io"]resources: ["verticalpodautoscalers"]verbs: ["list", "watch"]customResourceState:enabled: trueconfig:kind: CustomResourceStateMetricsspec:resources:- groupVersionKind:group: autoscaling.k8s.iokind: "VerticalPodAutoscaler"version: "v1"labelsFromPath:verticalpodautoscaler: [metadata, name]namespace: [metadata, namespace]target_api_version: [spec, targetRef, apiVersion]target_kind: [spec, targetRef, kind]target_name: [spec, targetRef, name]metrics:
Labels
- name: "verticalpodautoscaler_labels"help: "VPA container recommendations. Kubernetes labels converted to Prometheus labels"each:type: Infoinfo:labelsFromPath:name: [metadata, name]
Memory Information
- name: "verticalpodautoscaler_status_recommendation_containerrecommendations_target"help: "VPA container recommendations for memory. Target resources the VerticalPodAutoscaler recommends for the container."each:type: Gaugegauge:path: [status, recommendation, containerRecommendations]valueFrom: [target, memory]labelsFromPath:container: [containerName]commonLabels:resource: "memory"unit: "byte"- name: "verticalpodautoscaler_status_recommendation_containerrecommendations_lowerbound"help: "VPA container recommendations for memory. Minimum resources the container can use before the VerticalPodAutoscaler updater evicts it"each:type: Gaugegauge:path: [status, recommendation, containerRecommendations]valueFrom: [lowerBound, memory]labelsFromPath:container: [containerName]commonLabels:resource: "memory"unit: "byte"- name: "verticalpodautoscaler_status_recommendation_containerrecommendations_upperbound"help: "VPA container recommendations for memory. Maximum resources the container can use before the VerticalPodAutoscaler updater evicts it"each:type: Gaugegauge:path: [status, recommendation, containerRecommendations]valueFrom: [upperBound, memory]labelsFromPath:container: [containerName]commonLabels:resource: "memory"unit: "byte"- name: "verticalpodautoscaler_status_recommendation_containerrecommendations_uncappedtarget"help: "VPA container recommendations for memory. Target resources the VerticalPodAutoscaler recommends for the container ignoring bounds"each:type: Gaugegauge:path: [status, recommendation, containerRecommendations]valueFrom: [uncappedTarget, memory]labelsFromPath:container: [containerName]commonLabels:resource: "memory"unit: "byte"
CPU Information
- name: "verticalpodautoscaler_status_recommendation_containerrecommendations_target"help: "VPA container recommendations for cpu. Target resources the VerticalPodAutoscaler recommends for the container."each:type: Gaugegauge:path: [status, recommendation, containerRecommendations]valueFrom: [target, cpu]labelsFromPath:container: [containerName]commonLabels:resource: "cpu"unit: "core"- name: "verticalpodautoscaler_status_recommendation_containerrecommendations_lowerbound"help: "VPA container recommendations for cpu. Minimum resources the container can use before the VerticalPodAutoscaler updater evicts it"each:type: Gaugegauge:path: [status, recommendation, containerRecommendations]valueFrom: [lowerBound, cpu]labelsFromPath:container: [containerName]commonLabels:resource: "cpu"unit: "core"- name: "verticalpodautoscaler_status_recommendation_containerrecommendations_upperbound"help: "VPA container recommendations for cpu. Maximum resources the container can use before the VerticalPodAutoscaler updater evicts it"each:type: Gaugegauge:path: [status, recommendation, containerRecommendations]valueFrom: [upperBound, cpu]labelsFromPath:container: [containerName]commonLabels:resource: "cpu"unit: "core"- name: "verticalpodautoscaler_status_recommendation_containerrecommendations_uncappedtarget"help: "VPA container recommendations for cpu. Target resources the VerticalPodAutoscaler recommends for the container ignoring bounds"each:type: Gaugegauge:path: [status, recommendation, containerRecommendations]valueFrom: [uncappedTarget, cpu]labelsFromPath:container: [containerName]commonLabels:resource: "cpu"unit: "core"selfMonitor:enabled: true
end
</details>## 1. Create a Deployment workload controller {params[anchor=k8s-vertical-pod-autoscaler-create-deployment]}1. Create a manifest file for a `Deployment` type [workload](https://kubernetes.io/docs/concepts/workloads/) controller for the `nginx` application:```consolekubectl apply -f -apiVersion: apps/v1kind: Deploymentmetadata:labels:app: nginxname: nginxspec:replicas: 2selector:matchLabels:app: nginxstrategy: {}template:metadata:labels:app: nginxspec:containers:- image: nginxname: nginxresources:requests:cpu: 50mmemory: 50Milimits:cpu: 100mmemory: 100Mi
Here, the resources block requests 50 mebibytes of RAM and 50m of CPU for the pods and sets a limit of 100 mebibytes of RAM and 100m of CPU.
-
Set the
LoadBalancertype for theDeploymentcontroller:kubectl expose deployment nginx --type=LoadBalancer --port 80 -
Wait a few minutes for the load balancer to start, and run the command to get its external IP address:
kubectl get services nginxFind the
EXTERNAL-IPvalue in the output table:NAME TYPE ... EXTERNAL-IP ...nginx LoadBalancer ... 100.70.146.28 ...
-
Create a manifest file for the VPA add-on with the Updater component in
Recreatemode (updateMode: "Recreate"):kubectl apply -f -apiVersion: autoscaling.k8s.io/v1kind: VerticalPodAutoscalermetadata:name: nginx-vpaspec:targetRef:apiVersion: "apps/v1"kind: Deploymentname: nginxupdatePolicy:updateMode: "Recreate"resourcePolicy:containerPolicies:- containerName: "nginx"minAllowed:cpu: "250m"memory: "100Mi"maxAllowed:cpu: "2000m"memory: "2048Mi" -
Run a load test on the application created earlier using the load balancer IP address:
hey -z 300s -c 1000 http://100.70.146.28
-
View the VPA recommendations using the command:
kubectl describe vpa nginx-vpaIn the command output, in the
Recommendationblock, you will see the VPA recommendations for theDeploymentobject, showing how the pod resources can be changed when they are subsequently created manually or via VPA. Example output:Name: nginx-vpaNamespace: defaultLabels: <none>Annotations: <none>API Version: autoscaling.k8s.io/v1Kind: VerticalPodAutoscalerMetadata:Creation Timestamp: 2025-09-16T12:02:45ZGeneration: 1Resource Version: 10112725UID: 35866c72-60b4-4b7c-87e3-f79de17c4263Spec:Resource Policy:Container Policies:Container Name: nginxMax Allowed:Cpu: 2000mMemory: 2048MiMin Allowed:Cpu: 250mMemory: 100MiTarget Ref:API Version: apps/v1Kind: DeploymentName: nginxUpdate Policy:Update Mode: RecreateStatus:Conditions:Last Transition Time: 2025-09-16T12:03:38ZStatus: TrueType: RecommendationProvidedRecommendation:Container Recommendations:Container Name: nginxLower Bound:Cpu: 250mMemory: 104857600Target:Cpu: 250mMemory: 104857600Uncapped Target:Cpu: 15mMemory: 104857600Upper Bound:Cpu: 250mMemory: 104857600Events:Type Reason Age From Message---- ------ ---- ---- -------Normal EvictedPod 100s vpa-updater VPA Updater evicted Pod nginx-76894897bc-tmc44 to apply resource recommendation.Normal EvictedPod 40s vpa-updater VPA Updater evicted Pod nginx-76894897bc-vfm9n to apply resource recommendation.Here, in the
Recommendationblock:Lower Bound— the minimum recommended resource value at which the pod can be guaranteed to remain operational. The lower bound protects the pod from a resource shortage that could lead to unstable operation or errors.Target— the value that reflects the actual resource consumption.Uncapped Target— the optimal resource value that VPA calculated based on the actual pod consumption. It may match theTargetvalue.Upper Bound— the maximum possible resource value recommended for the pod. The upper bound prevents excessive increases in allocated resources.
When VPA operates with the Updater component in
Recreatemode, the pod will be deleted and subsequently recreated if both of the following conditions are met simultaneously:- The pod consumption is below the
Lower Boundvalue and 10% higher or lower than theTargetvalue. - The pod consumption is above the
Upper Boundvalue and 10% higher or lower than theTargetvalue.
-
Track the pod state in real time by running the command:
kubectl get po --watchIn the command output, you will see the result of VPA operation with
updateMode: "Recreate": VPA changed the resource request recommendations, and as a result, theDeploymentobject deleted the old pod and recreated it with the new parameters. Example output:NAME READY STATUS RESTARTS AGEmetrics-scraper 1/1 Running 0 29dnginx-76894897bc-vfm9n 1/1 Running 0 55snginx-76894897bc-wrbj7 0/1 ContainerCreating 0 7svpa-admission-controller-5f84cf7bf5-9xr8p 1/1 Running 0 15mvpa-recommender-8d8999979-8stct 1/1 Running 0 4d21hvpa-updater-6d48bdf676-p7vbj 1/1 Running 0 15mnginx-76894897bc-wrbj7 1/1 Running 0 12snginx-76894897bc-vfm9n 1/1 Running 0 108snginx-76894897bc-vfm9n 1/1 Terminating 0 108snginx-76894897bc-vfm9n 1/1 Terminating 0 108snginx-76894897bc-mskzl 0/1 Pending 0 0snginx-76894897bc-mskzl 0/1 Pending 0 0snginx-76894897bc-mskzl 0/1 ContainerCreating 0 0snginx-76894897bc-mskzl 0/1 ContainerCreating 0 0snginx-76894897bc-vfm9n 1/1 Terminating 0 109snginx-76894897bc-vfm9n 0/1 Terminating 0 109snginx-76894897bc-vfm9n 0/1 Terminating 0 109snginx-76894897bc-vfm9n 0/1 Terminating 0 109snginx-76894897bc-vfm9n 0/1 Terminating 0 109snginx-76894897bc-mskzl 1/1 Running 0 11s -
View the meta-information that the Admission Controller component adds when recreating a pod deleted by the Updater component. To do this, run the command:
kubectl describe po nginx-76894897bc-mskzlExample output:
Name: nginx-76894897bc-mskzlNamespace: defaultPriority: 0Service Account: defaultNode: k8s-test-129-prometheus-0/192.168.1.27Start Time: Tue, 16 Sep 2025 15:04:54 +0300Labels: app=nginxpod-template-hash=76894897bcAnnotations: cni.projectcalico.org/containerID: 161f4da190ec19f47c835ec3b2078e8c65750468c0eca310cec5f1e47f92a49ecni.projectcalico.org/podIP: 10.100.124.114/32cni.projectcalico.org/podIPs: 10.100.124.114/32vpaObservedContainers: nginxvpaUpdates: Pod resources updated by nginx-vpa: container 0: cpu request, memory request, cpu limit, memory limitStatus: RunningIP: 10.100.124.114 -
(Optional) View the add-on operation results in the Grafana dashboard:
- Connect to the Grafana web interface included in the Kube Prometheus Stack add-on.
- View the dashboards in the Home → Dashboards → General → VPA Recommendations section.
A running cluster consumes computing resources and is charged accordingly. If you created it for testing purposes and no longer need it: