用于部署 HyperPod 推理模型的自动缩放策略 - 亚马逊 SageMaker AI

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

用于部署 HyperPod 推理模型的自动缩放策略

以下信息提供了在 Amazon SageMaker HyperPod 推理模型部署上实施自动扩展策略的实际示例和配置。

您将了解如何使用部署 YAML 文件中的内置 autoScalingSpec 来配置自动扩缩,以及如何为高级扩展场景创建独立的 KEDA ScaledObject 配置。这些示例涵盖了基于 CloudWatch 指标、Amazon SQS 队列长度、Prometheus 查询以及 CPU 和内存等资源利用率指标的扩展触发器。

autoScalingSpec 在部署中使用 YAML

亚马逊 SageMaker HyperPod 推理运算符使用来自和亚马逊托管 Prometheus ( CloudWatch AMP) 的指标为模型部署提供内置的自动缩放功能。以下部署 YAML 示例包括一个 autoScalingSpec 部分,该部分定义了用于扩展模型部署的配置值。

apiVersion: inference.sagemaker.aws.amazon.com/v1 kind: JumpStartModel metadata: name: deepseek-sample624 namespace: ns-team-a spec: sageMakerEndpoint: name: deepsek7bsme624 model: modelHubName: SageMakerPublicHub modelId: deepseek-llm-r1-distill-qwen-1-5b modelVersion: 2.0.4 server: instanceType: ml.g5.8xlarge metrics: enabled: true environmentVariables: - name: SAMPLE_ENV_VAR value: "sample_value" maxDeployTimeInSeconds: 1800 tlsConfig: tlsCertificateOutputS3Uri: "s3://{USER}-tls-bucket-{REGION}/certificates" autoScalingSpec: minReplicaCount: 0 maxReplicaCount: 5 pollingInterval: 15 initialCooldownPeriod: 60 cooldownPeriod: 120 scaleDownStabilizationTime: 60 scaleUpStabilizationTime: 0 cloudWatchTrigger: name: "SageMaker-Invocations" namespace: "AWS/SageMaker" useCachedMetrics: false metricName: "Invocations" targetValue: 10.5 activationTargetValue: 5.0 minValue: 0.0 metricCollectionStartTime: 300 metricCollectionPeriod: 30 metricStat: "Sum" metricType: "Average" dimensions: - name: "EndpointName" value: "deepsek7bsme624" - name: "VariantName" value: "AllTraffic" prometheusTrigger: name: "Prometheus-Trigger" useCachedMetrics: false serverAddress: http://<prometheus-host>:9090 query: sum(rate(http_requests_total{deployment="my-deployment"}[2m])) targetValue: 10.0 activationTargetValue: 5.0 namespace: "namespace" customHeaders: "X-Client-Id=cid" metricType: "Value"

部署 YAML 中使用的字段的说明

minReplicaCount(可选,整数)

指定要在集群中维护的模型部署副本的最小数量。在缩减事件期间,部署会缩减至此最小数量的容器组(pod)。必须大于或等于 0。默认值:1。

maxReplicaCount(可选,整数)

指定要在集群中维护的模型部署副本的最大数量。必须大于或等于 minReplicaCount。在纵向扩展事件期间,部署会纵向扩展至此最大数量的容器组(pod)。默认值:5。

pollingInterval(可选,整数)

查询指标的时间间隔(以秒为单位)。最小值:0。默认值:30 秒。

cooldownPeriod(可选,整数)

在缩减事件期间,从 1 个容器组(pod)缩减至 0 个容器组(pod)之前等待的时间间隔(以秒为单位)。只有当 minReplicaCount 设置为 0 时才适用。最小值:0。默认值:300 秒。

initialCooldownPeriod(可选,整数)

在初始部署期间,从 1 个容器组(pod)缩减至 0 个容器组(pod)之前等待的时间间隔(以秒为单位)。只有当 minReplicaCount 设置为 0 时才适用。最小值:0。默认值:300 秒。

scaleDownStabilizationTime(可选,整数)

缩减触发器激活后且实际缩减操作执行前的稳定时段(以秒为单位)。最小值:0。默认值:300 秒。

