本文档采用自动化机器翻译技术翻译。 尽管我们力求提供准确的译文,但不对翻译内容的完整性、准确性或可靠性作出任何保证。 若出现任何内容不一致情况,请以原始 英文 版本为准,且原始英文版本为权威文本。

使用 kubectl 测试 HPA

本文档描述了如何在使用负载测试工具对 HPA 进行扩缩容后检查其状态。有关如何从 Rancher UI(至少版本 2.3.x)检查状态的信息,请参阅 使用 Rancher UI 管理 HPA

为了使 HPA 正常工作,服务部署应为容器定义资源请求。按照这个 hello-world 示例测试 HPA 是否正常工作。

  1. 配置 kubectl 以连接到您的 Kubernetes 集群。

  2. 复制下面的 hello-world 部署清单。

    Hello World 清单

    +

      apiVersion: apps/v1beta2
      kind: Deployment
      metadata:
        labels:
          app: hello-world
        name: hello-world
        namespace: default
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: hello-world
        strategy:
          rollingUpdate:
            maxSurge: 1
            maxUnavailable: 0
          type: RollingUpdate
        template:
          metadata:
            labels:
              app: hello-world
          spec:
            containers:
            - image: rancher/hello-world
              imagePullPolicy: Always
              name: hello-world
              resources:
                requests:
                  cpu: 500m
                  memory: 64Mi
              ports:
              - containerPort: 80
                protocol: TCP
            restartPolicy: Always
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: hello-world
        namespace: default
      spec:
        ports:
        - port: 80
          protocol: TCP
          targetPort: 80
        selector:
          app: hello-world

    +

  3. 将其部署到您的集群。

     # kubectl create -f <HELLO_WORLD_MANIFEST>
  4. 根据您使用的指标类型,复制下面的一个 HPA:

    Hello World HPA:资源指标

    +

       apiVersion: autoscaling/v2beta1
       kind: HorizontalPodAutoscaler
       metadata:
         name: hello-world
         namespace: default
       spec:
         scaleTargetRef:
           apiVersion: extensions/v1beta1
           kind: Deployment
           name: hello-world
         minReplicas: 1
         maxReplicas: 10
         metrics:
         - type: Resource
           resource:
             name: cpu
             targetAverageUtilization: 50
         - type: Resource
           resource:
             name: memory
             targetAverageValue: 1000Mi

    +

+

  apiVersion: autoscaling/v2beta1
  kind: HorizontalPodAutoscaler
  metadata:
    name: hello-world
    namespace: default
  spec:
    scaleTargetRef:
      apiVersion: extensions/v1beta1
      kind: Deployment
      name: hello-world
    minReplicas: 1
    maxReplicas: 10
    metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: 50
    - type: Resource
      resource:
        name: memory
        targetAverageValue: 100Mi
    - type: Pods
      pods:
        metricName: cpu_system
        targetAverageValue: 20m

