Overview

A horizontal pod autoscaler, defined by a HorizontalPodAutoscaler object, specifies how the system should automatically increase or decrease the scale of a replication controller or deployment configuration, based on metrics collected from the pods that belong to that replication controller or deployment configuration.

Requirements for Using Horizontal Pod Autoscalers

In order to use horizontal pod autoscalers, your cluster administrator must have properly configured cluster metrics.

Supported Metrics

The following metrics are supported by horizontal pod autoscalers:

Table 1. Metrics
Metric Description

CPU utilization

Percentage of the requested CPU

Memory utilization

Percentage of the requested memory.

Autoscaling

You can create a horizontal pod autoscaler with the oc autoscale command and specify the minimum and maximum number of pods you want to run, as well as the CPU utilization your pods should target.

After a horizontal pod autoscaler is created, it begins attempting to query Heapster for metrics on the pods. It may take one to two minutes before Heapster obtains the initial metrics.

After metrics are available in Heapster, the horizontal pod autoscaler computes the ratio of the current metric utilization with the desired metric utilization, and scales up or down accordingly. The scaling will occur at a regular interval, but it may take one to two minutes before metrics make their way into Heapster.

For replication controllers, this scaling corresponds directly to the replicas of the replication controller. For deployment configurations, scaling corresponds directly to the replica count of the deployment configuration. Note that autoscaling applies only to the latest deployment in the Complete phase.

OpenShift Origin automatically accounts for resources and prevents unnecessary autoscaling during resource spikes, such as during start up. Pods in the unready state have 0 CPU usage when scaling up and the autoscaler ignores the pods when scaling down. Pods without known metrics have 0% CPU usage when scaling up and 100% CPU when scaling down. This allows for more stability during the HPA decision. To use this feature, you must configure readiness checks to determine if a new pod is ready for use.

Autoscaling for CPU Utilization

Use the oc autoscale command and specify at least the maximum number of pods you want to run at any given time. You can optionally specify the minimum number of pods and the average CPU utilization your pods should target, otherwise those are given default values from the OpenShift Origin server.

For example:

$ oc autoscale dc/frontend --min 1 --max 10 /
  --cpu-percent=80 deploymentconfig "frontend" autoscaled

The above example creates a horizontal pod autoscaler with the following definition when using version one of the horizontal pod autoscaler:

Example 1. Horizontal Pod Autoscaler Object Definition
apiVersion: extensions/v1beta1
kind: HorizontalPodAutoscaler
metadata:
  name: frontend (1)
spec:
  scaleRef:
    kind: DeploymentConfig (2)
    name: frontend (3)
    apiVersion: v1 (4)
    subresource: scale
  minReplicas: 1 (5)
  maxReplicas: 10 (6)
  cpuUtilization:
    targetPercentage: 80 (7)
1 The name of this horizontal pod autoscaler object
2 The kind of object to scale
3 The name of the object to scale
4 The API version of the object to scale
5 The minimum number of replicas to which to scale down
6 The maximum number of replicas to which to scale up
7 The percentage of the requested CPU that each pod should ideally be using

Alternatively, the oc autoscale command creates a horizontal pod autoscaler with the following definition when using version two of the horizontal pod autoscaler:

apiVersion: autoscaling/v2alpha1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-resource-metrics-cpu (1)
spec:
  scaleTargetRef:
    apiVersion: apps/v1beta1 (2)
    kind: ReplicationController (3)
    name: hello-hpa-cpu (4)
  minReplicas: 1 (5)
  maxReplicas: 10 (6)
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 50 (7)
1 The name of this horizontal pod autoscaler object
2 The API version of the object to scale
3 The kind of object to scale
4 The name of the object to scale
5 The minimum number of replicas to which to scale down
6 The maximum number of replicas to which to scale up
7 The average utilization for each pod

Autoscaling for Memory Utilization

Unlike CPU-based autoscaling, memory-based autoscaling requires specifying the autoscaler using YAML instead of using the oc autoscale command. Optionally, you can specify the minimum number of pods and the average memory utilization your pods should target as well, otherwise those are given default values from the OpenShift Origin server.

  1. Memory-based autoscaling is only available with the v2alpha1 version of the autoscaling API. Enable memory-based autoscaling by adding the following to your cluster’s master-config.yaml file:

    ...
    apiServerArguments:
      runtime-config:
      - apis/autoscaling/v2alpha1=true
    ...
  2. Place the following in a file, such as hpa.yaml:

    apiVersion: autoscaling/v2alpha1
    kind: HorizontalPodAutoscaler
    metadata:
      name: hpa-resource-metrics-memory (1)
    spec:
      scaleTargetRef:
        apiVersion: apps/v1beta1 (2)
        kind: ReplicationController (3)
        name: hello-hpa-memory (4)
      minReplicas: 1 (5)
      maxReplicas: 10 (6)
      metrics:
      - type: Resource
        resource:
          name: memory
          targetAverageUtilization: 50 (7)
    1 The name of this horizontal pod autoscaler object
    2 The API version of the object to scale
    3 The kind of object to scale
    4 The name of the object to scale
    5 The minimum number of replicas to which to scale down
    6 The maximum number of replicas to which to scale up
    7 The average percentage of the requested memory that each pod should be using
  3. Then, create the autoscaler from the above file:

    $ oc create -f hpa.yaml

Viewing a Horizontal Pod Autoscaler

To view the status of a horizontal pod autoscaler:

$ oc get hpa/frontend
NAME              REFERENCE                                 TARGET    CURRENT   MINPODS        MAXPODS   AGE
frontend          DeploymentConfig/default/frontend/scale   80%       79%       1              10        8d

$ oc describe hpa/frontend
Name:                           frontend
Namespace:                      default
Labels:                         <none>
CreationTimestamp:              Mon, 26 Oct 2015 21:13:47 -0400
Reference:                      DeploymentConfig/default/frontend/scale
Target CPU utilization:         80%
Current CPU utilization:        79%
Min pods:                       1
Max pods:                       10