scaleUpStabilizationTime(可选,整数)

纵向扩展触发器激活后且实际纵向扩展操作执行前的稳定时段(以秒为单位)。最小值:0。默认值:0 秒。

cloudWatchTrigger

自动缩放决策中使用的 CloudWatch 指标的触发器配置。以下字段在 cloudWatchTrigger 中可用:

  • name(可选,字符串)- CloudWatch 触发器的名称。如果未提供,则使用默认格式:< model-deployment-name >-scaled-object-cloudwatch-trigger。

  • useCachedMetrics(可选,布尔值)– 确定是否缓存由 KEDA 查询的指标。KEDA 使用 pollingInterval 查询指标,而 Horizontal Pod Autoscaler(HPA)每 15 秒向 KEDA 请求一次指标。设置为 true 时,查询到的指标将被缓存并用于响应 HPA 请求。默认值:真。

  • namespace(必填,字符串)-要查询的指标的 CloudWatch命名空间。

  • metricName(必填,字符串)- CloudWatch 指标的名称。

  • dimensions(可选,列表)– 指标的维度列表。每个维度均包含一个名称(维度名称 – 字符串)和值(维度值 – 字符串)。

  • targetValue(必填,浮点型)-自动缩放决策中使用的 CloudWatch 指标的目标值。

  • activationTargetValue(可选,Float)-从 0 缩放到 1 个 pod 时使用的 CloudWatch 指标的目标值。只有当 minReplicaCount 设置为 0 时才适用。默认值:0。

  • minValue(可选,Float)- CloudWatch 查询不返回任何数据时使用的值。默认值:0。

  • metricCollectionStartTime(可选,整数)-指标查询的开始时间,计算为 T-metricCollectionStart 时间。必须大于或等于 metricCollectionPeriod。默认值:300 秒。

  • metricCollectionPeriod(可选,整数)– 指标查询的持续时间(以秒为单位)。必须是 CloudWatch支持的值(1、5、10、30 或 60 的倍数)。默认值:300 秒。

  • metricStat(可选,字符串)- CloudWatch 查询的统计数据类型。默认值:Average

  • metricType(可选,字符串)– 定义如何使用指标进行扩缩计算。默认值:Average。允许的值:AverageValue

    • Average:所需副本数 = ceil(指标值)/(目标值)

    • Value:所需副本数 =(当前副本数)× ceil(指标值)/(目标值)

prometheusTrigger

自动扩缩决策中使用的 Amazon Managed Prometheus(AMP)指标的触发器配置。以下字段在 prometheusTrigger 中可用:

  • name(可选,字符串)- CloudWatch 触发器的名称。如果未提供,则使用默认格式:< model-deployment-name >-scaled-object-cloudwatch-trigger。

  • useCachedMetrics(可选,布尔值)– 确定是否缓存由 KEDA 查询的指标。KEDA 使用 pollingInterval 查询指标,而 Horizontal Pod Autoscaler(HPA)每 15 秒向 KEDA 请求一次指标。设置为 true 时,查询到的指标将被缓存并用于响应 HPA 请求。默认值:真。

  • serverAddress(必填,字符串)– AMP 服务器的地址。必须使用以下格式:<https://aps-workspaces.<region>.amazonaws.com/workspaces/<workspace_id>

  • query(必填,字符串)– 用于指标的 PromQL 查询。必须返回标量值。

  • targetValue(必填,浮点型)-自动缩放决策中使用的 CloudWatch 指标的目标值。

  • activationTargetValue(可选,Float)-从 0 缩放到 1 个 pod 时使用的 CloudWatch 指标的目标值。只有当 minReplicaCount 设置为 0 时才适用。默认值:0。

  • namespace(可选,字符串)– 用于命名空间查询的命名空间。默认值:空字符串("")。

  • customHeaders(可选,字符串)– 查询 Prometheus 端点时要包含的自定义标头。默认值:空字符串("")。

  • metricType(可选,字符串)– 定义如何使用指标进行扩缩计算。默认值:Average。允许的值:AverageValue

    • Average:所需副本数 = ceil(指标值)/(目标值)

    • Value:所需副本数 =(当前副本数)× ceil(指标值)/(目标值)

