// Module included in the following assemblies:
//
// * nodes/nodes-pods-autoscaling.adoc

:_mod-docs-content-type: PROCEDURE
[id="nodes-pods-autoscaling-creating-memory-specific_{context}"]

= Creating a horizontal pod autoscaler object for specific memory use

[role="_abstract"]
You can use the {product-title} CLI to create a horizontal pod autoscaler (HPA) that automatically scales an existing object. The HPA scales the pods associated with that object to maintain the average memory use that you specify.

[NOTE]
====
Use a `Deployment` object or `ReplicaSet` object unless you need a specific feature or behavior provided by other objects.
====

You can specify the minimum and maximum number of pods and the average memory use that your pods should target. If you do not specify a minimum, the pods are given default values from the {product-title} server.

.Prerequisites

include::snippets/nodes-pods-autoscaling-creating-cpu-prereqs.adoc[]

.Procedure

. Create a `HorizontalPodAutoscaler` object similar to the following for an existing object:
+
[source,yaml,options="nowrap"]
----
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-resource-metrics-memory
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: example
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: memory
      target:
        type: AverageValue
        averageValue: 500Mi
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Pods
        value: 4
        periodSeconds: 60
      - type: Percent
        value: 10
        periodSeconds: 60
      selectPolicy: Max
----
+
where:
+
--
`apiVersion`:: Specifies the `autoscaling/v2` API.
`metadata.name`:: Specifies a name for this horizontal pod autoscaler object.
`spec.scaleTargetRef.apiVersion`:: Specifies the API version of the object to scale:
* For a `Deployment`, `ReplicaSet`, `Statefulset` object, use `apps/v1`.
* For a `ReplicationController`, use `v1`.
* For a `DeploymentConfig`, use `apps.openshift.io/v1`.
`spec.scaleTargetRef.kind`:: Specifies the type of object. The object must be a `Deployment`, `DeploymentConfig`/`dc`, `ReplicaSet`/`rs`, `ReplicationController`/`rc`, or `StatefulSet`.
`spec.scaleTargetRef.name`:: Specifies the name of the object to scale. The object must exist.
`spec.minReplicas`:: Specifies the minimum number of replicas when scaling down.
`spec.maxReplicas`:: Specifies the maximum number of replicas when scaling up.
`spec.metrics`:: Specifies the parameters to calculate the desired replica count. Set the
`spec.metrics.resource.name`:: Specifies a name for the resource.
`spec.metrics.resource.target.type`:: Specifies the type of target, here `AverageValue` for a specific memory value.
`spec.metrics.resource.target.averageValue`:: Specifies the targeted memory value.
`spec.behavior`:: Optional: Specifies a scaling policy to control the rate of scaling up or down.
--

. Create the horizontal pod autoscaler by using a command similar to the following:
+
[source,terminal]
----
$ oc create -f <file-name>.yaml
----
+
For example:
+
[source,terminal]
----
$ oc create -f hpa.yaml
----
+
.Example output
[source,terminal]
----
horizontalpodautoscaler.autoscaling/hpa-resource-metrics-memory created
----

.Verification

* Check that the horizontal pod autoscaler was created by using a command similar to the following:
+
[source,terminal]
----
$ oc get hpa hpa-resource-metrics-memory
----
+
.Example output
[source,terminal]
----
NAME                          REFERENCE            TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
hpa-resource-metrics-memory   Deployment/example   2441216/500Mi   1         10        1          20m
----

* Check the details of the horizontal pod autoscaler by using a command similar to the following:
+
[source,terminal]
----
$ oc describe hpa hpa-resource-metrics-memory
----
+
.Example output
[source,text]
----
Name:                        hpa-resource-metrics-memory
Namespace:                   default
Labels:                      <none>
Annotations:                 <none>
CreationTimestamp:           Wed, 04 Mar 2020 16:31:37 +0530
Reference:                   Deployment/example
Metrics:                     ( current / target )
  resource memory on pods:   2441216 / 500Mi
Min replicas:                1
Max replicas:                10
ReplicationController pods:  1 current / 1 desired
Conditions:
  Type            Status  Reason              Message
  ----            ------  ------              -------
  AbleToScale     True    ReadyForNewScale    recommended size matches current size
  ScalingActive   True    ValidMetricFound    the HPA was able to successfully calculate a replica count from memory resource
  ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
Events:
  Type     Reason                   Age                 From                       Message
  ----     ------                   ----                ----                       -------
  Normal   SuccessfulRescale        6m34s               horizontal-pod-autoscaler  New size: 1; reason: All metrics below target
----
