本頁面說明如何使用 GKE 水平 Pod 自動調度器 (HPA) 設定自動調度基礎架構,並透過 Hugging Face 的 Text Generation Interface (TGI) 服務架構部署 Gemma 大型語言模型 (LLM)。
如要進一步瞭解如何選取自動調度資源的指標,請參閱在 GKE 上使用 GPU 自動調度 LLM 工作負載的最佳做法。
事前準備
開始之前,請確認你已完成下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update
,取得最新版本。
- 請參閱「使用 Hugging Face TGI 在 GKE 上透過 GPU 提供 Gemma 開放模型」,熟悉工作流程。
使用伺服器指標自動調度資源
您可以運用 TGI 推論伺服器發出的工作負載專屬效能指標,直接調度 Pod 資源。如要進一步瞭解這些指標,請參閱「伺服器指標」。
如要使用伺服器指標設定自訂指標自動調度資源,請按照下列步驟操作:
將 TGI 伺服器的指標匯出至 Cloud Monitoring。您使用 Google Cloud Managed Service for Prometheus,簡化 Prometheus 收集器的部署和設定作業。GKE 叢集預設會啟用 Google Cloud Managed Service for Prometheus,您也可以手動啟用。
以下資訊清單範例說明如何設定 PodMonitoring 資源定義,引導 Google Cloud Managed Service for Prometheus 以每 15 秒的間隔,從 Pod 抓取指標:
apiVersion: monitoring.googleapis.com/v1 kind: PodMonitoring metadata: name: gemma-pod-monitoring spec: selector: matchLabels: app: gemma-server endpoints: - port: 8000 interval: 15s
安裝自訂指標 Stackdriver 轉接器。這個轉接程式會將您匯出至 Monitoring 的自訂指標,顯示給 HPA 控制器。詳情請參閱 Google Cloud Managed Service for Prometheus 說明文件中的Pod 水平自動調度資源。
以下範例指令說明如何安裝轉接器:
kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/k8s-stackdriver/master/custom-metrics-stackdriver-adapter/deploy/production/adapter_new_resource_model.yaml
設定以自訂指標為依據的 HPA 資源。部署以偏好自訂指標為依據的 HPA 資源。詳情請參閱 Google Cloud Managed Service for Prometheus 說明文件中的Pod 水平自動調度資源。
選取下列任一分頁,即可查看如何在資訊清單中設定 HorizontalPodAutoscaler 資源的範例:
佇列大小
這個範例使用
tgi_queue_size
TGI 伺服器指標,代表佇列中的要求數量。如要判斷 HPA 的合適佇列大小門檻,請參閱使用 GPU 自動調整 LLM 推論工作負載的最佳做法。
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: gemma-server spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: tgi-gemma-deployment minReplicas: 1 maxReplicas: 5 metrics: - type: Pods pods: metric: name: prometheus.googleapis.com|tgi_queue_size|gauge target: type: AverageValue averageValue: $HPA_AVERAGEVALUE_TARGET
批量
這個範例使用
tgi_batch_size
TGI 伺服器指標,代表目前批次中的要求數。如要判斷 HPA 的適當批次大小門檻,請參閱使用 GPU 自動調整 LLM 推論工作負載的最佳做法。
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: gemma-server spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: tgi-gemma-deployment minReplicas: 1 maxReplicas: 5 metrics: - type: Pods pods: metric: name: prometheus.googleapis.com|tgi_batch_current_size|gauge target: type: AverageValue averageValue: $HPA_AVERAGEVALUE_TARGET
使用 GPU 指標自動調度資源
您可以根據 GPU 發出的使用量和效能指標,直接為 Pod 啟用自動調整功能。如要進一步瞭解這些指標,請參閱 GPU 指標。
如要使用 GPU 指標設定自訂指標自動調度資源,請按照下列步驟操作:
將 GPU 指標匯出至 Cloud Monitoring。如果 GKE 叢集已啟用系統指標,系統會每 60 秒透過
container/accelerator/duty_cycle
系統指標,自動將 GPU 使用率指標傳送至 Cloud Monitoring。- 如要瞭解如何啟用 GKE 系統指標,請參閱「設定指標收集作業」。
- 如要設定代管收集作業,請參閱 Google Cloud Managed Service for Prometheus 說明文件中的「開始使用代管收集作業」。
- 如需在 GKE 中監控 GPU 工作負載效能的其他技巧,請參閱「在 GKE Standard 節點集區中執行 GPU」。
以下資訊清單範例說明如何設定 PodMonitoring 資源定義,從 NVIDIA DCGM 工作負載擷取指標:
apiVersion: monitoring.googleapis.com/v1 kind: PodMonitoring metadata: name: nvidia-dcgm-exporter-for-hpa namespace: gke-managed-system labels: app.kubernetes.io/name: nvidia-dcgm-exporter app.kubernetes.io/part-of: google-cloud-managed-prometheus spec: selector: matchLabels: app.kubernetes.io/name: gke-managed-dcgm-exporter endpoints: - port: metrics interval: 15s metricRelabeling: - action: keep sourceLabels: [__name__] - action: replace sourceLabels: [__name__] targetLabel: __name__ regex: DCGM_FI_DEV_GPU_UTIL replacement: dcgm_fi_dev_gpu_util
在程式碼中,請務必將 HPA 中使用的 DCGM 指標名稱改為小寫。 這是因為已知問題:HPA 無法處理大寫的外部指標名稱。如果叢集未使用受管理 DCGM 匯出工具,請確保 HPA 的
metadata.namespace
和spec.selector.matchLabels
與 DCGM 匯出工具的設定完全一致。這項精確的對齊作業至關重要,有助於 HPA 順利探索及查詢自訂指標。安裝自訂指標 Stackdriver 轉接器。這個轉接程式會將您匯出至 Monitoring 的自訂指標,顯示給 HPA 控制器。詳情請參閱 Google Cloud Managed Service for Prometheus 說明文件中的Pod 水平自動調度資源。
以下範例指令說明如何執行這項安裝作業:
kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/k8s-stackdriver/master/custom-metrics-stackdriver-adapter/deploy/production/adapter_new_resource_model.yaml
設定以自訂指標為依據的 HPA 資源。依據偏好的自訂指標部署 HPA 資源。詳情請參閱 Google Cloud Managed Service for Prometheus 說明文件中的Pod 水平自動調度資源。
- 找出 HPA 的平均值目標,以觸發自動調度資源。您可以進行實驗,例如在伺服器上產生越來越高的負載,並觀察 GPU 使用率達到尖峰的時間點。請注意HPA 容許度,這項設定預設為目標值周圍的 0.1 無動作範圍,可抑制震盪。
- 建議使用 locust-load-inference 工具進行測試。您也可以建立 Cloud Monitoring 自訂資訊主頁,以視覺化方式呈現指標行為。
選取其中一個分頁,查看如何在資訊清單中設定 HorizontalPodAutoscaler 資源的範例:
工作週期 (GKE 系統)
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: gemma-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: tgi-gemma-deployment minReplicas: 1 maxReplicas: 5 metrics: - type: External external: metric: name: kubernetes.io|container|accelerator|duty_cycle selector: matchLabels: resource.labels.container_name: inference-server resource.labels.namespace_name: default target: type: AverageValue averageValue: $HPA_AVERAGEVALUE_TARGET
工作週期 (DCGM)
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: gemma-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: tgi-gemma-deployment minReplicas: 1 maxReplicas: 5 metrics: - type: External external: metric: name: prometheus.googleapis.com|dcgm_fi_dev_gpu_util|unknown selector: matchLabels: metric.labels.exported_container: inference-server metric.labels.exported_namespace: default target: type: AverageValue averageValue: $HPA_AVERAGEVALUE_TARGET
後續步驟
- 瞭解如何根據 Cloud Monitoring 的指標,最佳化 Pod 自動調度資源。
- 如要進一步瞭解水平 Pod 自動調度資源,請參閱開放原始碼 Kubernetes 說明文件。