如要在 Google Kubernetes Engine (GKE) 上使用 GPU,透過 vLLM 框架提供 Gemma 4 大型語言模型 (LLM),您必須使用支援的加速器 (例如 NVIDIA B200、H100、RTX Pro 6000 或 L4 GPU) 佈建 GKE 叢集。
如要取得 Gemma 4 模型權重,您可以設定預先建構的 vLLM 容器,從 Hugging Face 存放區下載權重。或者,容器可以從現有的永久儲存空間載入模型權重,例如在 Google Cloud Managed Lustre 執行個體上快取 Cloud Storage 模型 bucket。
載入權重後,vLLM 容器會公開與 OpenAI 相容的 API 端點,以進行高處理量推論。
本教學課程的適用對象為機器學習 (ML) 工程師、平台管理員和營運人員,以及有興趣使用 Kubernetes 容器自動化調度管理功能,在 H200、H100、A100 和 L4 GPU 硬體上提供 AI/ML 工作負載服務的資料和 AI 專家。如要進一步瞭解內容中提及的常見角色和範例工作,請參閱「常見的 GKE 使用者角色和工作」。 Google Cloud
如果您需要整合式代管 AI 平台,以經濟實惠的方式快速建構及提供機器學習模型,建議您試用 Vertex AI 部署解決方案。
閱讀本頁面之前,請先熟悉下列項目:
背景
本節說明本指南中使用的重要技術。
GPU
GPU 可加速處理節點上執行的特定工作負載,例如機器學習和資料處理。GKE 提供各種機器類型選項,可供節點設定使用,包括搭載 NVIDIA H200、H100、L4 和 A100 GPU 的機器類型。
vLLM
vLLM 是經過高度最佳化的開放原始碼 LLM 服務架構,可提升 GPU 的服務處理量,並提供下列功能:
- 使用 PagedAttention 實作最佳化轉換器
- 持續批次處理,提升整體放送輸送量
- 在多個 GPU 上進行張量平行處理和分散式服務
詳情請參閱 vLLM 說明文件。
目標
本教學課程提供基礎知識,協助您瞭解及探索如何在 Kubernetes 代管環境中,實際部署 LLM 以進行推論。
- 在 Autopilot 或標準模式中,使用 GKE 叢集準備環境。
- 將 vLLM 容器部署至叢集。
- 使用 vLLM 透過 curl 和網頁即時通訊介面提供 Gemma 4 模型。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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 Google Cloud project.
Enable the required API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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 Google Cloud project.
Enable the required API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
確認您在專案中具備下列角色: roles/container.admin、roles/iam.serviceAccountAdmin
檢查角色
-
前往 Google Cloud 控制台的「IAM」頁面。
前往「IAM」頁面 - 選取專案。
-
在「主體」欄中,找出所有識別您或您所屬群組的資料列。如要瞭解自己所屬的群組,請與管理員聯絡。
- 針對指定或包含您的所有列,請檢查「角色」欄,確認角色清單是否包含必要角色。
授予角色
-
前往 Google Cloud 控制台的「IAM」頁面。
前往「IAM」頁面 - 選取專案。
- 按一下「Grant access」(授予存取權)。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是指 Google 帳戶的電子郵件地址。
- 按一下「選取角色」,然後搜尋角色。
- 如要授予其他角色,請按一下「Add another role」(新增其他角色),然後新增其他角色。
- 按一下「Save」(儲存)。
-
準備環境
在本教學課程中,您將使用 Cloud Shell 管理Google Cloud上託管的資源。Cloud Shell 已預先安裝本教學課程所需的軟體,包括 kubectl 和 gcloud CLI。
如要使用 Cloud Shell 設定環境,請按照下列步驟操作:
在 Google Cloud 控制台中,按一下
Google Cloud 控制台中的「啟用 Cloud Shell」,啟動 Cloud Shell 工作階段。系統會在 Google Cloud 控制台的底部窗格啟動工作階段。
設定預設環境變數:
gcloud config set project PROJECT_ID gcloud config set billing/quota_project PROJECT_ID export PROJECT_ID=$(gcloud config get project) export REGION=REGION export ZONE=ZONE export CLUSTER_NAME=CLUSTER_NAME替換下列值:
PROJECT_ID:您的 Google Cloud 專案 ID。REGION:支援您要使用的加速器類型,例如 L4 GPU 的us-central1。您可以查看哪個區域提供哪些 GPU。ZONE:支援您要使用的加速器類型,例如 RTX PRO 6000 GPU 的us-central1-b和us-central1-f。您可以查看哪些區域提供哪些 GPU。CLUSTER_NAME:叢集名稱。
建立及設定 Google Cloud 資源
請按照下列操作說明建立必要資源。
建立 GKE 叢集和節點集區
您可以在 GKE Autopilot 或 Standard 叢集的 GPU 上提供 Gemma 服務。建議您使用 Autopilot 叢集,享有全代管 Kubernetes 體驗。如要為工作負載選擇最合適的 GKE 作業模式,請參閱「選擇 GKE 作業模式」。
Autopilot
在 Cloud Shell 中執行下列指令:
gcloud container clusters create-auto CLUSTER_NAME \
--project=PROJECT_ID \
--location=CONTROL_PLANE_LOCATION \
--release-channel=rapid
替換下列值:
PROJECT_ID:您的 Google Cloud 專案 ID。CONTROL_PLANE_LOCATION:叢集控制層的 Compute Engine 區域。提供支援您要使用加速器類型的區域,例如 L4 GPU 的us-central1。CLUSTER_NAME:叢集名稱。
GKE 會根據部署的工作負載要求,建立含 CPU 和 GPU 節點的 Autopilot 叢集。
標準
在 Cloud Shell 中執行下列指令,建立 Standard 叢集:
gcloud container clusters create CLUSTER_NAME \ --project=PROJECT_ID \ --location=CONTROL_PLANE_LOCATION \ --workload-pool=PROJECT_ID.svc.id.goog \ --release-channel=rapid \ --num-nodes=1替換下列值:
PROJECT_ID:您的 Google Cloud 專案 ID。CONTROL_PLANE_LOCATION:叢集控制層的 Compute Engine 區域。提供支援您要使用加速器類型的區域,例如 L4 GPU 的us-central1。CLUSTER_NAME:叢集名稱。
建立叢集可能需要幾分鐘的時間。
如要為叢集建立節點集區,並設定適當的磁碟大小,請執行下列指令:
gcloud container node-pools create gpupool \ --accelerator type=nvidia-rtx-pro-6000,count=1,gpu-driver-version=latest \ --project=PROJECT_ID \ --location=REGION \ --node-locations=ZONE \ --cluster=CLUSTER_NAME \ --machine-type=g4-standard-48 \ --num-nodes=1GKE 會建立包含 RTX PRO 的單一節點集區。每個節點 6000 個 GPU。
使用 Hugging Face 權重,在 vLLM 上部署 Gemma 4 模型
如要使用 Hugging Face 權重部署 Gemma 4 模型,請為所選模型大小套用 Kubernetes 部署資訊清單。Deployment 是 Kubernetes API 物件,可讓您執行多個 Pod 副本,並將這些副本分配到叢集中的節點。
程序
套用這個資訊清單會提取 vLLM 容器映像檔、要求 NVIDIA GPU,並自動從 Hugging Face 下載權重,以啟動 vLLM 推論引擎。
Gemma 4 E2B-it
請按照下列操作說明部署 Gemma 4 E2B 指令微調模型 (僅限文字輸入)。
建立下列
vllm-4-e2b-it.yaml資訊清單:apiVersion: apps/v1 kind: Deployment metadata: name: vllm-gemma-deployment spec: replicas: 1 selector: matchLabels: app: gemma-server template: metadata: labels: app: gemma-server ai.gke.io/model: gemma-4-e2b-it 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: "2" memory: "10Gi" ephemeral-storage: "10Gi" nvidia.com/gpu: "1" limits: cpu: "2" memory: "10Gi" ephemeral-storage: "10Gi" nvidia.com/gpu: "1" command: ["python3", "-m", "vllm.entrypoints.api_server"] args: - --model=$(MODEL_ID) - --host=0.0.0.0 - --port=8000 - --tensor-parallel-size=1 - --enable-log-requests - --enable-chunked-prefill - --enable-prefix-caching - --enable-auto-tool-choice - --generation-config=auto - --tool-call-parser=gemma4 - --dtype=bfloat16 - --max-num-seqs=16 - --max-model-len=32768 - --gpu-memory-utilization=0.95 - --reasoning-parser=gemma4 - --trust-remote-code env: - name: LD_LIBRARY_PATH value: ${LD_LIBRARY_PATH}:/usr/local/nvidia/lib64 - name: MODEL_ID value: google/gemma-4-E2B-it volumeMounts: - mountPath: /dev/shm name: dshm volumes: - name: dshm emptyDir: medium: Memory nodeSelector: cloud.google.com/gke-accelerator: nvidia-rtx-pro-6000 cloud.google.com/gke-gpu-driver-version: latest --- apiVersion: v1 kind: Service metadata: name: llm-service spec: selector: app: gemma-server type: ClusterIP ports: - protocol: TCP port: 8000 targetPort: 8000套用資訊清單:
kubectl apply -f vllm-4-e2b-it.yaml
Gemma 4 E4B-it
請按照下列操作說明部署 Gemma 4 E4B 指令微調模型。
建立下列
vllm-4-e4b-it.yaml資訊清單:apiVersion: apps/v1 kind: Deployment metadata: name: vllm-gemma-deployment spec: replicas: 1 selector: matchLabels: app: gemma-server template: metadata: labels: app: gemma-server ai.gke.io/model: gemma-4-e4b-it 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: "4" memory: "20Gi" ephemeral-storage: "20Gi" nvidia.com/gpu: "1" limits: cpu: "4" memory: "20Gi" ephemeral-storage: "20Gi" nvidia.com/gpu: "1" command: ["python3", "-m", "vllm.entrypoints.api_server"] args: - --model=$(MODEL_ID) - --host=0.0.0.0 - --port=8000 - --tensor-parallel-size=1 - --enable-log-requests - --enable-chunked-prefill - --enable-prefix-caching - --enable-auto-tool-choice - --generation-config=auto - --tool-call-parser=gemma4 - --dtype=bfloat16 - --max-num-seqs=16 - --max-model-len=32768 - --gpu-memory-utilization=0.95 - --reasoning-parser=gemma4 - --trust-remote-code env: - name: LD_LIBRARY_PATH value: ${LD_LIBRARY_PATH}:/usr/local/nvidia/lib64 - name: MODEL_ID value: google/gemma-4-E4b-it volumeMounts: - mountPath: /dev/shm name: dshm volumes: - name: dshm emptyDir: medium: Memory nodeSelector: cloud.google.com/gke-accelerator: nvidia-rtx-pro-6000 cloud.google.com/gke-gpu-driver-version: latest --- apiVersion: v1 kind: Service metadata: name: llm-service spec: selector: app: gemma-server type: ClusterIP ports: - protocol: TCP port: 8000 targetPort: 8000套用資訊清單:
kubectl apply -f vllm-4-E4b-it.yaml在我們的範例中,我們使用 vLLM 選項
--max-model-len=32768,將脈絡窗口限制為 32, 000 個詞元。 如要使用較大的脈絡窗口大小 (最多 128K),請調整資訊清單和節點集區設定,增加 GPU 容量。
Gemma 4 26B-A4B-it
請按照下列操作說明,部署 Gemma 4 26B-A4B 指令微調模型。
建立下列
vllm-4-26b-a4b-it.yaml資訊清單:apiVersion: apps/v1 kind: Deployment metadata: name: vllm-gemma-deployment spec: replicas: 1 selector: matchLabels: app: gemma-server template: metadata: labels: app: gemma-server ai.gke.io/model: gemma-4-26b-a4b-it 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: "20" memory: "80Gi" ephemeral-storage: "80Gi" nvidia.com/gpu: "1" limits: cpu: "20" memory: "80Gi" ephemeral-storage: "80Gi" nvidia.com/gpu: "1" command: ["python3", "-m", "vllm.entrypoints.api_server"] args: - --model=$(MODEL_ID) - --host=0.0.0.0 - --port=8000 - --tensor-parallel-size=1 - --enable-log-requests - --enable-chunked-prefill - --enable-prefix-caching - --enable-auto-tool-choice - --generation-config=auto - --tool-call-parser=gemma4 - --dtype=bfloat16 - --max-num-seqs=16 - --max-model-len=16384 - --gpu-memory-utilization=0.95 - --reasoning-parser=gemma4 - --trust-remote-code env: - name: LD_LIBRARY_PATH value: ${LD_LIBRARY_PATH}:/usr/local/nvidia/lib64 - name: MODEL_ID value: google/gemma-4-26B-A4B-it volumeMounts: - mountPath: /dev/shm name: dshm volumes: - name: dshm emptyDir: medium: Memory nodeSelector: cloud.google.com/gke-accelerator: nvidia-rtx-pro-6000 cloud.google.com/gke-gpu-driver-version: latest --- apiVersion: v1 kind: Service metadata: name: llm-service spec: selector: app: gemma-server type: ClusterIP ports: - protocol: TCP port: 8000 targetPort: 8000套用資訊清單:
kubectl apply -f vllm-4-26b-a4b-it.yaml在我們的範例中,我們使用 vLLM 選項
--max-model-len=16384,將內容視窗大小限制為 16K。如要擴大脈絡窗口大小 (最多 128K),請調整資訊清單和節點集區設定,增加 GPU 容量。
Gemma 4 31B-it
請按照下列操作說明部署 Gemma 4 31B 指令微調模型。
建立下列
vllm-4-31b-it.yaml資訊清單:apiVersion: apps/v1 kind: Deployment metadata: name: vllm-gemma-deployment spec: replicas: 1 selector: matchLabels: app: gemma-server template: metadata: labels: app: gemma-server ai.gke.io/model: gemma-4-31b-it 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: "22" memory: "100Gi" ephemeral-storage: "80Gi" nvidia.com/gpu: "1" limits: cpu: "22" memory: "100Gi" ephemeral-storage: "80Gi" nvidia.com/gpu: "1" command: ["python3", "-m", "vllm.entrypoints.api_server"] args: - --model=$(MODEL_ID) - --host=0.0.0.0 - --port=8000 - --tensor-parallel-size=1 - --enable-log-requests - --enable-chunked-prefill - --enable-prefix-caching - --enable-auto-tool-choice - --generation-config=auto - --tool-call-parser=gemma4 - --dtype=bfloat16 - --max-num-seqs=16 - --max-model-len=16384 - --gpu-memory-utilization=0.95 - --reasoning-parser=gemma4 - --trust-remote-code env: - name: LD_LIBRARY_PATH value: ${LD_LIBRARY_PATH}:/usr/local/nvidia/lib64 - name: MODEL_ID value: google/gemma-4-31B-it volumeMounts: - mountPath: /dev/shm name: dshm volumes: - name: dshm emptyDir: medium: Memory nodeSelector: cloud.google.com/gke-accelerator: nvidia-rtx-pro-6000 cloud.google.com/gke-gpu-driver-version: latest --- apiVersion: v1 kind: Service metadata: name: llm-service spec: selector: app: gemma-server type: ClusterIP ports: - protocol: TCP port: 8000 targetPort: 8000套用資訊清單:
kubectl apply -f vllm-4-31b-it.yaml在我們的範例中,我們使用 vLLM 選項
--max-model-len=16384,將內容視窗大小限制為 16K。如要使用較大的脈絡窗口大小 (最多 128K),請調整資訊清單和節點集區設定,增加 GPU 容量。
驗證
等待部署作業完成:
kubectl wait --for=condition=Available --timeout=1800s deployment/vllm-gemma-deployment查看執行中 Deployment 的記錄:
kubectl logs -f -l app=gemma-serverDeployment 資源會下載 Gemma 模型資料。這項程序會在幾分鐘內完成。輸出結果會與下列內容相似:
... ... (APIServer pid=1) INFO: Started server process [1] (APIServer pid=1) INFO: Waiting for application startup. (APIServer pid=1) INFO: Application startup complete.
Hugging Face 部署作業完成後,請設定連接埠轉送,與模型互動。
從 Managed Lustre 部署微調後的 Gemma
如要提供已儲存在 Google Cloud Managed Lustre 執行個體中的微調 Gemma 模型 (例如 Gemma 3 27B) 服務,您必須將對應的 PersistentVolumeClaim (PVC) 掛接至 vLLM 容器。
必要條件
請確認 GKE 叢集中有現有的 PVC,可連線至 Lustre 執行個體。在本範例中,PVC 名為 gemma-lustre-pvc。
如要瞭解如何為現有執行個體建立 PVC 和 PersistentVolume (PV),請參閱「存取現有的 Managed Lustre 執行個體」。
程序
將下列 YAML 資訊清單儲存為
vllm-lustre-gemma.yaml。在本範例中,部署作業會將 Lustre PVC 掛接至/data,並指示 vLLM 從該本機路徑載入模型權重。apiVersion: apps/v1 kind: Deployment metadata: name: vllm-gemma-lustre spec: replicas: 1 selector: matchLabels: app: gemma-server template: metadata: labels: app: gemma-server spec: containers: - name: vllm image: vllm/vllm-openai:latest command: ["python3", "-m", "vllm.entrypoints.openai.api_server"] args: - --model=/data/gemma-3-27b - --tensor-parallel-size=1 resources: limits: nvidia.com/gpu: "1" volumeMounts: - name: model-weights mountPath: /data volumes: - name: model-weights persistentVolumeClaim: claimName: gemma-lustre-pvc nodeSelector: cloud.google.com/gke-accelerator: nvidia-l4 cloud.google.com/gke-gpu-driver-version: latest將資訊清單套用至 GKE 叢集:
kubectl apply -f vllm-lustre-gemma.yaml
驗證
如要確認模型是否已從 Lustre 磁碟區成功載入,請檢查 Pod 記錄中的 vLLM 啟動序列:
kubectl logs -l app=gemma-server
提供模型
在本節中,您將與模型互動。請先確認模型已完全下載,再繼續操作。
設定通訊埠轉送
執行下列指令,設定通訊埠轉送至模型:
kubectl port-forward service/llm-service 8000:8000
輸出結果會與下列內容相似:
Forwarding from 127.0.0.1:8000 -> 8000
使用 curl 與模型互動
本節說明如何執行基本冒煙測試,驗證已部署的 Gemma 4 指令微調模型。如果是其他模型,請將 gemma-4-e4b-it 替換為對應模型的名稱。
本例說明如何使用純文字輸入內容,測試 Gemma 4 E4B 指令微調模型。
在新終端機工作階段中,使用 curl 與模型對話:
curl http://127.0.0.1:8000/v1/chat/completions \
-X POST \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemma-4-26B-A4B-it",
"messages": [
{
"role": "user",
"content": "Why is the sky blue?"
}
],
"chat_template_kwargs": {
"enable_thinking": true
},
"skip_special_tokens": false
}'
輸出看起來類似以下內容:
{
"id": "chatcmpl-be75ccfcbdf753d1",
"object": "chat.completion",
"created": 1775006187,
"model": "google/gemma-4-26B-A4B-it",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The short answer is a phenomenon called **Rayleigh scattering**.\n\nTo understand how it works, you have to look at three things: sunlight, the Earth's atmosphere, and how light travels.\n\n### 1. Sunlight is a Rainbow\nAlthough sunlight looks white to us, it is actually made up of all the colors of the rainbow (red, orange, yellow, green, blue, indigo, and violet). Light travels as **waves**, and each color has a different wavelength:\n* **Red light** travels in long, lazy, wide waves.\n* **Blue and violet light** travel in short, choppy, tight waves.\n\n### 2. The Atmosphere is an Obstacle Course\nEarth's atmosphere is filled with gases (mostly nitrogen and oxygen). As sunlight travels through the atmosphere, it strikes the molecules of these gases. \n\nBecause the gas molecules are very small, they affect the colors differently based on their wavelength:\n* The **long waves** (reds and yellows) pass through the atmosphere mostly straight, without hitting much. They are like large ocean waves that roll right over small pebbles.\n* The **short waves** (blues and violets) strike the gas molecules and get scattered in every direction. They are like small ripples that hit a pebble and splash everywhere.\n\nBecause this blue light is being scattered in every direction by the air, when you look up, your eyes are catching that scattered blue light coming from every part of the sky.\n\n### 3. Why isn't the sky violet?\nIf violet light has an even shorter wavelength than blue light, you might wonder why the sky doesn't look purple. There are two main reasons:\n1. **The Sun's output:** The Sun emits much more blue light than violet light.\n2. **Human Biology:** Human eyes are much more sensitive to blue than to violet. Our eyes interpret the scatter of mixed blue and violet light simply as pale blue.\n\n---\n\n### Bonus: Why are sunsets red?\nWhen the sun is setting, it is much lower on the horizon. This means the sunlight has to travel through a much **thicker** layer of the atmosphere to reach your eyes. \n\nBy the time the light gets to you, the blue light has been scattered away completely로. Only the long-wavelength colors—the reds, oranges, and pinks—are able to make it through that thick layer of air without being scattered away, creating the beautiful colors of a sunset.",
"refusal": null,
"annotations": null,
"audio": null,
"function_call": null,
"tool_calls": [],
"reasoning": "\"Why is the sky blue?\"\nScience/Physics (Atmospheric scattering).\nExplain the phenomenon clearly, accurately, and engagingly.\n\n * Sunlight (white light) is made of a spectrum of colors (ROYGBIV).\n * Earth's atmosphere is filled with gases (nitrogen, oxygen) and particles.\n * Rayleigh Scattering: Shorter wavelengths (blue/violet) scatter more easily than longer wavelengths (red/yellow) when hitting small gas molecules.\n * The human eye's sensitivity: Why isn't it violet?\n\n A good scientific explanation should follow a logical flow:\n * *Direct Answer:* The core mechanism (Rayleigh Scattering).\n * *The Components:* Sunlight and the Atmosphere.\n * *The Mechanism:* How light interacts with gas molecules.\n * *The Wavelength Factor:* Comparing colors.\n * *The \"Wait, why not violet?\" question:* Addressing human perception.\n * *Bonus/Related concept:* Why sunsets are red.\n\n * Use the term **Rayleigh Scattering**.\n * Summarize: Its how sunlight interacts with the Earth's atmosphere.\n\n * Sunlight looks white, but it's actually a mix of all colors (the rainbow).\n * Each color travels as a different wavelength. Red = long/lazy waves; Blue/Violet = short/choppy waves.\n\n * The atmosphere is mostly Nitrogen and Oxygen.\n * When sunlight hits these tiny gas molecules, the light gets scattered in all directions.\n\n * Blue light travels in shorter, smaller waves.\n * Because these waves are small, they strike the gas molecules more frequently and get scattered more easily than the longer red/yellow waves.\n * Result: When you look up, your eyes are catching this \"scattered\" blue light coming from every direction.\n\n * *Technically*, violet light has an even shorter wavelength than blue, so it scatters *even more*. Why isn't the sky violet?\n * Two reasons: 1. The Sun emits more blue light than violet light. 2. Human eyes are much more sensitive to blue than violet.\n\n * Briefly mention sunsets to provide a complete picture.\n * At sunset, light travels through *more* atmosphere. The blue is scattered away completely, leaving only the long red/orange waves to reach your eyes.\n\n * *Tone Check:* Is it too academic? Use analogies (like waves in water or skipping stones) if needed, but keep it concise.\n * *Clarity:* Ensure the distinction between wavelength and scattering is clear."
},
"logprobs": null,
"finish_reason": "stop",
"stop_reason": 106,
"token_ids": null
}
],
"service_tier": null,
"system_fingerprint": null,
"usage": {
"prompt_tokens": 21,
"total_tokens": 1122,
"completion_tokens": 1101,
"prompt_tokens_details": null
},
"prompt_logprobs": null,
"prompt_token_ids": null,
"kv_transfer_params": null
}
(選用) 透過 Gradio 對話介面與模型互動
在本節中,您將建構網頁聊天應用程式,與經過指令微調的模型互動。為求簡潔,本節僅說明使用 4B-it 模型的測試方法。
Gradio 是 Python 程式庫,內含 ChatInterface 包裝函式,可為聊天機器人建立使用者介面。
部署對話介面
在 Cloud Shell 中,將下列資訊清單儲存為
gradio.yaml。將google/gemma-4-E4B-it變更為您在部署作業中使用的 Gemma 4 模型名稱。apiVersion: apps/v1 kind: Deployment metadata: name: gradio labels: app: gradio spec: replicas: 1 selector: matchLabels: app: gradio template: metadata: labels: app: gradio spec: containers: - name: gradio image: us-docker.pkg.dev/google-samples/containers/gke/gradio-app:v1.0.7 resources: requests: cpu: "250m" memory: "512Mi" limits: cpu: "500m" memory: "512Mi" env: - name: CONTEXT_PATH value: "/v1/chat/completions" - name: HOST value: "http://llm-service:8000" - name: LLM_ENGINE value: "openai-chat" - name: MODEL_ID value: "google/gemma-4-E4B-it" - name: DISABLE_SYSTEM_MESSAGE value: "true" ports: - containerPort: 7860 --- apiVersion: v1 kind: Service metadata: name: gradio spec: selector: app: gradio ports: - protocol: TCP port: 8080 targetPort: 7860 type: ClusterIP套用資訊清單:
kubectl apply -f gradio.yaml等待部署作業完成:
kubectl wait --for=condition=Available --timeout=900s deployment/gradio在 Cloud Shell 中執行下列指令:
kubectl port-forward service/gradio 8080:8080這項操作會建立從 Cloud Shell 到 Gradio 服務的通訊埠轉送。
按一下 Cloud Shell 工作列右上方的「Web Preview」
按鈕。按一下「透過以下通訊埠預覽:8080」。瀏覽器會開啟新分頁。
使用 Gradio 對話介面與 Gemma 互動。新增提示,然後按一下「提交」。
- 如果收到
Empty reply from server訊息,可能是因為容器尚未完成下載模型資料。再次檢查 Pod 的記錄,確認是否出現Connected訊息,表示模型已準備好提供服務。 - 如果看到
Connection refused,請確認連接埠轉送功能是否已啟用。 前往 Google Cloud 控制台的「Deployed Models」(已部署模型) 頁面。
如要查看特定部署作業的詳細資料,包括指標、記錄和資訊主頁,請按一下清單中的模型名稱。
在模型詳細資料頁面中,按一下「可觀測性」分頁標籤,即可查看下列資訊主頁。如果系統提示,請按一下 啟用,為叢集啟用指標收集功能。
- 「基礎架構用量」資訊主頁會顯示使用率指標。
- 「DCGM」DCGM資訊主頁會顯示 DCGM 指標。
- 如果您使用 vLLM,則可使用「模型效能」資訊主頁,查看 vLLM 模型效能指標。
使用即時通訊介面
排解問題
觀察模型成效
如要查看模型可觀測性指標的資訊主頁,請按照下列步驟操作:
您也可以在 Cloud Monitoring 中,透過 vLLM 資訊主頁整合功能查看指標。這些指標會匯總所有 vLLM 部署作業,且沒有預設篩選器
如要在 Cloud Monitoring 中使用資訊主頁,必須在 GKE 叢集中啟用 Google Cloud Managed Service for Prometheus,從 vLLM 收集指標。vLLM 預設會以 Prometheus 格式公開指標,您不必安裝額外的匯出工具。如要瞭解如何使用 Google Cloud Managed Service for Prometheus 收集模型指標,請參閱 Cloud Monitoring 說明文件中的 vLLM 可觀測性指南。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除已部署的資源
如要避免系統向您的 Google Cloud 帳戶收取本指南所建立資源的費用,請執行下列指令:
gcloud container clusters delete CLUSTER_NAME \
--location=CONTROL_PLANE_LOCATION
替換下列值:
CONTROL_PLANE_LOCATION:叢集控制層的 Compute Engine 區域。CLUSTER_NAME:叢集名稱。
後續步驟
- 進一步瞭解 GKE 中的 GPU。
- 如要瞭解如何在其他加速器 (包括 A100 和 H100 GPU) 上使用 vLLM 搭配 Gemma,請查看 GitHub 中的程式碼範例。
- 瞭解如何在 Autopilot 中部署 GPU 工作負載。
- 瞭解如何在 Standard 中部署 GPU 工作負載。
- 探索 vLLM GitHub 存放區和說明文件。
- 探索 Vertex AI Model Garden。
- 瞭解如何運用 GKE 平台的自動化調度管理功能,執行最佳化的 AI/機器學習工作負載。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2026-04-03 (世界標準時間)。