1.4. 自动缩放 pod

作为开发人员,您可以使用 pod 横向自动扩展 (HPA) 来指定 OpenShift Container Platform 如何根据从属于某复制控制器或部署配置的 pod 收集的指标来自动增加或缩小该复制控制器或部署配置的规模。

1.4.1. 了解 pod 横向自动扩展

您可以创建一个 pod 横向自动扩展来指定您要运行的 pod 的最小和最大数量,以及 pod 的目标 CPU 使用率或内存使用率。

重要

根据内存使用率自动缩放仅是一项技术预览功能。

在创建了 pod 横向自动扩展后,OpenShift Container Platform 会开始查询 pod 上的 CPU 和/或内存资源指标。当这些指标可用时,pod 横向自动扩展会计算当前指标使用率与所需指标使用率的比率,并相应地扩展或缩减。查询和缩放是定期进行的,但可能需要一到两分钟时间才会有可用指标。

对于复制控制器,这种缩放直接与复制控制器的副本对应。对于部署配置,缩放直接与部署配置的副本计数对应。注意,自动缩放仅应用到 Complete 阶段的最新部署。

OpenShift Container Platform 会自动考虑资源情况,并防止在资源激增期间进行不必要的自动缩放,比如在启动过程中。处于 unready 状态的 pod 在扩展时具有 0 CPU 用量,自动扩展在缩减时会忽略这些 pod。没有已知指标的 Pod 在扩展时具有 0% CPU 用量,在缩减时具有 100% CPU 用量。这在 HPA 决策过程中提供更高的稳定性。要使用这个功能,您必须配置就绪度检查来确定新 pod 是否准备就绪。

要使用 pod 横向自动扩展,您的集群管理员必须已经正确配置了集群指标。

1.4.1.1. 支持的指标

pod 横向自动扩展支持以下指标:

表 1.1. 指标

指标描述API 版本

CPU 使用率

已用的 CPU 内核数。可以用来计算 pod 的已请求 CPU 百分比。

autoscaling/v1autoscaling/v2beta2

内存使用率

已用内存量。可以用来计算 pod 的已请求内存百分比。

autoscaling/v2beta2

重要

对于基于内存的自动缩放,内存用量必须与副本数呈正比增大和减小。平均而言:

  • 增加副本数一定会导致每个 pod 的内存(工作集)用量总体降低。
  • 减少副本数一定会导致每个 pod 的内存用量总体增高。

使用 OpenShift Container Platform Web 控制台检查应用程序的内存行为,并确保应用程序在使用基于内存的自动缩放前满足这些要求。

1.4.2. 基于 CPU 使用率创建 pod 横向自动扩展

您可以为现有的 DeploymentConfig 或 ReplicationController 对象创建一个 pod 横向自动扩展 (HPA) ,用于自动扩展与该对象关联的 Pod,以维护您指定的 CPU 用量。

HPA 会在最小和最大数量之间增加和减少副本数,以保持所有 Pod 的指定 CPU 使用率。

为 CPU 使用率自动扩展时,您可以使用 oc autoscale 命令,并指定要在任意给定时间运行的 Pod 的最小和最大数量,以及 Pod 的目标平均 CPU 使用率。如果未指定最小值,则 OpenShift Container Platform 服务器会为 Pod 赋予一个默认值。要自动缩放特定 CPU 值,创建一个带有目标 CPU 和 Pod 限制的 HorizontalPodAutoscaler 对象。

先决条件

要使用 pod 横向自动扩展,您的集群管理员必须已经正确配置了集群指标。您可以使用 oc describe PodMetrics <pod-name> 命令来判断是否已配置了指标。如果配置了指标,输出类似于以下示例,其中 Usage 下列出了 CpuMemory

$ oc describe PodMetrics openshift-kube-scheduler-ip-10-0-135-131.ec2.internal
Name:         openshift-kube-scheduler-ip-10-0-135-131.ec2.internal
Namespace:    openshift-kube-scheduler
Labels:       <none>
Annotations:  <none>
API Version:  metrics.k8s.io/v1beta1
Containers:
  Name:  wait-for-host-port
  Usage:
    Memory:  0
  Name:      scheduler
  Usage:
    Cpu:     8m
    Memory:  45440Ki
