// Module included in the following assemblies:
//
// * nodes/nodes-vertical-autoscaler.adoc

:_mod-docs-content-type: PROCEDURE
[id="nodes-pods-vertical-autoscaler-configuring_{context}"]
= Using the Vertical Pod Autoscaler Operator

[role="_abstract"]
You can use the Vertical Pod Autoscaler Operator (VPA) to help you maintain the optimal CPU and memory usage for your pods by creating a VPA custom resource (CR). The CR indicates the pods to analyze and determines the actions for the VPA to take with those pods.

You can use the VPA to scale built-in resources such as deployments or stateful sets, and custom resources that manage pods. For more information, see "About using the Vertical Pod Autoscaler Operator".

The following procedure creates a VPA CR for a specific workload object.

.Prerequisites

* Ensure the workload object that you want to autoscale exists.

* Ensure that if you want to use an alternative recommender, a deployment including that recommender exists.

.Procedure

. Change to the location of the project for the workload object you want to scale.

.. Create a VPA CR YAML file:
+
[source,yaml]
----
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-recommender
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind:       Deployment
    name:       frontend
  updatePolicy:
    updateMode: "InPlaceOrRecreate"
  resourcePolicy:
    containerPolicies:
    - containerName: my-opt-sidecar
      mode: "Off"
  recommenders:
    - name: my-recommender
----
+
where:

`spec.targetRef.kind`:: Specifies the type of workload object you want this VPA to manage: `Deployment`, `StatefulSet`, `Job`, `DaemonSet`, `ReplicaSet`, or `ReplicationController`.
`spec.targetRef.name`:: Specifies the name of an existing workload object you want this VPA to manage.
`spec.updatePolicy.updateMode`:: Specifies the VPA mode:
* `InPlaceOrRecreate` to automatically apply the recommended resources on pods associated with the workload object. The VPA attempts to update the workload object with the new resources without re-creating the pod. If the VPA is unable to update the object in place, the VPA re-creates it.
* `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. Use the `Recreate` mode only if you need to ensure that the pods restart whenever the resource request changes.
* `Initial` to automatically apply the recommended resources to newly-created pods associated with the workload object. 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.
`spec.resourcePolicy`:: Specifies the containers you want to opt-out and set the mode to `Off`. This parameter is optional.
`spec.recommenders`:: Specifies an alternative recommender. This parameter is optional.

.. Create the VPA CR:
+
[source,terminal]
----
$ 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 by using the following command:
+
[source,terminal]
----
$ oc get vpa <vpa-name> --output yaml
----
+
The output shows the recommendations for CPU and memory requests, similar to the following:
+
.Example output
[source,yaml]
----
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-recommender
# ...
status:
# ...
  recommendation:
    containerRecommendations:
    - containerName: frontend
      lowerBound:
        cpu: 25m
        memory: 262144k
      target:
        cpu: 25m
        memory: 262144k
      uncappedTarget:
        cpu: 25m
        memory: 262144k
      upperBound:
        cpu: 262m
        memory: "274357142"
    - containerName: backend
      lowerBound:
        cpu: 12m
        memory: 131072k
      target:
        cpu: 12m
        memory: 131072k
      uncappedTarget:
        cpu: 12m
        memory: 131072k
      upperBound:
        cpu: 476m
        memory: "498558823"

...
----
+
where:

`status.recommendation.containerRecommendations`:: Specifies the minimum and maximum recommended resource levels for the container:
* `containerName` is the container for the recommended resource levels.
* `lowerBound` is the minimum recommended resource levels.
* `target` is the recommended resource levels.
* `upperBound` is the highest recommended resource levels.
* `uncappedTarget` is the most recent resource recommendations.
