在本指南中,您將使用 vLLM 服務架構,在 GKE 上透過單一主機 GPU 節點部署及提供 LLM。本指南提供部署下列開放模型的說明和設定:
本指南適用於機器學習 (ML) 工程師,以及有興趣探索 Kubernetes 容器調度功能,以提供開放模型進行推論的資料和 AI 專家。如要進一步瞭解 Google Cloud 內容中提及的常見角色和範例工作,請參閱「常見的 GKE 使用者角色和工作」。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the required APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the required APIs.
-
Make sure that you have the following role or roles on the project: roles/artifactregistry.admin, roles/browser, roles/compute.networkAdmin, roles/container.clusterAdmin, roles/iam.serviceAccountAdmin, roles/resourcemanager.projectIamAdmin, and roles/serviceusage.serviceUsageAdmin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往 IAM - 選取專案。
- 按一下 「授予存取權」。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
- 建立 Hugging Face 帳戶。
- 確認專案有足夠的 GPU 配額。詳情請參閱「分配配額」。
- 依序點選「Your Profile」(你的個人資料) >「Settings」(設定) >「Access Tokens」(存取權杖)。
- 選取「New Token」。
- 指定您選擇的「名稱」,以及至少「讀取」的「角色」。
- 選取「產生權杖」。
- 將產生的權杖複製到剪貼簿。
在 Cloud Shell 中,複製下列存放區:
git clone https://github.com/GoogleCloudPlatform/accelerated-platforms --branch hf-model-tutorial && \ cd accelerated-platforms && \ export ACP_REPO_DIR="$(pwd)"
設定環境變數:
export TF_VAR_platform_default_project_id=PROJECT_ID export HF_TOKEN_READ=HF_TOKEN
替換下列值:
PROJECT_ID
:您的 Google Cloud 專案 ID。HF_TOKEN
:您先前產生的 Hugging Face 權杖。
本指南需要使用 Terraform 1.8.0 以上版本。Cloud Shell 預設安裝 Terraform v1.5.7。
如要在 Cloud Shell 中更新 Terraform 版本,可以執行下列指令碼。這個指令碼會安裝
terraform-switcher
工具,並變更殼層環境。"${ACP_REPO_DIR}/tools/bin/install_terraform.sh" source ~/.bashrc
執行下列部署指令碼。部署指令碼會啟用必要的 Google Cloud API,並為本指南佈建必要的基礎架構。包括新的 VPC 網路、具有私有節點的 GKE 叢集,以及其他支援資源。指令碼可能需要幾分鐘才能完成。
您可以在 GKE Autopilot 或 Standard 叢集中,使用 GPU 提供模型。Autopilot 叢集提供全代管的 Kubernetes 體驗。如要進一步瞭解如何為工作負載選擇最合適的 GKE 作業模式,請參閱「關於 GKE 作業模式」。
Autopilot
"${ACP_REPO_DIR}/platforms/gke/base/tutorials/hf-gpu-model/deploy-ap.sh"
標準
"${ACP_REPO_DIR}/platforms/gke/base/tutorials/hf-gpu-model/deploy-standard.sh"
這個指令碼執行完畢後,您就會擁有可執行推論工作負載的 GKE 叢集。
執行下列指令,從共用設定檔設定環境變數:
source "${ACP_REPO_DIR}/platforms/gke/base/use-cases/inference-ref-arch/terraform/_shared_config/scripts/set_environment_variables.sh"
部署指令碼會在 Secret Manager 中建立密鑰,用來儲存 Hugging Face 權杖。部署叢集前,您必須手動將權杖新增至這個密鑰。在 Cloud Shell 中執行下列指令,將權杖新增至 Secret Manager。
echo ${HF_TOKEN_READ} | gcloud secrets versions add ${huggingface_hub_access_token_read_secret_manager_secret_name} \ --data-file=- \ --project=${huggingface_secret_manager_project_id}
為要部署的模型設定環境變數:
Gemma 3 27B-it
export ACCELERATOR_TYPE="h100" export HF_MODEL_ID="google/gemma-3-27b-it"
Llama 4 Scout 17B-16E-Instruct
export ACCELERATOR_TYPE="h100" export HF_MODEL_ID="meta-llama/llama-4-scout-17b-16e-instruct"
Qwen3 32B
export ACCELERATOR_TYPE="h100" export HF_MODEL_ID="qwen/qwen3-32b"
gpt-oss 20B
export ACCELERATOR_TYPE="h100" export HF_MODEL_ID="openai/gpt-oss-20b"
如需其他設定,包括其他模型變體和 GPU 類型,請參閱
accelerated-platforms
GitHub 存放區中的資訊清單。從部署作業中取得環境變數。這些環境變數包含您佈建基礎架構時的必要設定詳細資料。
source "${ACP_REPO_DIR}/platforms/gke/base/use-cases/inference-ref-arch/terraform/_shared_config/scripts/set_environment_variables.sh"
執行下列指令碼,設定 Kubernetes 工作,將模型下載至 Cloud Storage:
"${ACP_REPO_DIR}/platforms/gke/base/use-cases/inference-ref-arch/kubernetes-manifests/model-download/configure_huggingface.sh"
部署模型下載作業:
kubectl apply --kustomize "${ACP_REPO_DIR}/platforms/gke/base/use-cases/inference-ref-arch/kubernetes-manifests/model-download/huggingface"
等待下載作業完成。監控 Job 的狀態,並在
COMPLETIONS
為1/1
時按下Ctrl+C
鍵結束。watch --color --interval 5 --no-title "kubectl --namespace=${huggingface_hub_downloader_kubernetes_namespace_name} get job/${HF_MODEL_ID_HASH}-hf-model-to-gcs
將推論工作負載部署至 GKE 叢集。
"${ACP_REPO_DIR}/platforms/gke/base/use-cases/inference-ref-arch/kubernetes-manifests/online-inference-gpu/configure_deployment.sh" kubectl apply --kustomize "${ACP_REPO_DIR}/platforms/gke/base/use-cases/inference-ref-arch/kubernetes-manifests/online-inference-gpu/vllm/${ACCELERATOR_TYPE}-${HF_MODEL_NAME}"
等待推論伺服器 Pod 準備就緒。當「�」欄為「�」時,請按 � 鍵退出。
READY
1/1
Ctrl+C
watch --color --interval 5 --no-title "kubectl --namespace=${ira_online_gpu_kubernetes_namespace_name} get deployment/vllm-${ACCELERATOR_TYPE}-${HF_MODEL_NAME}"
執行下列指令碼,設定通訊埠轉送功能,並將範例要求傳送至模型。本範例使用 Gemma 3 27b-it 模型的酬載格式。
kubectl --namespace=${ira_online_gpu_kubernetes_namespace_name} port-forward service/vllm-${ACCELERATOR_TYPE}-${HF_MODEL_NAME} 8000:8000 >/dev/null & PF_PID=$! curl http://127.0.0.1:8000/v1/chat/completions \ --data '{ "model": "/gcs/'${HF_MODEL_ID}'", "messages": [ { "role": "user", "content": "What is GKE?" } ] }' \ --header "Content-Type: application/json" \ --request POST \ --show-error \ --silent | jq kill -9 ${PF_PID}
模型應會傳回 JSON 回覆,回答您的問題。
刪除推論工作負載:
kubectl delete --ignore-not-found --kustomize "${ACP_REPO_DIR}/platforms/gke/base/use-cases/inference-ref-arch/kubernetes-manifests/online-inference-gpu/vllm/${ACCELERATOR_TYPE}-${HF_MODEL_NAME}"
移除基礎 GKE 叢集:
Autopilot
"${ACP_REPO_DIR}/platforms/gke/base/tutorials/hf-gpu-model/teardown-ap.sh"
標準
"${ACP_REPO_DIR}/platforms/gke/base/tutorials/hf-gpu-model/teardown-standard.sh"
- 進一步瞭解 GKE 的 AI/機器學習模型推論。
- 使用 GKE Inference Quickstart 工具分析模型推論效能和成本。
- 在 GitHub 上探索其他以這個架構為基礎的用途和模式。
取得模型存取權
如要透過 Hugging Face 存取模型,必須提供 Hugging Face 權杖。
如要產生新權杖 (如果沒有),請按照下列步驟操作:
佈建 GKE 推論環境
在本節中,您將部署提供模型服務所需的基礎架構。
啟動 Cloud Shell
本指南使用 Cloud Shell 執行指令。Cloud Shell 已預先安裝必要工具,包括
gcloud
、kubectl
和git
。在 Google Cloud 控制台中,啟動 Cloud Shell 執行個體:
這項操作會在 Google Cloud 控制台的底部窗格啟動工作階段。
部署基礎架構
如要佈建 GKE 叢集和存取 Hugging Face 模型所需的資源,請按照下列步驟操作:
部署開放式模型
現在可以下載及部署模型了。
測試部署作業
清除所用資源
為避免產生費用,請刪除您建立的所有資源。
後續步驟
-