VK Cloud logo

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:

  1. Creates resource request recommendations for already existing pods.
  2. Deletes these pods and recreates them with the recommended values.

Preparation steps

  1. Create a Kubernetes cluster of the latest version, if not done so already.

  2. Install and configure kubectl if you have not already done so.

  3. Connect to the cluster using kubectl.

  4. Install the VPA add-on, if you have not already done so.

  5. (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-metrics component:

to kube-state-metrics

begin

rbac:   extraRules:     - apiGroups: ["autoscaling.k8s.io"]       resources: ["verticalpodautoscalers"]       verbs: ["list", "watch"] customResourceState:   enabled: true   config:     kind: CustomResourceStateMetrics     spec:       resources:         - groupVersionKind:             group: autoscaling.k8s.io             kind: "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: Info                 info:                   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: Gauge                 gauge:                   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: Gauge                 gauge:                   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: Gauge                 gauge:                   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: Gauge                 gauge:                   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: Gauge                 gauge:                   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: Gauge                 gauge:                   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: Gauge                 gauge:                   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: Gauge                 gauge:                   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: nginx  name: nginxspec:  replicas: 2  selector:    matchLabels:      app: nginx strategy: {} template:   metadata:     labels:       app: nginx  spec:    containers:    - image: nginx      name: nginx      resources:        requests:          cpu: 50m          memory: 50Mi        limits:          cpu: 100m          memory: 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.

  1. Set the LoadBalancer type for the Deployment controller:

    kubectl expose deployment nginx --type=LoadBalancer --port 80
  2. Wait a few minutes for the load balancer to start, and run the command to get its external IP address:

    kubectl get services nginx

    Find the EXTERNAL-IP value in the output table:

    NAME     TYPE             ...          EXTERNAL-IP             ...nginx    LoadBalancer     ...          100.70.146.28           ...

2. Create a VPA add-on object with the Updater component in Recreate mode and run testing

  1. Create a manifest file for the VPA add-on with the Updater component in Recreate mode (updateMode: "Recreate"):

    kubectl apply -f -apiVersion: autoscaling.k8s.io/v1kind: VerticalPodAutoscalermetadata: name: nginx-vpaspec: targetRef:   apiVersion: "apps/v1"   kind:       Deployment   name:       nginx updatePolicy:   updateMode: "Recreate" resourcePolicy:   containerPolicies:     - containerName: "nginx"       minAllowed:         cpu: "250m"         memory: "100Mi"       maxAllowed:         cpu: "2000m"         memory: "2048Mi"
  2. 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

3. View the results

  1. View the VPA recommendations using the command:

    kubectl describe vpa nginx-vpa

    In the command output, in the Recommendation block, you will see the VPA recommendations for the Deployment object, 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:45Z  Generation:          1  Resource Version:    10112725  UID:                 35866c72-60b4-4b7c-87e3-f79de17c4263Spec:  Resource Policy:    Container Policies:      Container Name:  nginx      Max Allowed:        Cpu:     2000m        Memory:  2048Mi      Min Allowed:        Cpu:     250m        Memory:  100Mi  Target Ref:    API Version:  apps/v1    Kind:         Deployment    Name:         nginx  Update Policy:    Update Mode:  RecreateStatus:  Conditions:    Last Transition Time:  2025-09-16T12:03:38Z    Status:                True    Type:                  RecommendationProvided Recommendation:   Container Recommendations:     Container Name:  nginx     Lower Bound:       Cpu:     250m       Memory:  104857600     Target:       Cpu:     250m       Memory:  104857600     Uncapped Target:       Cpu:     15m       Memory:  104857600     Upper Bound:       Cpu:     250m       Memory:  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 Recommendation block:

    • 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 the Target value.
    • 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 Recreate mode, the pod will be deleted and subsequently recreated if both of the following conditions are met simultaneously:

    • The pod consumption is below the Lower Bound value and 10% higher or lower than the Target value.
    • The pod consumption is above the Upper Bound value and 10% higher or lower than the Target value.
  2. Track the pod state in real time by running the command:

    kubectl get po --watch

    In the command output, you will see the result of VPA operation with updateMode: "Recreate": VPA changed the resource request recommendations, and as a result, the Deployment object deleted the old pod and recreated it with the new parameters. Example output:

    NAME                                        READY   STATUS              RESTARTS   AGE  metrics-scraper                             1/1     Running             0          29d  nginx-76894897bc-vfm9n                      1/1     Running             0          55s  nginx-76894897bc-wrbj7                      0/1     ContainerCreating   0          7s   vpa-admission-controller-5f84cf7bf5-9xr8p   1/1     Running             0          15m  vpa-recommender-8d8999979-8stct             1/1     Running             0          4d21hvpa-updater-6d48bdf676-p7vbj                1/1     Running             0          15m  nginx-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          0s  nginx-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
  3. 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-mskzl

    Example 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=nginx                  pod-template-hash=76894897bcAnnotations:      cni.projectcalico.org/containerID: 161f4da190ec19f47c835ec3b2078e8c65750468c0eca310cec5f1e47f92a49e                  cni.projectcalico.org/podIP: 10.100.124.114/32                  cni.projectcalico.org/podIPs: 10.100.124.114/32                  vpaObservedContainers: nginx                  vpaUpdates: Pod resources updated by nginx-vpa: container 0: cpu request, memory request, cpu limit, memory limitStatus:           RunningIP:               10.100.124.114
  4. (Optional) View the add-on operation results in the Grafana dashboard:

    1. Connect to the Grafana web interface included in the Kube Prometheus Stack add-on.
    2. View the dashboards in the Home → Dashboards → General → VPA Recommendations section.

Delete unused resources

A running cluster consumes computing resources and is charged accordingly. If you created it for testing purposes and no longer need it:

Was this article helpful?