文档

使用Knative实现资源自动扩缩容

更新时间:

借助阿里云Knative的HPA(Horizontal Pod Autoscaler)弹性能力,您可以在Knative Service中设置HPA指标的类型和阈值,实现在突发高负载的场景下资源的自动扩缩容。

前提条件

步骤一:部署Knative Service

  1. 登录容器服务管理控制台,在左侧导航栏选择集群

  2. 集群列表页面,单击目标集群名称,然后在左侧导航栏,选择应用 > Knative

  3. Knative页面的服务管理页签下,选择命名空间default,然后单击使用模板创建,将以下YAML示例粘贴至模板,最后单击创建

    创建一个名为helloworld-go-hpa的服务,以下为配置示例。

    重要

    请将下方代码中的{REGION-ID}替换为您集群所在地域,以确保可以正确地访问和使用镜像。

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-go-hpa # 服务名称。
    spec:
      template:
        metadata:
          labels:
            app: helloworld-go-hpa
          annotations:
            autoscaling.knative.dev/class: "hpa.autoscaling.knative.dev" # 指定弹性插件为HPA。
            autoscaling.knative.dev/metric: "cpu" # HPA的指标类型可以设置为CPU和Memory。此处以CPU为例。
            autoscaling.knative.dev/target: "75" # 设置HPA CPU指标的阈值。根据该阈值,Knative的HPA将自动调整副本数。
            autoscaling.knative.dev/minScale: "1" # 设置弹性策略实例数的最小值。
            autoscaling.knative.dev/maxScale: "10" # 设置弹性策略实例数的最大值。
        spec:
          containers:
            - image: registry.vpc.{REGION-ID}.aliyuncs.com/knative-samples/helloworld-go:160e4dc8 # {REGION-ID}为对应集群所在的地域,如cn-hangzhou。
              resources:
                requests:
                  cpu: '200m'
  4. 执行以下命令,检查服务是否正常运行。

    kubectl get ksvc

    预期输出类似如下结果:

    NAME                   URL                                               LATESTCREATED                LATESTREADY                  READY   REASON
    helloworld-go-hpa      http://helloworld-go-hpa.default.example.com      helloworld-go-hpa-00001      helloworld-go-hpa-00001      True        

    如果READY列为True,表明服务正常运行。如果READY列为False,请确认是否已按照文档正确地部署服务。

步骤二:基于请求实现自动弹性扩缩容

  1. 安装Hey压测工具。

    关于Hey压测工具的详细信息,请参见Hey

  2. 执行以下命令,模拟50个并发用户在30秒内对服务发起访问,从而进行压测。

    hey -z 30s -c 50   -host "helloworld-go.default.example.com"   "http://121.199.XXX.XXX?sleep=100&prime=10000&bloat=5" # 121.199.XXX.XXX为网关IP。

    在压测的同时,执行以下命令实时查看Pod扩缩容情况。

    kubectl get pods --watch

    Pod扩缩容预期输出如下结果:

    NAME                                                     READY   STATUS    RESTARTS   AGE
    # 该Pod正在运行,且容器已经就绪。
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxfl5      2/2     Running   0          101m
    # Pod扩容为4个,且状态为0/2 Pending,这意味着容器正在等待调度并分配资源。
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      0/2     Pending   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      0/2     Pending   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      0/2     Pending   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      0/2     Pending   0          0s
    # 4个Pod的状态变为0/2 ContainerCreating,表示容器正在创建。
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      0/2     ContainerCreating   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      0/2     ContainerCreating   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      0/2     ContainerCreating   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      0/2     ContainerCreating   0          0s
    # 4个Pod的状态变为1/2 Running和2/2 Running,表示其中一个或两个容器已经成功创建并运行。
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      1/2     Running             0          1s
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      2/2     Running             0          1s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      1/2     Running             0          1s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      2/2     Running             0          1s

    输出结果表明,Knative具有基于请求实现自动弹性扩缩容的能力。表现为当系统负载增加,需要更多的Pod来处理请求时,Pod数从最初的1个扩展到了4个,以提高系统的处理能力和吞吐量。

步骤三:查看Knative监控大盘

Knative提供开箱即用的可观测能力,在Knative页面单击监控大盘页签,即可查看服务的监控数据情况。关于大盘信息的详细介绍,请参见通过阿里云Prometheus监控查看Knative大盘

image.png

相关文档

  • 阿里云Knative通过整合容器构建(或者函数)、工作负载管理(动态扩缩)以及事件模型来实现Serverless标准。关于阿里云Knative的更多基本信息,请参见Knative概述

  • 关于阿里云Knative的更多功能,请参见阿里云Knative和开源Knative对比

  • 本页导读 (1)
文档反馈