+

  1. 查看 HPA 信息和描述。确认显示指标数据。

    资源指标
    1. 输入以下命令。 ` # kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE hello-world Deployment/hello-world 1253376 / 100Mi, 0% / 50% 1 10 1 6m # kubectl describe hpa Name: hello-world Namespace: default Labels: <none> Annotations: <none> CreationTimestamp: 2018年7月23日,星期一 20:21:16 +0200 参考: 部署/hello-world
      指标: (当前 / 目标)
      Pod 上的资源内存: 1253376 / 100Mi
      Pod 上的资源处理器(请求的百分比): 0% (0) / 50%
      最小副本数: 1
      最大副本数: 10
      条件: 类型 状态 原因 消息 ---- ------ ------ -------
      可扩展 真 准备好新扩展 上次扩展时间足够久,值得进行新的扩展
      扩展活动 真 找到有效指标 HPA能够成功计算出内存资源的副本数量
      扩展受限 假 在范围内期望 期望数量在可接受范围内
      事件: <none>
      `

  1. 输入以下命令。
    `
    # kubectl describe hpa
    `
    您应该收到以下输出。
    `
    名称: hello-world
    名称空间: 默认
    标签: <none>
    注释: <none>
    创建时间戳: 2018年7月24日 星期二 18:36:28 +0200
    参考: 部署/hello-world
    指标: (当前 / 目标)
    Pod 上的资源内存: 3514368 / 100Mi
    "cpu_system" 在 Pod 上: 0 / 20m
    Pod 上的资源处理器(请求的百分比): 0% (0) / 50%
    最小副本数: 1
    最大副本数: 10
    条件: 类型 状态 原因 消息 ---- ------ ------ -------
    可扩展 真 准备好新扩展 上次扩展时间足够久,值得进行新的扩展
    扩展活动 真 找到有效指标 HPA能够成功计算出内存资源的副本数量
    扩展受限 假 在范围内期望 期望数量在可接受范围内
    事件: <none>
    `

  1. 为服务生成负载,以测试您的 Pod 是否按预期自动扩展。您可以使用任何负载测试工具(Hey、Gatling等),但我们使用的是 Hey

  2. 测试 Pod 自动扩展是否按预期工作。

    使用资源指标测试自动扩展:

    扩展到 2 个 Pod:处理器使用率达到目标

    使用您的负载测试工具根据处理器使用率扩展到两个 Pod。

    1. 查看您的 HPA。

       # kubectl describe hpa

      您应该收到类似于以下内容的输出。

       Name:                                                  hello-world
       Namespace:                                             default
       Labels:                                                <none>
       Annotations:                                           <none>
       CreationTimestamp:                                     Mon, 23 Jul 2018 22:22:04 +0200
       Reference:                                             Deployment/hello-world
       Metrics:                                               ( current / target )
         resource memory on pods:                             10928128 / 100Mi
         resource cpu on pods  (as a percentage of request):  56% (280m) / 50%
       Min replicas:                                          1
       Max replicas:                                          10
       Conditions:
         Type            Status  Reason              Message
         ----            ------  ------              -------
         AbleToScale     True    SucceededRescale    the HPA controller was able to update the target scale to 2
         ScalingActive   True    ValidMetricFound    the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
         ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
       Events:
         Type    Reason             Age   From                       Message
         ----    ------             ----  ----                       -------
         Normal  SuccessfulRescale  13s   horizontal-pod-autoscaler  New size: 2; reason: cpu resource utilization (percentage of request) above target
    2. 输入以下命令以确认您已扩展到两个 Pod。

         # kubectl get pods

      您应该收到类似于以下内容的输出:

         NAME                                                     READY     STATUS    RESTARTS   AGE
         hello-world-54764dfbf8-k8ph2                             1/1       Running   0          1m
         hello-world-54764dfbf8-q6l4v                             1/1       Running   0          3h

使用您的负载测试工具根据处理器使用率将 horizontal-pod-autoscaler-upscale-delay 设置为 3 分钟,扩展到 3 个 Pod。

  1. 输入以下命令。

    # kubectl describe hpa

    您应该收到类似于以下内容的输出。

       Name:                                                  hello-world
       Namespace:                                             default
       Labels:                                                <none>
       Annotations:                                           <none>
       CreationTimestamp:                                     Mon, 23 Jul 2018 22:22:04 +0200
       Reference:                                             Deployment/hello-world
       Metrics:                                               ( current / target )
         resource memory on pods:                             9424896 / 100Mi
         resource cpu on pods  (as a percentage of request):  66% (333m) / 50%
       Min replicas:                                          1
       Max replicas:                                          10
       Conditions:
         Type            Status  Reason              Message
         ----            ------  ------              -------
         AbleToScale     True    SucceededRescale    the HPA controller was able to update the target scale to 3
         ScalingActive   True    ValidMetricFound    the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
         ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
       Events:
         Type    Reason             Age   From                       Message
         ----    ------             ----  ----                       -------
         Normal  SuccessfulRescale  4m    horizontal-pod-autoscaler  New size: 2; reason: cpu resource utilization (percentage of request) above target
         Normal  SuccessfulRescale  16s   horizontal-pod-autoscaler  New size: 3; reason: cpu resource utilization (percentage of request) above target
  2. 输入以下命令以确认三个 Pod 正在运行。

    # kubectl get pods

    您应该收到类似于以下内容的输出。

       NAME                                                     READY     STATUS    RESTARTS   AGE
       hello-world-54764dfbf8-f46kh                             0/1       Running   0          1m
       hello-world-54764dfbf8-k8ph2                             1/1       Running   0          5m
       hello-world-54764dfbf8-q6l4v                             1/1       Running   0          3h
缩减到 1 个 Pod:所有指标低于目标