Kind:        PodMetrics
Metadata:
  Creation Timestamp:  2019-05-23T18:47:56Z
  Self Link:           /apis/metrics.k8s.io/v1beta1/namespaces/openshift-kube-scheduler/pods/openshift-kube-scheduler-ip-10-0-135-131.ec2.internal
Timestamp:             2019-05-23T18:47:56Z
Window:                1m0s
Events:                <none>

流程

为 CPU 使用率创建 pod 横向自动扩展

  1. 执行以下步骤之一:

    • 要根据 CPU 使用率百分比来缩放,请为现有 DeploymentConfig 创建一个 HorizontalPodAutoscaler 对象:

      $ oc autoscale dc/<dc-name> \1
        --min <number> \2
        --max <number> \3
        --cpu-percent=<percent> 4
      1
      指定 DeploymentConfig 的名称。对象必须存在。
      2
      另外,还可以指定缩减时的最小副本数量。
      3
      指定扩展时的最大副本数量。
      4
      指定所有 pod 的目标平均 CPU 使用率(以请求 CPU 的百分比表示)。如果未指定或为负数,则会使用默认的自动缩放策略。
    • 要根据 CPU 使用率百分比来缩放,请为现有的 ReplicationController 创建一个HorizontalPodAutoscaler 对象:

      $ oc autoscale rc/<rc-name> 1
        --min <number> \2
        --max <number> \3
        --cpu-percent=<percent> 4
      1
      指定 ReplicationController 的名称。对象必须存在。
      2
      指定缩减时的最小副本数量。
      3
      指定扩展时的最大副本数量。
      4
      指定所有 pod 的目标平均 CPU 使用率(以请求 CPU 的百分比表示)。如果未指定或为负数,则会使用默认的自动缩放策略。
    • 要扩展特定 CPU 值,请为现有 DeploymentConfig 或 ReplicationController 创建类似如下的一个 YAML 文件:

      1. 创建一个类似以下示例的 YAML 文件:

        apiVersion: autoscaling/v2beta2 1
        kind: HorizontalPodAutoscaler
        metadata:
          name: cpu-autoscale 2
          namespace: default
        spec:
          scaleTargetRef:
            apiVersion: v1 3
            kind: ReplicationController 4
            name: example 5
          minReplicas: 1 6
          maxReplicas: 10 7
          metrics: 8
          - type: Resource
            resource:
              name: cpu 9
              target:
                type: Utilization 10
                averageValue: 500Mi 11
        1
        使用 autoscaling/v2beta2 API。
        2
        指定此 pod 横向自动扩展对象的名称。
        3
        指定要缩放对象的 API 版本。
        • 对于 ReplicationController,使用 v1
        • 对于 DeploymentConfig,使用 apps.openshift.io/v1
        4
        指定要缩放的对象类型,可以是 ReplicationControllerDeploymentConfig
        5
        指定要缩放的对象名称。对象必须存在。
        6
        指定缩减时的最小副本数量。
        7
        指定扩展时的最大副本数量。
        8
        对于内存使用率,使用 metrics 参数。
        9
        为 CPU 使用率指定 cpu
        10
        设置 Utilization
        11
        将类型设置为 averageValue
      2. 创建 Pod 横向自动扩展:

        $ oc create -f <file-name>.yaml
  2. 验证 pod 横向自动扩展是否已创建:

    $ oc get hpa hpa-resource-metrics-memory
    
    NAME                                     REFERENCE                       TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
    oc get hpa hpa-resource-metrics-memory   ReplicationController/example   2441216/500Mi   1         10        1          20m

例如,以下命令会创建一个 Pod 横向自动扩展,维护由 image-registry DeploymentConfig 控制的 3 到 7 个 Pod 副本,以便确保所有 Pod 的平均 CPU 使用率为 75%。

$ oc autoscale dc/image-registry --min 3 --max 7 --cpu-percent=75
deploymentconfig "image-registry" autoscaled

该命令创建带有以下定义的 pod 横向自动扩展:

$ oc edit hpa frontend -n openshift-image-registry
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  creationTimestamp: "2020-02-21T20:19:28Z"
  name: image-registry
  namespace: default
  resourceVersion: "32452"
  selfLink: /apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/frontend
  uid: 1a934a22-925d-431e-813a-d00461ad7521
