如何使用 vLLM 在 Cloud Run GPU 上运行 LLM 推理

1. 简介

概览

Cloud Run 是 Google Cloud 上的一个容器平台,可让您轻松在容器中运行代码,而无需管理集群。

Cloud Run 提供 L4 或 NVIDIA RTX PRO 6000 Blackwell GPU。每个 Cloud Run 实例有一个 GPU,并且 Cloud Run 自动扩缩功能仍然适用,包括在没有请求时缩减至零个实例。

GPU 的一个应用场景是运行您自己的开放大语言模型 (LLM)。本教程将引导您完成部署运行 LLM 的服务。

此 Codelab 介绍了如何使用预构建容器和 vLLM 推理库在 Cloud Run 上部署 Gemma 4 开放模型

学习内容

  • 如何在 Cloud Run 上使用 GPU。
  • 如何使用 vLLM 作为推理引擎,在 Cloud Run 上部署 Google 的 Gemma 4 2B 指令调优模型。

2. 设置和要求

前提条件

3. 启用 API 并设置环境变量

启用 API

在开始使用此 Codelab 之前,您需要启用多个 API。此 Codelab 需要使用以下 API。您可以运行以下命令来启用这些 API:

gcloud services enable run.googleapis.com \
    cloudbuild.googleapis.com \
    artifactregistry.googleapis.com

设置环境变量

在下方配置项目 ID。

export PROJECT_ID=<YOUR_PROJECT_ID>

export REGION=europe-west4
export SERVICE_NAME=gemma4-cr-codelab
export SERVICE_ACCOUNT_NAME=gemma4-cr-sa
export SERVICE_ACCOUNT_ADDRESS=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com

4. 创建服务账号

此服务账号用作 Cloud Run 服务身份。

gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME \
  --display-name="Cloud Run gemma 4 SA"

5. 部署服务

如需在 Cloud Run 上部署 Gemma 模型,请将以下 gcloud CLI 命令与建议的设置搭配使用:

CONTAINER_ARGS=(
    "serve"
    "google/gemma-4-E2B-it"
    "--enable-chunked-prefill"
    "--enable-prefix-caching"
    "--generation-config=auto"
    "--enable-auto-tool-choice"
    "--tool-call-parser=gemma4"
    "--reasoning-parser=gemma4"
    "--dtype=bfloat16"
    "--max-num-seqs=64"
    "--gpu-memory-utilization=0.95"
    "--tensor-parallel-size=1"
    "--port=8080"
    "--host=0.0.0.0"
)
gcloud beta run deploy $SERVICE_NAME \
    --image "us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:gemma4" \
    --project $PROJECT_ID \
    --region $REGION \
    --execution-environment gen2 \
    --no-allow-unauthenticated \
    --cpu 20 \
    --memory 80Gi \
    --gpu 1 \
    --gpu-type nvidia-rtx-pro-6000 \
    --no-gpu-zonal-redundancy \
    --no-cpu-throttling \
    --max-instances 3 \
    --concurrency 64 \
    --timeout 600 \
    --service-account $SERVICE_ACCOUNT_ADDRESS \
    --startup-probe tcpSocket.port=8080,initialDelaySeconds=240,failureThreshold=1,timeoutSeconds=240,periodSeconds=240 \
    --command "vllm" \
    --args=$(IFS=','; echo "${CONTAINER_ARGS[*]}")

6. 测试服务

部署完成后,您可以使用 Cloud Run 开发代理服务(该服务会自动为您添加 ID 令牌),也可以直接使用 curl 命令访问服务网址。

使用 Cloud Run 开发代理服务

首先,启动代理

gcloud run services proxy $SERVICE_NAME \
  --project $PROJECT \
  --region $REGION \
  --port=9090

在单独的终端标签页中运行以下命令以发送请求,让代理保持运行状态。代理在 localhost:9090 上运行

curl http://localhost:9090/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "google/gemma-4-E2B-it",
    "messages": [{"role": "user", "content": "Why is the sky blue?"}],
    "chat_template_kwargs": {
         "enable_thinking": true
     },
     "skip_special_tokens": false
  }'

您应看到类似于以下内容的输出:

{
 "id": "chatcmpl-9cf1ab1450487047",
 "object": "chat.completion",
 "created": 1774904187,
 "model": "google/gemma-4-E2B-it",
 "choices": [
   {
     "index": 0,
     "message": {
       "role": "assistant",
       "content": "The short answer is a phenomenon called **Rayleigh scattering**...",
       "function_call": null,
       "tool_calls": [],
       "reasoning": "*   Question: \"Why is the sky blue?\"\n..."
     },
     "finish_reason": "stop",
     "stop_reason": 106
   }
 ],
 "usage": {
   "prompt_tokens": 21,
   "total_tokens": 877,
   "completion_tokens": 856
 }
}

直接使用服务网址

首先,检索已部署服务的网址。

SERVICE_URL=$(gcloud run services describe $SERVICE_NAME --region $REGION --format 'value(status.url)')

使用 curl 命令调用服务

curl $SERVICE_URL/v1/chat/completions \
  -H "Authorization: bearer $(gcloud auth print-identity-token)" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "google/gemma-4-E2B-it",
    "messages": [{"role": "user", "content": "Why is the sky blue?"}],
    "chat_template_kwargs": {
         "enable_thinking": true
     },
     "skip_special_tokens": false
  }'

7. 恭喜!

恭喜您完成此 Codelab!

建议您查看 Cloud Run 文档

所学内容

  • 如何在 Cloud Run 上使用 GPU。
  • 如何使用 vLLM 作为推理引擎在 Cloud Run 上部署 Google 的 Gemma 4 (2B) 模型。

8. 清理

为避免意外产生费用(例如,如果 Cloud Run 服务的调用次数意外超过免费层级中每月 Cloud Run 调用次数的分配额),您可以删除 Cloud Run 或删除您在第 2 步中创建的项目。

如需删除 Cloud Run 服务,请前往 Cloud Run Cloud 控制台 (https://console.cloud.google.com/run),然后删除 gemma4-cr-codelab 服务。您可能还想删除 gemma4-cr-codelab-sa 服务账号。

如果您选择删除整个项目,可以前往 https://console.cloud.google.com/cloud-resource-manager,选择您在第 2 步中创建的项目,然后选择“删除”。如果您删除项目,则需要在 Cloud SDK 中更改项目。您可以运行 gcloud projects list 查看所有可用项目的列表。