通过 kubectl 使用 KEDA ScaledObject yaml 定义

除了通过部署 YAML 中的 autoScalingSpec 部分配置自动缩放外,您还可以使用 kubectl 创建和应用独立的 KEDA ScaledObject YAML 定义。

此方法为复杂的扩缩场景提供了更高的灵活性,并允许您独立于模型部署来管理自动扩缩策略。KEDA ScaledObject 配置支持各种扩展触发器,包括 CloudWatch 指标、Amazon SQS 队列长度、Prometheus 查询以及基于资源的指标,例如 CPU 和内存利用率。您可以通过引用 ScaledObject 规范 scaleTargetRef部分中的部署名称将这些配置应用于现有模型部署。

注意

确保在 HyperPod 推理运算符安装期间提供的 keda 操作员角色具有足够的权限来查询缩放对象触发器中定义的指标。

CloudWatch 指标

以下 KEDA yaml 策略使用 CloudWatch 指标作为触发器,在 kubernetes 部署上执行自动扩展。该策略会查询 Sagemaker 端点的调用次数并扩展部署容器组(pod)的数量。KEDA 支持的aws-cloudwatch触发参数的完整列表可在 https://keda 找到。 sh/docs/2.17/scalers/aws-cloudwatch/

apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: invocations-scaledobject # name of the scaled object that will be created by this namespace: ns-team-a # namespace that this scaled object targets spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: $DEPLOYMENT_NAME # name of the model deployment minReplicaCount: 1 # minimum number of pods to be maintained maxReplicaCount: 4 # maximum number of pods to scale to pollingInterval: 10 triggers: - type: aws-cloudwatch metadata: namespace: AWS/SageMaker metricName: Invocations targetMetricValue: "1" minMetricValue: "1" awsRegion: "us-west-2" dimensionName: EndpointName;VariantName dimensionValue: $ENDPOINT_NAME;$VARIANT_NAME metricStatPeriod: "30" # seconds metricStat: "Sum" identityOwner: operator

Amazon SQS 指标

以下 KEDA yaml 策略使用 Amazon SQS 指标作为触发器,在 kubernetes 部署上执行自动扩缩。该策略会查询 Sagemaker 端点的调用次数并扩展部署容器组(pod)的数量。KEDA 支持的aws-cloudwatch触发参数的完整列表可在 https://keda 找到。 sh/docs/2.17/scalers/aws-sqs/

apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: invocations-scaledobject # name of the scaled object that will be created by this namespace: ns-team-a # namespace that this scaled object targets spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: $DEPLOYMENT_NAME # name of the model deployment minReplicaCount: 1 # minimum number of pods to be maintained maxReplicaCount: 4 # maximum number of pods to scale to pollingInterval: 10 triggers: - type: aws-sqs-queue metadata: queueURL: https://sqs.eu-west-1.amazonaws.com/account_id/QueueName queueLength: "5" # Default: "5" awsRegion: "us-west-1" scaleOnInFlight: true identityOwner: operator

Prometheus 指标

以下 KEDA yaml 策略使用 Prometheus 指标作为触发器,在 kubernetes 部署上执行自动扩缩。该策略会查询 Sagemaker 端点的调用次数并扩展部署容器组(pod)的数量。KEDA 支持的aws-cloudwatch触发参数的完整列表可在 https://keda 找到。 sh/docs/2.17/scalers/prometheus/。

apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: invocations-scaledobject # name of the scaled object that will be created by this namespace: ns-team-a # namespace that this scaled object targets spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: $DEPLOYMENT_NAME # name of the model deployment minReplicaCount: 1 # minimum number of pods to be maintained maxReplicaCount: 4 # maximum number of pods to scale to pollingInterval: 10 triggers: - type: prometheus metadata: serverAddress: http://<prometheus-host>:9090 query: avg(rate(http_requests_total{deployment="$DEPLOYMENT_NAME"}[2m])) # Note: query must return a vector/scalar single element response threshold: '100.50' namespace: example-namespace # for namespaced queries, eg. Thanos customHeaders: X-Client-Id=cid,X-Tenant-Id=tid,X-Organization-Id=oid # Optional. Custom headers to include in query. In case of auth header, use the custom authentication or relevant authModes. unsafeSsl: "false" # Default is `false`, Used for skipping certificate check when having self-signed certs for Prometheus endpoint timeout: 1000 # Custom timeout for the HTTP client used in this scaler identityOwner: operator