spec:
  maxReplicas: 7
  minReplicas: 3
  scaleTargetRef:
    apiVersion: apps.openshift.io/v1
    kind: DeploymentConfig
    name: image-registry
  targetCPUUtilizationPercentage: 75
status:
  currentReplicas: 5
  desiredReplicas: 0

下例演示了 image-registry DeploymentConfig 的自动缩放。初始部署需要 3 个 Pod。HPA 对象将最小值增加到 5,如果 pod 的 CPU 用量达到 75%,会将 pod 数最高增加到 7:

$ oc get dc image-registry
NAME             REVISION   DESIRED   CURRENT   TRIGGERED BY
image-registry   1          3         3         config

$ oc autoscale dc/image-registry --min=5 --max=7 --cpu-percent=75
horizontalpodautoscaler.autoscaling/image-registry autoscaled

$ oc get dc image-registry
NAME             REVISION   DESIRED   CURRENT   TRIGGERED BY
image-registry   1          5         5         config

1.4.3. 基于内存使用率创建 pod 横向自动扩展对象

您可以为现有 DeploymentConfig 或 ReplicationController 对象创建一个 pod 横向自动扩展 (HPA) ,用于自动扩展与该对象关联的 Pod,以便维护您指定的平均内存使用率(可以是一个直接的值,也可以是请求的内存百分比)。

HPA 增加和减少最小和最大数量之间的副本数量,以维护所有 Pod 的指定内存使用率。

对于内存使用率,您可以指定 Pod 的最小和最大数量,以及 Pod 的目标平均内存使用率。如果未指定最小值,则 OpenShift Container Platform 服务器会为 Pod 赋予一个默认值。

重要

根据内存使用率自动缩放仅是一项技术预览功能。技术预览功能不包括在红帽生产服务级别协议 (SLA) 中,且其功能可能并不完善。因此,红帽不建议在生产环境中使用它们。这些技术预览功能可以使用户提早试用新的功能,并有机会在开发阶段提供反馈意见。

如需红帽技术预览功能支持范围的更多信息,请参阅 https://access.redhat.com/support/offerings/techpreview/

先决条件

要使用 pod 横向自动扩展,您的集群管理员必须已经正确配置了集群指标。您可以使用 oc describe PodMetrics <pod-name> 命令来判断是否已配置了指标。如果配置了指标,输出类似于以下示例,其中 Usage 下列出了 CpuMemory

$ oc describe PodMetrics openshift-kube-scheduler-ip-10-0-129-223.compute.internal -n openshift-kube-scheduler
Name:         openshift-kube-scheduler-ip-10-0-129-223.compute.internal
Namespace:    openshift-kube-scheduler
Labels:       <none>
Annotations:  <none>
API Version:  metrics.k8s.io/v1beta1
Containers:
  Name:  scheduler
  Usage:
    Cpu:     2m
    Memory:  41056Ki
  Name:      wait-for-host-port
  Usage:
    Memory:  0
Kind:        PodMetrics
Metadata:
  Creation Timestamp:  2020-02-14T22:21:14Z
  Self Link:           /apis/metrics.k8s.io/v1beta1/namespaces/openshift-kube-scheduler/pods/openshift-kube-scheduler-ip-10-0-129-223.compute.internal
Timestamp:             2020-02-14T22:21:14Z
Window:                5m0s
Events:                <none>

流程

