

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 用於 HyperPod 推論模型部署的自動擴展政策
<a name="sagemaker-hyperpod-model-deployment-autoscaling"></a>

下列資訊提供在 Amazon SageMaker HyperPod 推論模型部署上實作自動擴展政策的實際範例和組態。

您將了解如何設定在部署 YAML 檔案中使用內建 `autoScalingSpec` 的自動擴展，以及如何為進階擴展案例建立獨立的 KEDA `ScaledObject` 組態。這些範例涵蓋以 CloudWatch 指標、Amazon SQS 佇列長度、Prometheus 查詢和資源使用率指標 (例如 CPU 和記憶體) 為基礎的擴展觸發條件。

## 在部署 YAML 中使用 autoScalingSpec
<a name="sagemaker-hyperpod-model-deployment-autoscaling-yaml"></a>

Amazon SageMaker HyperPod 推論運算子會使用來自 CloudWatch 和 Amazon Managed Prometheus (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 中所用欄位的說明
<a name="sagemaker-hyperpod-model-deployment-autoscaling-fields"></a>

`minReplicaCount` (選用，整數)  
指定要在叢集中維護的模型部署複本數量下限。在縮減事件期間，部署會縮減到此 Pod 數量下限。必須等於大於 0。預設：1。

`maxReplicaCount` (選用，整數)  
指定要在叢集中維護的模型部署複本數量上限。必須等於或大於 `minReplicaCount`。在擴增事件期間，部署會擴增到此 Pod 數量上限。預設：5。

`pollingInterval` (選用，整數)  
查詢指標的時間間隔，以秒為單位。下限：0。預設：30秒。

`cooldownPeriod` (選用，整數)  
在縮減事件期間，從 1 個縮減至 0 個 Pod 之前等待的時間間隔，以秒為單位。僅在 `minReplicaCount` 設為 0 時才適用。下限：0。預設：300 秒。

`initialCooldownPeriod` (選用，整數)  
在初始部署期間，從 1 個縮減到 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 請求。預設：true。
+ `namespace` (必要，字串) - 要查詢之指標的 CloudWatch 命名空間。
+ `metricName` (必要，字串) - CloudWatch 指標的名稱。
+ `dimensions` (選用，列出) - 指標的維度清單。每個維度都包含名稱 (維度名稱 - 字串) 和值 (維度值 - 字串)。
+ `targetValue` (必要，浮點數) - 用於自動擴展決策的 CloudWatch 指標目標值。
+ `activationTargetValue` (必要，浮點數) - 從 0 擴展到 1 個 Pod 時所使用的 CloudWatch 指標目標值。僅在 `minReplicaCount` 設為 0 時才適用。預設：0.
+ `minValue` (選用，浮點數) - CloudWatch 查詢未傳回資料時要使用的值。預設：0.
+ `metricCollectionStartTime` (選用，整數) - 指標查詢的開始時間，計算為 T-metricCollectionStartTime。必須大於或等於 metricCollectionPeriod。預設：300 秒。
+ `metricCollectionPeriod` (選用，整數) - 指標查詢的持續時間，以秒為單位。必須是 CloudWatch 支援的值 (1、5、10、30 或 60 的倍數)。預設：300 秒。
+ `metricStat` (選用，字串) - CloudWatch 查詢的統計資料類型。預設：`Average`。
+ `metricType` (選用，字串) - 定義如何將指標用於擴展計算。預設：`Average`。允許的值：`Average`、`Value`。
  + **平均值**：所需複本 = ceil (指標值) / (targetValue)
  + **值**：所需複本 = (目前複本) × ceil (指標值) / (targetValue)

`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 請求。預設：true。
+ `serverAddress` (必要，字串) - AMP 伺服器的位址。必須使用格式：<https://aps-workspaces.<region>.amazonaws.com/workspaces/<workspace\$1id>
+ `query` (必要，字串) - 用於指標的 PromQL 查詢。必須傳回純量值。
+ `targetValue` (必要，浮點數) - 用於自動擴展決策的 CloudWatch 指標目標值。
+ `activationTargetValue` (必要，浮點數) - 從 0 擴展到 1 個 Pod 時所使用的 CloudWatch 指標目標值。僅在 `minReplicaCount` 設為 0 時才適用。預設：0.
+ `namespace` (選用，字串) - 用於命名空間查詢的命名空間。預設：空白字串 (`""`)
+ `customHeaders` (選用，字串) - 查詢 Prometheus 端點時要包含的自訂標頭。預設：空白字串 ("")
+ `metricType` (選用，字串) - 定義如何將指標用於擴展計算。預設：`Average`。允許的值：`Average`、`Value`。
  + **平均值**：所需複本 = ceil (指標值) / (targetValue)
  + **值**：所需複本 = (目前複本) × ceil (指標值) / (targetValue)

## 透過 kubectl 使用 KEDA ScaledObject yaml 定義
<a name="sagemaker-hyperpod-model-deployment-autoscaling-kubectl"></a>

除了透過部署 YAML 中的 autoScalingSpec 區段設定自動擴展之外，您還可以使用 kubectl 建立和套用獨立 KEDA `ScaledObject` YAML 定義。

此方法可為複雜的擴展案例提供更大的彈性，並可讓您獨立於模型部署管理自動擴展政策。KEDA `ScaledObject` 組態支援[各種擴展觸發條件](https://keda.sh/docs/2.17/scalers/)，包括 CloudWatch 指標、Amazon SQS 佇列長度、Prometheus 查詢，以及 CPU 和記憶體使用率等資源型指標。您可以在 ScaledObject 規格的 scaleTargetRef 區段中參考部署名稱，將這些組態套用至現有的模型部署。

**注意**  
確保 HyperPod Inference 運算子安裝期間提供的 keda 運算子角色具有足夠的許可，來查詢擴展物件觸發條件中定義的指標。

### CloudWatch 指標
<a name="sagemaker-hyperpod-model-deployment-autoscaling-kubectl-cw"></a>

下列 KEDA yaml 政策會使用 CloudWatch 指標做為觸發條件，以在 kubernetes 部署上執行自動擴展。此政策會查詢 Sagemaker 端點的調用次數，並擴展部署 Pod 的數量。您可以在 [https://keda.sh/docs/2.17/scalers/aws-cloudwatch/](https://keda.sh/docs/2.17/scalers/aws-cloudwatch/) 找到 KEDA for `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 指標
<a name="sagemaker-hyperpod-model-deployment-autoscaling-kubectl-sqs"></a>

下列 KEDA yaml 政策會使用 Amazon SQS 指標做為觸發條件，以在 kubernetes 部署上執行自動擴展。此政策會查詢 Sagemaker 端點的調用次數，並擴展部署 Pod 的數量。您可以在 [https://keda.sh/docs/2.17/scalers/aws-sqs/](https://keda.sh/docs/2.17/scalers/aws-sqs/) 找到 KEDA for `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-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 指標
<a name="sagemaker-hyperpod-model-deployment-autoscaling-kubectl-prometheus"></a>

下列 KEDA yaml 政策會使用 Prometheus 指標做為觸發條件，以在 kubernetes 部署上執行自動擴展。此政策會查詢 Sagemaker 端點的調用次數，並擴展部署 Pod 的數量。您可以在 [https://keda.sh/docs/2.17/scalers/prometheus/](https://keda.sh/docs/2.17/scalers/prometheus/) 找到 KEDA for `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: 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 指標
<a name="sagemaker-hyperpod-model-deployment-autoscaling-kubectl-cpu"></a>

下列 KEDA yaml 政策會使用 cpu 指標做為觸發條件，以在 kubernetes 部署上執行自動擴展。此政策會查詢 Sagemaker 端點的調用次數，並擴展部署 Pod 的數量。您可以在 [https://keda.sh/docs/2.17/scalers/prometheus/](https://keda.sh/docs/2.17/scalers/prometheus/) 找到 KEDA for `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: cpu
    metricType: Utilization # Allowed types are 'Utilization' or 'AverageValue'
    metadata:
        value: "60"
        containerName: "" # Optional. You can use this to target a specific container
```

### 記憶體指標
<a name="sagemaker-hyperpod-model-deployment-autoscaling-kubectl-memory"></a>

下列 KEDA yaml 政策會使用 Prometheus 指標查詢做為觸發條件，以在 kubernetes 部署上執行自動擴展。此政策會查詢 Sagemaker 端點的調用次數，並擴展部署 Pod 的數量。您可以在 [https://keda.sh/docs/2.17/scalers/prometheus/](https://keda.sh/docs/2.17/scalers/prometheus/) 找到 KEDA for `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: 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 政策
<a name="sagemaker-hyperpod-model-deployment-autoscaling-kubectl-sample"></a>

下列 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 \$1 CPU 或 Prometheus \$1 CPU) 時，CPU 和記憶體觸發條件才能縮減至 0。