Automatically adjust pod resource levels with the vertical pod autoscaler

    The VPA helps you to understand the optimal CPU and memory usage for your pods and can automatically maintain pod resources through the pod lifecycle.

    The Vertical Pod Autoscaler Operator (VPA) is implemented as an API resource and a custom resource (CR). The CR determines the actions the Vertical Pod Autoscaler Operator should take with the pods associated with a specific workload object, such as a daemon set, replication controller, and so forth, in a project.

    The VPA automatically computes historic and current CPU and memory usage for the containers in those pods and uses this data to determine optimized resource limits and requests to ensure that these pods are operating efficiently at all times. For example, the VPA reduces resources for pods that are requesting more resources than they are using and increases resources for pods that are not requesting enough.

    The VPA automatically deletes any pods that are out of alignment with its recommendations one at a time, so that your applications can continue to serve requests with no downtime. The workload objects then re-deploy the pods with the original resource limits and requests. The VPA uses a mutating admission webhook to update the pods with optimized resource limits and requests before the pods are admitted to a node. If you do not want the VPA to delete pods, you can view the VPA resource limits and requests and manually update the pods as needed.

    For example, if you have a pod that uses 50% of the CPU but only requests 10%, the VPA determines that the pod is consuming more CPU than requested and deletes the pod. The workload object, such as replica set, restarts the pods and the VPA updates the new pod with its recommended resources.

    For developers, you can use the VPA to help ensure your pods stay up during periods of high demand by scheduling pods onto nodes that have appropriate resources for each pod.

    Administrators can use the VPA to better utilize cluster resources, such as preventing pods from reserving more CPU resources than needed. The VPA monitors the resources that workloads are actually using and adjusts the resource requirements so capacity is available to other workloads. The VPA also maintains the ratios between limits and requests that are specified in initial container configuration.

    If you stop running the VPA or delete a specific VPA CR in your cluster, the resource requests for the pods already modified by the VPA do not change. Any new pods get the resources defined in the workload object, not the previous recommendations made by the VPA.

    You can use the OKD web console to install the Vertical Pod Autoscaler Operator (VPA).

    Prerequisites

    • Ensure that you have downloaded the pull secret from the Red Hat OpenShift Cluster Manager site as shown in Obtaining the installation program in the installation documentation for your platform.

      If you have the pull secret, add the redhat-operators catalog to the OperatorHub custom resource (CR) as shown in Configuring OKD to use Red Hat Operators.

    Procedure

    1. In the OKD web console, click OperatorsOperatorHub.

    2. Choose VerticalPodAutoscaler from the list of available Operators, and click Install.

    3. On the Install Operator page, ensure that the Operator recommended namespace option is selected. This installs the Operator in the mandatory openshift-vertical-pod-autoscaler namespace, which is automatically created if it does not exist.

    4. Click Install.

    5. Verify the installation by listing the VPA Operator components:

      1. Navigate to WorkloadsPods.

      2. Select the openshift-vertical-pod-autoscaler project from the drop-down menu and verify that there are four pods running.

      3. Navigate to WorkloadsDeployments to verify that there are four deployments running.

    6. Optional. Verify the installation in the OKD CLI using the following command:

      The output shows four pods and four deplyoments:

      Example output

      1. NAME READY STATUS RESTARTS AGE
      2. pod/vertical-pod-autoscaler-operator-85b4569c47-2gmhc 1/1 Running 0 3m13s
      3. pod/vpa-admission-plugin-default-67644fc87f-xq7k9 1/1 Running 0 2m56s
      4. pod/vpa-recommender-default-7c54764b59-8gckt 1/1 Running 0 2m56s
      5. pod/vpa-updater-default-7f6cc87858-47vw9 1/1 Running 0 2m56s
      6. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
      7. service/vpa-webhook ClusterIP 172.30.53.206 <none> 443/TCP 2m56s
      8. NAME READY UP-TO-DATE AVAILABLE AGE
      9. deployment.apps/vertical-pod-autoscaler-operator 1/1 1 1 3m13s
      10. deployment.apps/vpa-admission-plugin-default 1/1 1 1 2m56s
      11. deployment.apps/vpa-recommender-default 1/1 1 1 2m56s
      12. deployment.apps/vpa-updater-default 1/1 1 1 2m56s
      13. NAME DESIRED CURRENT READY AGE
      14. replicaset.apps/vertical-pod-autoscaler-operator-85b4569c47 1 1 1 3m13s
      15. replicaset.apps/vpa-admission-plugin-default-67644fc87f 1 1 1 2m56s
      16. replicaset.apps/vpa-recommender-default-7c54764b59 1 1 1 2m56s
      17. replicaset.apps/vpa-updater-default-7f6cc87858 1 1 1 2m56s

    To use the Vertical Pod Autoscaler Operator (VPA), you create a VPA custom resource (CR) for a workload object in your cluster. The VPA learns and applies the optimal CPU and memory resources for the pods associated with that workload object. You can use a VPA with a deployment, stateful set, job, daemon set, replica set, or replication controller workload object. The VPA CR must be in the same project as the pods you want to monitor.

    You use the VPA CR to associate a workload object and specify which mode the VPA operates in:

    • The Auto and Recreate modes automatically apply the VPA CPU and memory recommendations throughout the pod lifetime. The VPA deletes any pods in the project that are out of alignment with its recommendations. When redeployed by the workload object, the VPA updates the new pods with its recommendations.

    • The Off mode only provides recommended resource limits and requests, allowing you to manually apply the recommendations. The off mode does not update pods.

    You can also use the CR to opt-out certain containers from VPA evaluation and updates.

    For example, a pod has the following limits and requests:

    1. resources:
    2. limits:
    3. cpu: 1
    4. memory: 500Mi
    5. requests:
    6. cpu: 500m
    7. memory: 100Mi

    After creating a VPA that is set to auto, the VPA learns the resource usage and deletes the pod. When redeployed, the pod uses the new resource limits and requests:

    1. resources:
    2. limits:
    3. cpu: 50m
    4. memory: 1250Mi
    5. requests:
    6. cpu: 25m
    7. memory: 262144k

    You can view the VPA recommendations using the following command:

    1. $ oc get vpa <vpa-name> --output yaml

    After a few minutes, the output shows the recommendations for CPU and memory requests, similar to the following:

    Example output

    1. ...
    2. status:
    3. ...
    4. recommendation:
    5. containerRecommendations:
    6. - containerName: frontend
    7. lowerBound:
    8. cpu: 25m
    9. memory: 262144k
    10. target:
    11. cpu: 25m
    12. memory: 262144k
    13. uncappedTarget:
    14. cpu: 25m
    15. memory: 262144k
    16. upperBound:
    17. memory: "274357142"
    18. - containerName: backend
    19. lowerBound:
    20. cpu: 12m
    21. memory: 131072k
    22. target:
    23. cpu: 12m
    24. memory: 131072k
    25. uncappedTarget:
    26. cpu: 12m
    27. memory: 131072k
    28. upperBound:
    29. cpu: 476m
    30. memory: "498558823"
    31. ...

    The output shows the recommended resources, target, the minimum recommended resources, lowerBound, the highest recommended resources, upperBound, and the most recent resource recommendations, uncappedTarget.

    The VPA uses the lowerBound and upperBound values to determine if a pod needs to be updated. If a pod has resource requests below the values or above the upperBound values, the VPA terminates and recreates the pod with the target values.

    By default, workload objects must specify a minimum of two replicas in order for the VPA to automatically delete and update their pods. As a result, workload objects that specify fewer than two replicas are not automatically acted upon by the VPA. The VPA does update new pods from these workload objects if the pods are restarted by some process external to the VPA. You can change this cluster-wide minimum value by modifying the minReplicas parameter in the VerticalPodAutoscalerController custom resource (CR).

    For example, if you set minReplicas to 3, the VPA does not delete and update pods for workload objects that specify fewer than three replicas.

    If you set minReplicas to 1, the VPA can delete the only pod for a workload object that specifies only one replica. You should use this setting with one-replica objects only if your workload can tolerate downtime whenever the VPA deletes a pod to adjust its resources. To avoid unwanted downtime with one-replica objects, configure the VPA CRs with the podUpdatePolicy set to Initial, which automatically updates the pod only when it is restarted by some process external to the VPA, or Off, which allows you to update the pod manually at an appropriate time for your application.

    Example VerticalPodAutoscalerController object

    1Specify the minimum number of replicas in a workload object for the VPA to act on. Any objects with replicas fewer than the minimum are not automatically deleted by the VPA.

    To use the VPA to automatically update pods, create a VPA CR for a specific workload object with updateMode set to Auto or Recreate.

    When the pods are created for the workload object, the VPA constantly monitors the containers to analyze their CPU and memory needs. The VPA deletes any pods that do not meet the VPA recommendations for CPU and memory. When redeployed, the pods use the new resource limits and requests based on the VPA recommendations, honoring any pod disruption budget set for your applications. The recommendations are added to the status field of the VPA CR for reference.

    By default, workload objects must specify a minimum of two replicas in order for the VPA to automatically delete their pods. Workload objects that specify fewer replicas than this minimum are not deleted. If you manually delete these pods, when the workload object redeploys the pods, the VPA does update the new pods with its recommendations. You can change this minimum by modifying the VerticalPodAutoscalerController object as shown shown in Changing the VPA minimum value.

    Example VPA CR for the Auto mode

    1. apiVersion: autoscaling.k8s.io/v1
    2. kind: VerticalPodAutoscaler
    3. metadata:
    4. name: vpa-recommender
    5. spec:
    6. targetRef:
    7. apiVersion: "apps/v1"
    8. kind: Deployment (1)
    9. name: frontend (2)
    10. updatePolicy:
    11. updateMode: "Auto" (3)

    There must be operating pods in the project before the VPA can determine recommended resources and apply the recommendations to new pods.

    To use the VPA to apply the recommended resources only when a pod is first deployed, create a VPA CR for a specific workload object with updateMode set to Initial.

    Then, manually delete any pods associated with the workload object that you want to use the VPA recommendations. In the Initial mode, the VPA does not delete pods and does not update the pods as it learns new resource recommendations.

    Example VPA CR for the Initial mode

    1. apiVersion: autoscaling.k8s.io/v1
    2. kind: VerticalPodAutoscaler
    3. metadata:
    4. name: vpa-recommender
    5. spec:
    6. targetRef:
    7. apiVersion: "apps/v1"
    8. kind: Deployment (1)
    9. name: frontend (2)
    10. updatePolicy:
    11. updateMode: "Initial" (3)
    1The type of workload object you want this VPA CR to manage.
    2The name of the workload object you want this VPA CR to manage.
    3Set the mode to Initial. The VPA assigns resources when pods are created and does not change the resources during the lifetime of the pod.

    There must be operating pods in the project before a VPA can determine recommended resources and apply the recommendations to new pods.

    To use the VPA to only determine the recommended CPU and memory values, create a VPA CR for a specific workload object with updateMode set to off.

    When the pods are created for that workload object, the VPA analyzes the CPU and memory needs of the containers and records those recommendations in the status field of the VPA CR. The VPA does not update the pods as it determines new resource recommendations.

    Example VPA CR for the Off mode

    1. apiVersion: autoscaling.k8s.io/v1
    2. kind: VerticalPodAutoscaler
    3. metadata:
    4. name: vpa-recommender
    5. spec:
    6. targetRef:
    7. apiVersion: "apps/v1"
    8. kind: Deployment (1)
    9. name: frontend (2)
    10. updatePolicy:
    11. updateMode: "Off" (3)
    1The type of workload object you want this VPA CR to manage.
    2The name of the workload object you want this VPA CR to manage.
    3Set the mode to Off.

    You can view the recommendations using the following command.

    1. $ oc get vpa <vpa-name> --output yaml

    If your workload object has multiple containers and you do not want the VPA to evaluate and act on all of the containers, create a VPA CR for a specific workload object and add a resourcePolicy to opt-out specific containers.

    When the VPA updates the pods with recommended resources, any containers with a resourcePolicy are not updated and the VPA does not present recommendations for those containers in the pod.

    1. apiVersion: autoscaling.k8s.io/v1
    2. kind: VerticalPodAutoscaler
    3. metadata:
    4. name: vpa-recommender
    5. spec:
    6. targetRef:
    7. apiVersion: "apps/v1"
    8. kind: Deployment (1)
    9. name: frontend (2)
    10. updatePolicy:
    11. updateMode: "Auto" (3)
    12. resourcePolicy: (4)
    13. containerPolicies:
    14. - containerName: my-opt-sidecar
    15. mode: "Off"
    1The type of workload object you want this VPA CR to manage.
    2The name of the workload object you want this VPA CR to manage.
    3Set the mode to Auto, Recreate, or Off. The Recreate mode should be used rarely, only if you need to ensure that the pods are restarted whenever the resource request changes.
    4Specify the containers you want to opt-out and set mode to Off.

    For example, a pod has two containers, the same resource requests and limits:

    After launching a VPA CR with the container set to opt-out, the VPA terminates and recreates the pod with the recommended resources applied only to the frontend container:

    1. ...
    2. spec:
    3. containers:
    4. name: frontend
    5. resources:
    6. limits:
    7. cpu: 50m
    8. memory: 1250Mi
    9. cpu: 25m
    10. memory: 262144k
    11. ...
    12. name: backend
    13. resources:
    14. limits:
    15. cpu: "1"
    16. memory: 500Mi
    17. requests:
    18. cpu: 500m
    19. memory: 100Mi
    20. ...

    You can use the Vertical Pod Autoscaler Operator (VPA) by creating a VPA custom resource (CR). The CR indicates which pods it should analyze and determines the actions the VPA should take with those pods.

    Procedure

    To create a VPA CR for a specific workload object:

    1. Change to the project where the workload object you want to scale is located.

      1. Create a VPA CR YAML file:

        1. apiVersion: autoscaling.k8s.io/v1
        2. kind: VerticalPodAutoscaler
        3. metadata:
        4. name: vpa-recommender
        5. spec:
        6. targetRef:
        7. apiVersion: "apps/v1"
        8. kind: Deployment (1)
        9. name: frontend (2)
        10. updatePolicy:
        11. updateMode: "Auto" (3)
        12. resourcePolicy: (4)
        13. containerPolicies:
        14. - containerName: my-opt-sidecar
        15. mode: "Off"
        1Specify the type of workload object you want this VPA to manage: Deployment, StatefulSet, Job, DaemonSet, ReplicaSet, or ReplicationController.
        2Specify the name of an existing workload object you want this VPA to manage.
        3Specify the VPA mode:
        • auto to automatically apply the recommended resources on pods associated with the controller. The VPA terminates existing pods and creates new pods with the recommended resource limits and requests.

        • recreate to automatically apply the recommended resources on pods associated with the workload object. The VPA terminates existing pods and creates new pods with the recommended resource limits and requests. The recreate mode should be used rarely, only if you need to ensure that the pods are restarted whenever the resource request changes.

        • initial to automatically apply the recommended resources when pods associated with the workload object are created. The VPA does not update the pods as it learns new resource recommendations.

        • off to only generate resource recommendations for the pods associated with the workload object. The VPA does not update the pods as it learns new resource recommendations and does not apply the recommendations to new pods.

        4Optional. Specify the containers you want to opt-out and set the mode to Off.
      2. Create the VPA CR:

        1. $ oc create -f <file-name>.yaml

        After a few moments, the VPA learns the resource usage of the containers in the pods associated with the workload object.

        You can view the VPA recommendations using the following command:

        1. $ oc get vpa <vpa-name> --output yaml

        The output shows the recommendations for CPU and memory requests, similar to the following:

        Example output

        1. ...
        2. status:
        3. ...
        4. recommendation:
        5. containerRecommendations:
        6. - containerName: frontend
        7. lowerBound: (1)
        8. cpu: 25m
        9. memory: 262144k
        10. target: (2)
        11. cpu: 25m
        12. memory: 262144k
        13. uncappedTarget: (3)
        14. cpu: 25m
        15. memory: 262144k
        16. upperBound: (4)
        17. cpu: 262m
        18. memory: "274357142"
        19. - containerName: backend
        20. lowerBound:
        21. cpu: 12m
        22. memory: 131072k
        23. target:
        24. cpu: 12m
        25. memory: 131072k
        26. uncappedTarget:
        27. cpu: 12m
        28. memory: 131072k
        29. upperBound:
        30. cpu: 476m
        31. memory: "498558823"
        32. ...
        1lowerBound is the minimum recommended resource levels.
        2target is the recommended resource levels.
        3upperBound is the highest recommended resource levels.
        4 is the most recent resource recommendations.

    You can remove the Vertical Pod Autoscaler Operator (VPA) from your OKD cluster. After uninstalling, the resource requests for the pods already modified by an existing VPA CR do not change. Any new pods get the resources defined in the workload object, not the previous recommendations made by the Vertical Pod Autoscaler Operator.

    You can remove a specific VPA using the oc delete vpa <vpa-name> command. The same actions apply for resource requests as uninstalling the vertical pod autoscaler.

    Prerequisites

    • The Vertical Pod Autoscaler Operator must be installed.

    Procedure

    1. In the OKD web console, click OperatorsInstalled Operators.

    2. Switch to the openshift-vertical-pod-autoscaler project.

    3. In the dialog box, click Uninstall.