当所有指标在 horizontal-pod-autoscaler-downscale-delay(默认 5 分钟)内低于目标时,使用您的负载测试工具缩减到 1 个 Pod。

  1. 输入以下命令。

       # kubectl describe hpa

    您应该收到类似于以下内容的输出。

       Name:                                                  hello-world
       Namespace:                                             default
       Labels:                                                <none>
       Annotations:                                           <none>
       CreationTimestamp:                                     Mon, 23 Jul 2018 22:22:04 +0200
       Reference:                                             Deployment/hello-world
       Metrics:                                               ( current / target )
         resource memory on pods:                             10070016 / 100Mi
         resource cpu on pods  (as a percentage of request):  0% (0) / 50%
       Min replicas:                                          1
       Max replicas:                                          10
       Conditions:
         Type            Status  Reason              Message
         ----            ------  ------              -------
         AbleToScale     True    SucceededRescale    the HPA controller was able to update the target scale to 1
         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  10m   horizontal-pod-autoscaler  New size: 2; reason: cpu resource utilization (percentage of request) above target
         Normal  SuccessfulRescale  6m    horizontal-pod-autoscaler  New size: 3; reason: cpu resource utilization (percentage of request) above target
         Normal  SuccessfulRescale  1s    horizontal-pod-autoscaler  New size: 1; reason: All metrics below target

+ 使用自定义指标测试自动扩展:

+ .扩展到 2 个 Pod:处理器使用率达到目标

Details

使用您的负载测试工具根据处理器使用率扩展到两个 Pod。

  1. 输入以下命令:
    `
    # kubectl describe hpa
    `
    您应该收到类似于以下内容的输出:
    `
    名称: hello-world
    名称空间: default
    标签: <none>
    注释: <none>
    创建时间戳: 2018年7月24日星期二 18:01:11 +0200 参考: 部署/hello-world
    指标: ( 当前 / 目标 )
    Pod 上的资源内存: 8159232 / 100Mi
    "cpu_system" 在 Pod 上: 7m / 20m
    资源处理器在 Pod 上(请求的百分比): 64% (321m) / 50%
    最小副本数: 1
    最大副本数: 10 条件: 类型 状态 原因 消息 ---- ------ ------ -------
    AbleToScale True SucceededRescale HPA 控制器能够将目标规模更新为 2
    ScalingActive True ValidMetricFound HPA 能够成功计算出基于处理器资源利用率(请求的百分比)的副本数量
    ScalingLimited False DesiredWithinRange 期望数量在可接受范围内
    Events: 类型 原因 年龄 来源 消息 ---- ------ ---- ---- ------- 正常 成功重新缩放 16s 水平 Pod 自动扩缩器 新大小:2;原因:处理器 资源利用率(请求的百分比)超过目标 `

  2. 输入以下命令以确认两个 Pod 正在运行。
    `
    # kubectl get pods
    `
    您应该收到类似于以下内容的输出:
    `
    名称 准备 状态 重启次数 年龄
    hello-world-54764dfbf8-5pfdr 1/1 运行中 0 3s
    hello-world-54764dfbf8-q6l82 1/1 运行中 0 6h
    `

扩展到 3 个 Pod:处理器使用率达到目标

使用您的负载测试工具在 cpu_system 使用限制达到目标时扩展到三个 Pod。 ..输入以下命令。

+

# kubectl describe hpa

+ 您应该收到类似于以下内容的输出:

+

   Name:                                                  hello-world
   Namespace:                                             default
   Labels:                                                <none>
   Annotations:                                           <none>
   CreationTimestamp:                                     Tue, 24 Jul 2018 18:01:11 +0200
   Reference:                                             Deployment/hello-world
   Metrics:                                               ( current / target )
     resource memory on pods:                             8374272 / 100Mi
     "cpu_system" on pods:                                27m / 20m
     resource cpu on pods  (as a percentage of request):  71% (357m) / 50%
   Min replicas:                                          1
   Max replicas:                                          10
   Conditions:
     Type            Status  Reason              Message
     ----            ------  ------              -------
     AbleToScale     True    SucceededRescale    the HPA controller was able to update the target scale to 3
     ScalingActive   True    ValidMetricFound    the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
     ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
   Events:
     Type    Reason             Age   From                       Message
     ----    ------             ----  ----                       -------
     Normal  SuccessfulRescale  3m    horizontal-pod-autoscaler  New size: 2; reason: cpu resource utilization (percentage of request) above target
     Normal  SuccessfulRescale  3s    horizontal-pod-autoscaler  New size: 3; reason: pods metric cpu_system above target
  1. 输入以下命令以确认三个 Pod 正在运行。

    # kubectl get pods

    您应该收到类似于以下内容的输出:

       # kubectl get pods
       NAME                           READY     STATUS    RESTARTS   AGE
       hello-world-54764dfbf8-5pfdr   1/1       Running   0          3m
       hello-world-54764dfbf8-m2hrl   1/1       Running   0          1s
       hello-world-54764dfbf8-q6l82   1/1       Running   0          6h
