在本指南中,您将使用 vLLM 服务框架在 GKE 上部署 LLM 并提供服务,所用的是单主机 GPU 节点。本指南提供了有关部署以下开放模型的说明和配置:
本指南适用于机器学习 (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 - 选择项目。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
- 创建 Hugging Face 账号。
- 确保您的项目具有足够的 GPU 配额。如需了解详情,请参阅分配配额。
- 依次点击您的个人资料 > 设置 > 访问令牌。
- 选择新建令牌 (New Token)。
- 指定您选择的名称和一个至少为读取的角色。
- 选择生成令牌。
- 将生成的令牌复制到剪贴板。
在 Cloud Shell 中,克隆以下 Git 代码库:
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
工具并更改您的 shell 环境。"${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 中创建一个 Secret 来存储您的 Hugging Face 令牌。您必须先手动将令牌添加到此 Secret,然后再部署集群。在 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"
运行以下脚本以配置将模型下载到 Cloud Storage 的 Kubernetes 作业:
"${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"
等待下载完成。监控作业的状态,当
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 模型所需的资源,请按以下步骤操作:
部署开放模型
现在,您可以下载并部署模型了。
测试部署
清理
为避免产生费用,请删除您创建的所有资源。
后续步骤
-