GKE 推論閘道

本文說明如何設定 Google Kubernetes Engine 部署作業,以便使用 Google Cloud Managed Service for Prometheus 從 GKE Inference Gateway 收集指標。本文將說明如何執行下列操作:

  • 設定 GKE Inference Gateway 回報指標。
  • 設定 ClusterPodMonitoring 資源,讓 Managed Service for Prometheus 收集匯出的指標。
  • 在 Cloud Monitoring 中存取資訊主頁,即可查看指標。

只有在使用 Managed Service for Prometheus 的 代管收集作業時,才適用這些操作說明。如果您使用自行部署的收集作業,請參閱 GKE Inference Gateway 說明文件,瞭解安裝資訊。

這些操作說明僅供參考,適用於大多數 Kubernetes 環境。 如果因安全或機構政策限制而無法安裝應用程式或匯出工具,建議您參閱開放原始碼文件尋求支援。

如要瞭解 GKE Inference Gateway,請參閱「GKE Inference Gateway」。

必要條件

如要使用 Managed Service for Prometheus 和代管收集作業,從 GKE Inference Gateway 匯出工具收集指標,部署作業必須符合下列規定:

  • 叢集必須執行 Google Kubernetes Engine 1.28.15-gke.2475000 以上版本。
  • 您必須執行 Managed Service for Prometheus,並啟用代管收集作業。詳情請參閱「 開始使用代管集合」一文。

GKE Inference Gateway 匯出工具會自動公開 Prometheus 格式的指標,您不必另外安裝。

如要確認 GKE Inference Gateway 匯出工具是否在預期端點上發出指標,請按照下列步驟操作:

  1. 新增密鑰、ServiceAccount、ClusterRole 和 ClusterBinding。 GKE Inference Gateway 匯出工具的可觀測性端點受到驗證權杖保護。如要取得憑證,用戶端需要對應至具有已連結 ClusterRole 的服務帳戶的 Secret,以符合 nonResourceURLs: "/metrics", verbs: get 規則。詳情請參閱「建立服務帳戶的密鑰」。

  2. 使用下列指令設定通訊埠轉送:

    kubectl -n NAMESPACE_NAME port-forward POD_NAME 9090
    
  3. 在另一個視窗中,執行下列操作:

    1. 執行下列指令來擷取權杖:

      TOKEN=$(kubectl -n default get secret inference-gateway-sa-metrics-reader-secret  -o jsonpath='{.secrets[0].name}' -o jsonpath='{.data.token}' | base64 --decode)
      
    2. 使用 curl 公用程式存取端點 localhost:9090/metrics

      curl -H "Authorization: Bearer $TOKEN" localhost:9090/metrics
      

為服務帳戶建立密鑰

如要保護 GKE Inference Gateway 匯出工具端點,Managed Service for Prometheus Operator 必須在 gmp-system 命名空間中,使用密鑰授權收集指標。

如果叢集使用 Autopilot 模式,請將 gmp-system 替換為 gke-gmp-system

您可以使用下列 Secret、ServiceAccount、ClusterRole 和 ClusterRoleBinding 設定:

# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: inference-gateway-metrics-reader
rules:
- nonResourceURLs:
  - /metrics
  verbs:
  - get
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: inference-gateway-sa-metrics-reader
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: inference-gateway-sa-metrics-reader-role-binding
  namespace: default
subjects:
- kind: ServiceAccount
  name: inference-gateway-sa-metrics-reader
  namespace: default
roleRef:
  kind: ClusterRole
  name: inference-gateway-metrics-reader
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: Secret
metadata:
  name: inference-gateway-sa-metrics-reader-secret
  namespace: default
  annotations:
    kubernetes.io/service-account.name: inference-gateway-sa-metrics-reader
type: kubernetes.io/service-account-token
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: inference-gateway-sa-metrics-reader-secret-read
rules:
- resources:
  - secrets
  apiGroups: [""]
  verbs: ["get", "list", "watch"]
  resourceNames: ["inference-gateway-sa-metrics-reader-secret"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: gmp-system:collector:inference-gateway-sa-metrics-reader-secret-read
  namespace: default
roleRef:
  name: inference-gateway-sa-metrics-reader-secret-read
  kind: ClusterRole
  apiGroup: rbac.authorization.k8s.io
subjects:
- name: collector
  namespace: gmp-system
  kind: ServiceAccount

詳情請參閱匯出工具的指標與可觀測性指南

如要套用本機檔案的設定變更,請執行下列指令:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

您也可以使用 Terraform 管理設定。

定義 ClusterPodMonitoring 資源

如要探索目標,Managed Service for Prometheus Operator 需要與相同命名空間中的 GKE Inference Gateway 匯出工具對應的 ClusterPodMonitoring 資源。

您可以使用下列 ClusterPodMonitoring 設定:

# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: ClusterPodMonitoring
metadata:
  name: inference-optimized-gateway-monitoring
  labels:
    app.kubernetes.io/name: inference-optimized-gateway
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  endpoints:
  - port: metrics
    scheme: http
    interval: 5s
    path: /metrics
    authorization:
      type: Bearer
      credentials:
        secret:
          name: inference-gateway-sa-metrics-reader-secret
          key: token
          namespace: default
  selector:
    matchLabels:
      app: inference-gateway-ext-proc

GKE Inference Gateway 使用 ClusterPodMonitoring 資源,而非 PodMonitoring 資源,因為它需要存取另一個命名空間中的密鑰。

ClusterPodMonitoring 設定的 matchLabels 選擇器中,您可以將 inference-gateway-ext-procapp 值替換為 GKE Inference Gateway 部署作業中的標籤。

如要套用本機檔案的設定變更,請執行下列指令:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

您也可以使用 Terraform 管理設定。

驗證設定

您可以使用 Metrics Explorer 驗證是否已正確設定 GKE Inference Gateway 匯出工具。Cloud Monitoring 可能需要一到兩分鐘才能擷取指標。

如要確認指標已擷取,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的 「Metrics Explorer」頁面:

    前往 Metrics Explorer

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果

  2. 在查詢建構工具窗格的工具列中,選取名稱為  MQL PromQL 的按鈕。
  3. 確認已在「Language」(語言) 切換按鈕中選取「PromQL」。語言切換按鈕位於同一工具列,可供你設定查詢格式。
  4. 輸入並執行下列查詢:
    inference_model_request_total{cluster="CLUSTER_NAME", namespace="NAMESPACE_NAME"}

查看資訊主頁

Cloud Monitoring 整合功能包含 GKE Inference Gateway Prometheus 總覽資訊主頁。設定整合作業後,系統會自動安裝資訊主頁。 您也可以查看資訊主頁的靜態預覽畫面,不需安裝整合功能。

如要查看已安裝的資訊主頁,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中,前往「Dashboards」(資訊主頁) 頁面:

    前往「Dashboards」(資訊主頁)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果

  2. 選取「資訊主頁清單」分頁標籤。
  3. 選擇「整合」類別。
  4. 按一下資訊主頁的名稱,例如「GKE Inference Gateway Prometheus Overview」

如要查看資訊主頁的靜態預覽畫面,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的 「Integrations」(整合) 頁面

    前往「整合」

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果

  2. 按一下「Kubernetes Engine」部署平台篩選器。
  3. 找出 GKE Inference Gateway 整合項目,然後按一下「查看詳細資料」
  4. 選取「資訊主頁」分頁標籤。

疑難排解

如要瞭解如何排解指標擷取問題,請參閱「 排解擷取端問題」一文中的「 收集匯出工具資料時發生問題」一節。