用于部署 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/v1alpha1 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 到 0 个 pod 之前等待的时间间隔(以秒为单位)。仅在设置minReplicaCount为 0 时适用。最小值:0。默认值:300 秒。

initialCooldownPeriod(可选,整数)

在初始部署期间从 1 个 pod 缩减到 0 之前等待的时间间隔(以秒为单位)。仅在设置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 查询指标,而水平吊舱自动扩缩器 (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

    • 平均值:所需副本 = ceil(指标值)/(targetValue)

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

prometheusTrigger

自动扩展决策中使用的亚马逊托管 Prometheus (AMP) 指标的触发器配置。中提供了以下字段prometheusTrigger

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

  • useCachedMetrics(可选,布尔值)-确定是否缓存 KEDA 查询的指标。KEDA 使用 PollingInterval 查询指标,而水平吊舱自动扩缩器 (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

    • 平均值:所需副本 = ceil(指标值)/(targetValue)

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

通过 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 终端节点的调用次数并扩展部署容器的数量。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 策略使用亚马逊 SQS 指标作为触发器,在 kubernetes 部署上执行自动扩展。该策略查询 Sagemaker 终端节点的调用次数并扩展部署容器的数量。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 终端节点的调用次数并扩展部署容器的数量。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 终端节点的调用次数并扩展部署容器的数量。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 终端节点的调用次数并扩展部署容器的数量。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 个吊舱的 Prometheus 策略示例

以下 KEDA yaml 策略使用 prometheus 指标查询作为触发器,在 kubernetes 部署上执行自动扩展。此策略使用 0 中的minReplicaCount,允许 KEDA 将部署规模缩小到 0 个 pod。当设置minReplicaCount为 0 时,您需要提供激活标准,以便在吊舱缩小到 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 或内存的额外缩放器时,CPU 和内存触发器才能缩放到 0(例如 SQS + CPU,或 Prometheus + CPU)。