如要在 Google Kubernetes Engine (GKE) 上使用 GPU,透過 vLLM 框架提供 Mistral 大型語言模型 (LLM),您必須佈建具有支援加速器的 GKE 叢集,例如 NVIDIA H100 GPU。
如要提供 Mistral Small 4 模型,請設定預先建構的 vLLM 容器,載入模型權重。權重會從 Cloud Storage bucket 載入 (由 --model 引數指定)。
載入權重後,vLLM 容器會公開與 OpenAI 相容的 API 端點,以進行高處理量推論。
本教學課程適用於機器學習 (ML) 工程師、平台管理員和營運人員,以及有興趣使用 Kubernetes 容器自動化調度管理功能,在 H100 GPU 硬體上提供 AI/ML 工作負載服務的資料和 AI 專家。
閱讀本頁內容前,請務必先熟悉下列事項:
目標
本教學課程將提供基礎知識,協助您瞭解及探索如何在 Kubernetes 代管環境中,實際部署 LLM 以進行推論。
- 在 Autopilot 模式中,使用 GKE 叢集準備環境。
- 將 vLLM 容器部署至叢集。
- 透過 curl 介面,使用 vLLM 提供 Mistral 模型。
事前準備
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Cloud de Confiance project.
Enable the required API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
請確認您在專案中具備下列角色: roles/container.admin、roles/iam.serviceAccountAdmin
檢查角色
-
前往 Cloud de Confiance 控制台的「IAM」頁面。
前往「IAM」頁面 - 選取專案。
-
在「主體」欄中,找出所有可識別您或您所屬群組的資料列。如要瞭解自己所屬的群組,請與管理員聯絡。
- 針對指定或包含您的所有資料列,檢查「角色」欄,確認角色清單是否包含必要角色。
授予角色
-
前往 Cloud de Confiance 控制台的「IAM」頁面。
前往「IAM」頁面 - 選取專案。
- 按一下「Grant access」(授予存取權)。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是指員工身分集區中使用者的 ID。詳情請參閱「在 IAM 政策中代表工作團隊集區使用者」,或聯絡管理員。
- 按一下「Select a role」(選取角色),然後搜尋角色。
- 如要授予其他角色,請按一下「Add another role」(新增其他角色),然後新增其他角色。
- 按一下「Save」(儲存)。
-
準備環境
在本教學課程中,您將使用 kubectl 和 gcloud CLI 管理Cloud de Confiance by S3NS上託管的資源。您可以使用 gcloud CLI 授權存取 Cloud de Confiance by S3NS。
如要使用 gcloud CLI 設定環境,請在 gcloud CLI 中設定預設環境變數:
gcloud config set project PROJECT_ID
gcloud config set billing/quota_project PROJECT_ID
export PROJECT_ID=$(gcloud config get project)
export REGION=u-france-east1
export CLUSTER_NAME=CLUSTER_NAME
export KUBERNETES_SERVICEACCOUNT=KUBERNETES_SERVICEACCOUNT
export NAMESPACE=NAMESPACE
export PROJECT_NUMBER=$(gcloud projects describe PROJECT_ID --format="value(projectNumber)")
export MODEL_BUCKET_NAME=MODEL_BUCKET_NAME
替換下列值:
PROJECT_ID:您的 Cloud de Confiance 專案 ID。REGION:支援 H100 GPU 的u-france-east1區域。如要瞭解哪些區域提供哪些 GPU,請參閱這篇文章。CLUSTER_NAME:叢集名稱。KUBERNETES_SERVICEACCOUNT:Kubernetes ServiceAccount 的名稱,例如mistral-small-sa。NAMESPACE:Kubernetes 命名空間,例如default。MODEL_BUCKET_NAME:Cloud Storage bucket 的名稱,模型權重會儲存在這個 bucket 中。可以與所選模型同名,例如「mistral-small-4-119b-weights」。
建立及設定 Cloud de Confiance 資源
請按照下列操作說明建立必要資源。
建立 GKE 叢集和節點集區
您可以在 GKE Autopilot 叢集的 GPU 上提供 Mistral 服務。Autopilot 叢集提供全代管 Kubernetes 體驗。
在 gcloud CLI 中執行下列指令:
gcloud container clusters create-auto CLUSTER_NAME \
--project=PROJECT_ID \
--location=REGION \
--release-channel=rapid
替換下列值:
PROJECT_ID:您的 Cloud de Confiance 專案 ID。CLUSTER_NAME:叢集名稱。REGION:叢集所在的區域。
GKE 會根據部署的工作負載要求,建立具有 CPU 和 GPU 節點的 Autopilot 叢集。
建立 Cloud Storage bucket
在 gcloud CLI 中執行下列指令:
gcloud storage buckets create gs://${MODEL_BUCKET_NAME} \ --project=${PROJECT_ID} \ --location=${REGION} \ --uniform-bucket-level-access這會建立 Cloud Storage bucket,用於儲存從 Hugging Face 下載的模型檔案。
下載及上傳模型權重:
您需要取得要提供的 Mistral Small 4 模型權重 (例如來自 Hugging Face 或其他官方來源)。將下載的檔案整理到本機目錄中。例如:
./mistral-small-4-119b-weights-local/(內含 Mistral Small 4 模型的所有檔案)
將這些目錄上傳至 Cloud Storage bucket,並使用部署資訊清單預期的特定前置字元:
# Upload files for the mistral-small-4 model gcloud storage cp --recursive ./mistral-small-4-119b-weights-local/* gs://${MODEL_BUCKET_NAME}這個指令結構可確保模型檔案位於
gs://${MODEL_BUCKET_NAME}/config.json等路徑。
設定 Cloud Storage 存取權
如要允許 Kubernetes Pod 存取含有模型權重的 Cloud Storage bucket,請設定 GKE 適用的 Workload Identity Federation for GKE。
建立 Kubernetes ServiceAccount:
kubectl create serviceaccount ${KUBERNETES_SERVICEACCOUNT} --namespace ${NAMESPACE}確認 ServiceAccount 是否存在:
kubectl get serviceaccounts --namespace ${NAMESPACE}將 bucket 的 Storage 物件檢視者角色 (
roles/storage.objectViewer) 授予 Kubernetes ServiceAccount:if [[ $PROJECT_ID == *:* ]]; then DOMAIN=$(echo $PROJECT_ID | cut -d: -f1) PROJ_NAME=$(echo $PROJECT_ID | cut -d: -f2) export WI_MEMBER="principal://iam.googleapis.com/projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/${PROJ_NAME}.${DOMAIN}.s3ns.svc.id.goog/subject/ns/${NAMESPACE}/sa/${KUBERNETES_SERVICEACCOUNT}" else export WI_MEMBER="principal://iam.googleapis.com/projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/${PROJECT_ID}.s3ns.svc.id.goog/subject/ns/${NAMESPACE}/sa/${KUBERNETES_SERVICEACCOUNT}" fi gcloud storage buckets add-iam-policy-binding gs://${MODEL_BUCKET_NAME} \ --member="${WI_MEMBER}" \ --role="roles/storage.objectViewer" \ --project=${PROJECT_ID} \ --condition=None
在 vLLM 上部署 Mistral Small 4 模型
如要部署 Mistral Small 4 模型,請為每個模型建立 Cloud Storage 值區來儲存模型權重,並為所選模型大小套用 Kubernetes Deployment 資訊清單。Deployment 是 Kubernetes API 物件,可讓您執行多個 Pod 副本,這些副本會分散在叢集的節點中。
程序
套用這個資訊清單會提取 vLLM 容器映像檔、要求 NVIDIA GPU,並自動連線至 Cloud Storage bucket 中的模型權重,啟動 vLLM 推論引擎。
Mistral Small 4
請按照下列指示部署 Mistral Small 4 指令調整模型。
建立下列
vllm-mistral-small.yaml資訊清單:apiVersion: cloud.google.com/v1 kind: ComputeClass metadata: name: a3-edgegpu-8g-nolssd spec: priorities: - machineType: a3-edgegpu-8g-nolssd gpu: count: 8 type: nvidia-h100-80gb nodePoolAutoCreation: enabled: true --- apiVersion: apps/v1 kind: Deployment metadata: name: vllm-mistral-deployment spec: replicas: 1 selector: matchLabels: app: mistral-server template: metadata: labels: app: mistral-server ai.gke.io/model: mistral-small-4-119b-weights ai.gke.io/inference-server: vllm examples.ai.gke.io/source: user-guide spec: containers: - name: inference-server image: us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:gemma4 resources: requests: cpu: "48" memory: "200Gi" ephemeral-storage: "250Gi" nvidia.com/gpu: "2" limits: cpu: "48" memory: "200Gi" ephemeral-storage: "250Gi" nvidia.com/gpu: "2" command: ["./entrypoint.sh"] # Use the image's entrypoint args: - "python" - "-m" - "vllm.entrypoints.api_server" - "--host=0.0.0.0" - "--port=8080" - "--model=gs://mistral-small-4-119b-weights" # YOUR Cloud Storage PATH - "--tensor-parallel-size=2" - "--enable-log-requests" - "--enable-chunked-prefill" - "--enable-prefix-caching" - "--enable-auto-tool-choice" - "--generation-config=auto" - "--tool-call-parser=mistral" - "--dtype=bfloat16" - "--max-num-seqs=256" - "--max-model-len=8192" - "--gpu-memory-utilization=0.90" - "--reasoning-parser=mistral" - "--trust-remote-code" ports: - containerPort: 8080 env: - name: GOOGLE_CLOUD_UNIVERSE_DOMAIN value: "" - name: CLOUDSDK_CORE_UNIVERSE_DOMAIN value: "" - name: GCS_URI_ARG_KEY value: "model" - name: GCS_URI_ENV_KEY value: "AIP_STORAGE_URI" - name: LORA_ADAPTER_ARG_KEY value: "lora-modules" - name: HF_HUB_ENABLE_HF_TRANSFER value: "1" volumeMounts: - mountPath: /dev/shm name: dshm volumes: - name: dshm emptyDir: medium: Memory nodeSelector: cloud.google.com/compute-class: a3-edgegpu-8g-nolssd --- apiVersion: v1 kind: Service metadata: name: llm-service spec: selector: app: mistral-server type: ClusterIP ports: - protocol: TCP port: 8080 targetPort: 8080套用資訊清單:
kubectl apply -f vllm-mistral-small.yaml如要限制脈絡窗口大小為 16K,可以使用 vLLM 選項
--max-model-len=16384。 如要擴大脈絡窗口大小 (最多 128K),請調整資訊清單和節點集區設定,增加 GPU 容量。
驗證
等待部署作業完成:
kubectl wait --for=condition=Available --timeout=1800s deployment/vllm-mistral-deployment查看執行中 Deployment 的記錄:
kubectl logs -f -l app=mistral-serverDeployment 資源會下載 Mistral Small 4 模型資料。這項程序會在幾分鐘內完成。輸出結果會與下列內容相似:
... ... (APIServer pid=1) INFO: Started server process [1] (APIServer pid=1) INFO: Waiting for application startup. (APIServer pid=1) INFO: Application startup complete.
部署作業完成後,請設定連接埠轉送,與模型互動。
提供模型
在本節中,您將與模型互動。請先確認模型已完全下載,再繼續操作。
設定通訊埠轉送
執行下列指令,設定通訊埠轉送至模型:
kubectl port-forward svc/llm-service 8080:8080 --namespace default &
輸出結果會與下列內容相似:
Forwarding from 127.0.0.1:8080 -> 8080
使用 curl 與模型互動
本節說明如何執行基本冒煙測試,驗證部署的 Mistral 指令微調模型。如果是其他模型,請將 mistral-small-4-119b-weights 替換為相應模型的名稱。
這個範例說明如何使用純文字輸入內容,測試 Mistral 指令微調模型。
在新的終端機工作階段中,使用 curl 與模型對話:
curl http://127.0.0.1:8080/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "mistralai/Mistral-Small-4-119B-2603-eagle",
"prompt": "What is the capital of France?",
"max_tokens": 50,
"temperature": 0.7
}'
輸出看起來類似以下內容:
{
"id": "cmpl-b5d649b6a2d7a330",
"object": "text_completion",
"created": 1781137490,
"model": "openapi",
"choices": [
{
"index": 0,
"text": " This question is likely to have been asked millions of times,
and the answer is always the same: Paris. But why is Paris the capital
of France? The answer to this question is not as simple as
it may seem, as it involves a complex",
"logprobs": null,
"finish_reason": "length",
"stop_reason": null,
"token_ids": null,
"prompt_logprobs": null,
"prompt_token_ids": null
}
],
"service_tier": null,
"system_fingerprint": null,
"usage": {
"prompt_tokens": 8,
"total_tokens": 58,
"completion_tokens": 50,
"prompt_tokens_details": null
},
"kv_transfer_params": null
}
排解問題
- 如果收到
Empty reply from server訊息,可能是容器尚未完成下載模型資料。再次檢查 Pod 的記錄,確認是否出現Connected訊息,這表示模型已可提供服務。 - 如果看到
Connection refused,請確認連接埠轉送功能是否已啟用。
觀察模型成效
如要查看模型的觀測指標資訊主頁,請按照下列步驟操作:
前往 Cloud de Confiance 控制台的「Deployed Models」(已部署模型) 頁面。
如要查看特定部署作業的詳細資料,包括指標、記錄和資訊主頁,請按一下清單中的模型名稱。
在模型詳細資料頁面中,按一下「可觀測性」分頁標籤,即可查看下列資訊主控台。如果系統提示,請點選「啟用」,為叢集啟用指標收集功能。
- 「基礎架構使用情形」資訊主頁會顯示使用率指標。
- 「DCGM」DCGM資訊主頁會顯示 DCGM 指標。
- 如果您使用 vLLM,則可使用「模型效能」資訊主頁,查看 vLLM 模型效能指標。
您也可以在 Cloud Monitoring 中,透過 vLLM 資訊主頁整合功能查看指標。這些指標會匯總所有 vLLM 部署作業,且沒有預設篩選器
vLLM 預設會以 Prometheus 格式公開指標,因此您不需要安裝額外的匯出工具。如要瞭解如何使用 Google Cloud Managed Service for Prometheus 收集模型指標,請參閱 Cloud Monitoring 說明文件中的 vLLM 可觀測性指南。清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除已部署的資源
如要避免系統向您的 Cloud de Confiance 帳戶收取本指南所建立資源的費用,請執行下列指令:
gcloud container clusters delete CLUSTER_NAME \
--location=REGION
替換下列值:
REGION:叢集所在的區域。CLUSTER_NAME:叢集名稱。
後續步驟
- 進一步瞭解 GKE 中的 GPU。
- 瞭解如何在 Autopilot 中部署 GPU 工作負載。
- 探索 vLLM GitHub 存放區和說明文件。
- 探索 Vertex AI Model Garden。
- 瞭解如何運用 GKE 平台的自動化調度管理功能,執行最佳化的 AI/機器學習工作負載。