根据内存使用率创建 pod 横向自动扩展:

  1. 为以下之一创建一个 YAML 文件:

    • 要扩展特定内存值,请为现有 DeploymentConfig 或 ReplicationController 创建类似如下的 HorizontalPodAutoscaler 对象:

      apiVersion: autoscaling/v2beta2 1
      kind: HorizontalPodAutoscaler
      metadata:
        name: hpa-resource-metrics-memory 2
        namespace: default
      spec:
        scaleTargetRef:
          apiVersion: v1 3
          kind: ReplicationController 4
          name: example 5
        minReplicas: 1 6
        maxReplicas: 10 7
        metrics: 8
        - type: Resource
          resource:
            name: memory 9
            target:
              type: Utilization 10
              averageValue: 500Mi 11
      1
      使用 autoscaling/v2beta2 API。
      2
      指定此 pod 横向自动扩展对象的名称。
      3
      指定要缩放对象的 API 版本。
      • 对于 ReplicationController,使用 v1
      • 对于 DeploymentConfig,使用 apps.openshift.io/v1
      4
      指定要缩放的对象类型,可以是 ReplicationControllerDeploymentConfig
      5
      指定要缩放的对象名称。对象必须存在。
      6
      指定缩减时的最小副本数量。
      7
      指定扩展时的最大副本数量。
      8
      对于内存使用率,使用 metrics 参数。
      9
      为内存使用率指定 memory
      10
      将类型设置为 Utilization
      11
      指定 averageValue 和一个特定的内存值。
    • 要缩放一个百分比,创建一个与以下类似的 HorizontalPodAutoscaler 对象:

      apiVersion: autoscaling/v2beta2 1
      kind: HorizontalPodAutoscaler
      metadata:
        name: memory-autoscale 2
        namespace: default
      spec:
        scaleTargetRef:
          apiVersion: apps.openshift.io/v1 3
          kind: DeploymentConfig 4
          name: example 5
        minReplicas: 1 6
        maxReplicas: 10 7
        metrics:
        metrics: 8
        - type: Resource
          resource:
            name: memory 9
            target:
              type: Utilization 10
              averageValue: 50 11
      1
      使用 autoscaling/v2beta2 API。
      2
      指定此 pod 横向自动扩展对象的名称。
      3
      指定要缩放对象的 API 版本。
      • 对于 ReplicationController,使用 v1
      • 对于 DeploymentConfig,使用 apps.openshift.io/v1
      4
      指定要缩放的对象类型,可以是 ReplicationControllerDeploymentConfig
      5
      指定要缩放的对象名称。对象必须存在。
      6
      指定缩减时的最小副本数量。
      7
      指定扩展时的最大副本数量。
      8
      对于内存使用率,使用 metrics 参数。
      9
      为内存使用率指定 memory
      10
      设置 Utilization
      11
      指定 averageUtilizationaverageValue ,以及一个内存值。
  2. 创建 Pod 横向自动扩展:

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

    例如:

    $ oc create -f hpa.yaml
    
    horizontalpodautoscaler.autoscaling/hpa-resource-metrics-memory created
  3. 验证 pod 横向自动扩展是否已创建:

    $ oc get hpa hpa-resource-metrics-memory
    
    NAME                                     REFERENCE                       TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
    oc get hpa hpa-resource-metrics-memory   ReplicationController/example   2441216/500Mi   1         10        1          20m
    $ oc describe hpa hpa-resource-metrics-memory
    Name:                        hpa-resource-metrics-memory
    Namespace:                   default
    Labels:                      <none>
    Annotations:                 <none>
    CreationTimestamp:           Wed, 04 Mar 2020 16:31:37 +0530
    Reference:                   ReplicationController/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

1.4.4. 了解 pod 横向自动扩展状态条件

您可以使用设置的状态条件来判断 pod 横向自动扩展 (HPA) 是否能够缩放,以及目前是否受到某种方式的限制。

HPA 状态条件可通过 v2beta1 版的自动扩展 API 使用。

HPA 可以通过下列状态条件给予响应:

  • AbleToScale 条件指示 HPA 是否能够获取和更新指标,以及是否有任何与退避相关的条件阻碍了缩放。

    • True 条件表示允许缩放。
    • False 条件表示因为指定原因不允许缩放。
  • ScalingActive 条件指示 HPA 是否已启用(例如,目标的副本数不为零),并且可以计算所需的指标。

    • True 条件表示指标工作正常。
    • False 条件通常表示获取指标时出现问题。
  • ScalingLimited 条件表示所需的规模由 pod 横向自动扩展限定最大或最小限制。

    • True 条件表示您需要提高或降低最小或最大副本数才能进行缩放。
    • False 条件表示允许请求的缩放。

      $ oc describe hpa cm-test
      Name:                           cm-test
      Namespace:                      prom
      Labels:                         <none>
      Annotations:                    <none>
      CreationTimestamp:              Fri, 16 Jun 2017 18:09:22 +0000
      Reference:                      ReplicationController/cm-test
      Metrics:                        ( current / target )
        "http_requests" on pods:      66m / 500m
      Min replicas:                   1
      Max replicas:                   4
      ReplicationController pods:     1 current / 1 desired
      Conditions: 1
        Type              Status    Reason              Message
        ----              ------    ------              -------
        AbleToScale       True      ReadyForNewScale    the last scale time was sufficiently old as to warrant a new scale
        ScalingActive     True      ValidMetricFound    the HPA was able to successfully calculate a replica count from pods metric http_request
        ScalingLimited    False     DesiredWithinRange  the desired replica count is within the acceptable range
      Events:
      1
      pod 横向自动扩展状态消息。