CPU 指标

以下 KEDA yaml 策略使用 CPU 指标作为触发器,在 kubernetes 部署上执行自动扩缩。该策略会查询 Sagemaker 端点的调用次数并扩展部署容器组(pod)的数量。KEDA 支持的aws-cloudwatch触发参数的完整列表可在 https://keda 找到。 sh/docs/2.17/scalers/prometheus/。

apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: invocations-scaledobject # name of the scaled object that will be created by this namespace: ns-team-a # namespace that this scaled object targets spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: $DEPLOYMENT_NAME # name of the model deployment minReplicaCount: 1 # minimum number of pods to be maintained maxReplicaCount: 4 # maximum number of pods to scale to pollingInterval: 10 triggers: - type: cpu metricType: Utilization # Allowed types are 'Utilization' or 'AverageValue' metadata: value: "60" containerName: "" # Optional. You can use this to target a specific container

内存指标

以下 KEDA yaml 策略使用 Prometheus 指标查询作为触发器,在 kubernetes 部署上执行自动扩缩。该策略会查询 Sagemaker 端点的调用次数并扩展部署容器组(pod)的数量。KEDA 支持的aws-cloudwatch触发参数的完整列表可在 https://keda 找到。 sh/docs/2.17/scalers/prometheus/。

apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: invocations-scaledobject # name of the scaled object that will be created by this namespace: ns-team-a # namespace that this scaled object targets spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: $DEPLOYMENT_NAME # name of the model deployment minReplicaCount: 1 # minimum number of pods to be maintained maxReplicaCount: 4 # maximum number of pods to scale to pollingInterval: 10 triggers: - type: memory metricType: Utilization # Allowed types are 'Utilization' or 'AverageValue' metadata: value: "60" containerName: "" # Optional. You can use this to target a specific container in a pod

用于缩减至 0 个容器组(pod)的 Prometheus 策略示例

以下 KEDA yaml 策略使用 Prometheus 指标查询作为触发器,在 kubernetes 部署上执行自动扩缩。此策略将 minReplicaCount 设置为 0,使 KEDA 能够将部署缩减至 0 个容器组(pod)。在将 minReplicaCount 设置为 0 的情况下,在容器组(pod)缩减至 0 个后,您需要提供激活条件来启动第一个容器组(pod)。对于 Prometheus 触发器,此值由 activationThreshold 提供。对于 SQS 队列,此值来自 activationQueueLength

注意

在将 minReplicaCount 设置为 0 时,确保激活不依赖于容器组(pod)所生成的指标。当容器组(pod)缩减至 0 个时,该指标将不再生成,并且容器组(pod)也不会再次纵向扩展。

apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: invocations-scaledobject # name of the scaled object that will be created by this namespace: ns-team-a # namespace that this scaled object targets spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: $DEPLOYMENT_NAME # name of the model deployment minReplicaCount: 0 # minimum number of pods to be maintained maxReplicaCount: 4 # maximum number of pods to scale to pollingInterval: 10 cooldownPeriod: 30 initialCooldownPeriod: 180 # time before scaling down the pods after initial deployment triggers: - type: prometheus metadata: serverAddress: http://<prometheus-host>:9090 query: sum(rate(http_requests_total{deployment="my-deployment"}[2m])) # Note: query must return a vector/scalar single element response threshold: '100.50' activationThreshold: '5.5' # Required if minReplicaCount is 0 for initial scaling namespace: example-namespace timeout: 1000 identityOwner: operator
注意

仅当您定义了至少一个非 CPU 或内存类型的额外扩缩器(例如 SQS + CPU,或 Prometheus + CPU)时,CPU 和内存触发器才能缩减至 0。