扩展到 4 个 Pod:处理器使用率达到目标

使用您的负载测试工具根据处理器使用情况扩展到四个 Pod。 horizontal-pod-autoscaler-upscale-delay 默认设置为三分钟。

  1. 输入以下命令。
    `
    # kubectl describe hpa
    `
    您应该收到类似于以下内容的输出:
    `
    名称: hello-world
    名称空间: default
    标签: <none>
    注释: <none>
    创建时间戳: 2018年7月24日星期二 18:01:11 +0200 参考: 部署/hello-world
    指标: ( 当前 / 目标 )
    Pod 上的资源内存: 8374272 / 100Mi "cpu_system" 在 Pods 上: 27m / 20m 资源 CPU 在 Pods 上(请求的百分比): 71% (357m) / 50% 最小副本: 1 最大副本: 10 条件: 类型 状态 原因 消息 ---- ------ ------ ------- 能够扩展 真 成功调整 HPA 控制器能够将目标规模更新为 3 扩展活动 真 找到有效指标 HPA 能够成功计算出基于 CPU 资源利用率(请求的百分比)的副本数量 扩展受限 假 期望在范围内 期望数量在可接受范围内 事件: 类型 原因 年龄 来源 消息 ---- ------ ---- ---- ------- 正常 成功调整 5m 水平 Pod 自动扩展器 新大小:2; 原因:CPU 资源利用率(请求的百分比)高于目标 正常 成功调整 3m 水平 Pod 自动扩展器 新大小:3; 原因:pods 指标 cpu_system 高于目标 正常 成功调整 4s 水平 Pod 自动扩展器 新大小:4; 原因:CPU 资源利用率(请求的百分比)高于目标 `

  2. 输入以下命令以确认四个 Pods 正在运行。

    # kubectl get pods

    您应该收到类似于以下内容的输出。

      NAME                           READY     STATUS    RESTARTS   AGE
      hello-world-54764dfbf8-2p9xb   1/1       Running   0          5m
      hello-world-54764dfbf8-5pfdr   1/1       Running   0          2m
      hello-world-54764dfbf8-m2hrl   1/1       Running   0          1s
      hello-world-54764dfbf8-q6l82   1/1       Running   0          6h
缩减到 1 个 Pod:所有指标低于目标

当所有指标低于 horizontal-pod-autoscaler-downscale-delay 的目标时,使用您的负载测试工具将副本缩减到一个 Pod。

  1. 输入以下命令。

     # kubectl describe hpa

    您应该收到类似于以下内容的输出。

         Name:                                                  hello-world
         Namespace:                                             default
         Labels:                                                <none>
         Annotations:                                           <none>
         CreationTimestamp:                                     Tue, 24 Jul 2018 18:01:11 +0200
         Reference:                                             Deployment/hello-world
         Metrics:                                               ( current / target )
           resource memory on pods:                             8101888 / 100Mi
           "cpu_system" on pods:                                8m / 20m
           resource cpu on pods  (as a percentage of request):  0% (0) / 50%
         Min replicas:                                          1
         Max replicas:                                          10
         Conditions:
           Type            Status  Reason              Message
           ----            ------  ------              -------
           AbleToScale     True    SucceededRescale    the HPA controller was able to update the target scale to 1
           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  10m    horizontal-pod-autoscaler  New size: 2; reason: cpu resource utilization (percentage of request) above target
           Normal  SuccessfulRescale  8m    horizontal-pod-autoscaler  New size: 3; reason: pods metric cpu_system above target
           Normal  SuccessfulRescale  5m    horizontal-pod-autoscaler  New size: 4; reason: cpu resource utilization (percentage of request) above target
           Normal   SuccessfulRescale             13s               horizontal-pod-autoscaler  New size: 1; reason: All metrics below target
  2. 输入以下命令以确认单个 Pod 正在运行。

         # kubectl get pods

    您应该收到类似于以下内容的输出。

         NAME                           READY     STATUS    RESTARTS   AGE
         hello-world-54764dfbf8-q6l82   1/1       Running   0          6h