下例中是一个无法缩放的 pod:

Conditions:
  Type         Status  Reason          Message
  ----         ------  ------          -------
  AbleToScale  False   FailedGetScale  the HPA controller was unable to get the target's current scale: no matches for kind "ReplicationController" in group "apps"
Events:
  Type     Reason          Age               From                       Message
  ----     ------          ----              ----                       -------
  Warning  FailedGetScale  6s (x3 over 36s)  horizontal-pod-autoscaler  no matches for kind "ReplicationController" in group "apps"

下例中是一个无法获得缩放所需指标的 pod:

Conditions:
  Type                  Status    Reason                    Message
  ----                  ------    ------                    -------
  AbleToScale           True     SucceededGetScale          the HPA controller was able to get the target's current scale
  ScalingActive         False    FailedGetResourceMetric    the HPA was unable to compute the replica count: unable to get metrics for resource cpu: no metrics returned from heapster

下例中是一个请求的自动缩放低于所需下限的 pod:

Conditions:
  Type              Status    Reason              Message
  ----              ------    ------              -------
  AbleToScale       True      ReadyForNewScale    the last scale time was sufficiently old as to warrant a new scale
  ScalingActive     True      ValidMetricFound    the HPA was able to successfully calculate a replica count from pods metric http_request
  ScalingLimited    False     DesiredWithinRange  the desired replica count is within the acceptable range

1.4.4.1. 查看 pod 横向自动扩展状态条件

您可以查看 pod 横向自动扩展 (HPA) 对 pod 设置的状态条件。

注意

pod 横向自动扩展状态条件可通过 v2beta1 版的自动扩展 API 使用。

先决条件

要使用 pod 横向自动扩展,您的集群管理员必须已经正确配置了集群指标。您可以使用 oc describe PodMetrics <pod-name> 命令来判断是否已配置了指标。如果配置了指标,输出类似于以下示例,其中 Usage 下列出了 CpuMemory

$ oc describe PodMetrics openshift-kube-scheduler-ip-10-0-135-131.ec2.internal

Name:         openshift-kube-scheduler-ip-10-0-135-131.ec2.internal
Namespace:    openshift-kube-scheduler
Labels:       <none>
Annotations:  <none>
API Version:  metrics.k8s.io/v1beta1
Containers:
  Name:  wait-for-host-port
  Usage:
    Memory:  0
  Name:      scheduler
  Usage:
    Cpu:     8m
    Memory:  45440Ki
Kind:        PodMetrics
Metadata:
  Creation Timestamp:  2019-05-23T18:47:56Z
  Self Link:           /apis/metrics.k8s.io/v1beta1/namespaces/openshift-kube-scheduler/pods/openshift-kube-scheduler-ip-10-0-135-131.ec2.internal
Timestamp:             2019-05-23T18:47:56Z
Window:                1m0s
Events:                <none>

流程

要查看 pod 上的状态条件,请使用以下命令并提供 pod 的名称:

$ oc describe hpa <pod-name>

例如:

$ oc describe hpa cm-test

这些条件会出现在输出中的 Conditions 字段里。

Name:                           cm-test
Namespace:                      prom
Labels:                         <none>
Annotations:                    <none>
CreationTimestamp:              Fri, 16 Jun 2017 18:09:22 +0000
Reference:                      ReplicationController/cm-test
Metrics:                        ( current / target )
  "http_requests" on pods:      66m / 500m
Min replicas:                   1
Max replicas:                   4
ReplicationController pods:     1 current / 1 desired
Conditions: 1
  Type              Status    Reason              Message
  ----              ------    ------              -------
  AbleToScale       True      ReadyForNewScale    the last scale time was sufficiently old as to warrant a new scale
  ScalingActive     True      ValidMetricFound    the HPA was able to successfully calculate a replica count from pods metric http_request
  ScalingLimited    False     DesiredWithinRange  the desired replica count is within the acceptable range

1.4.5. 其他资源

如需有关复制控制器和部署控制器的更多信息,请参见了解 Deployment 和 DeploymentConfig