GKE 推理网关

本文档介绍如何配置 Google Kubernetes Engine 部署,以便使用 Google Cloud Managed Service for Prometheus 从 GKE 推理网关收集指标。本页面介绍如何完成以下任务:

  • 设置 GKE 推理网关以报告指标。
  • 在 Cloud Monitoring 中访问信息中心以查看指标。

以下说明仅在您将托管式收集功能与 Managed Service for Prometheus 搭配使用时适用。 如果您使用的是自行部署的收集功能,请参阅 GKE 推理网关文档了解安装信息。

这些说明仅作为示例提供,应该适用于大多数 Kubernetes 环境。如果您因为限制性安全或组织政策而无法安装应用或导出器,则我们建议您查阅开源文档以获取支持。

如需了解 GKE 推理网关,请参阅 GKE 推理网关

前提条件

如需使用 Managed Service for Prometheus 和托管式收集功能从 GKE 推理网关导出器收集指标,您的部署必须满足以下要求:

  • 您的集群必须运行 Google Kubernetes Engine 1.28.15-gke.2475000 或更高版本。
  • 您必须运行 Managed Service for Prometheus,并启用托管式收集功能。如需了解详情,请参阅托管式收集功能使用入门

GKE 推理网关导出器会自动公开 Prometheus 格式的指标;您无需单独安装。

如需验证 GKE 推理网关导出器是否会在预期端点上发出指标,请执行以下操作:

  1. 添加 Secret、ServiceAccount、ClusterRole 和 ClusterBinding。GKE 推理网关导出器可观测性端点受身份验证令牌保护。为了获取凭证,客户端需要一个 Secret,该 Secret 映射到具有已连接 ClusterRole 的服务账号,以用于 nonResourceURLs: "/metrics", verbs: get 规则。如需了解详情,请参阅为服务账号创建 Secret

  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
      

为服务账号创建 Secret

对于受保护的 GKE 推理网关导出器端点,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 需要 ClusterPodMonitoring 资源且该资源与同一命名空间中的 GKE 推理网关导出器对应。

您可以使用以下 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 推理网关使用 ClusterPodMonitoring 资源而不是 PodMonitoring 资源,因为它需要访问来自其他命名空间的 Secret。

ClusterPodMonitoring 配置的 matchLabels 选择器中,您可以将 inference-gateway-ext-procapp 值替换为 GKE 推理网关部署中的标签。 确保 portmatchLabels 字段的值与要监控的 GKE 推理网关 pod 的值相匹配。

如需从本地文件应用配置更改,请运行以下命令:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

您还可以使用 Terraform 管理您的配置。

验证配置

您可以使用 Metrics Explorer 验证您是否正确配置了 GKE 推理网关导出器。Cloud Monitoring 可能需要一两分钟时间来注入您的指标。

要验证指标是否已注入,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往 Metrics Explorer 页面:

    进入 Metrics Explorer

    如果您使用搜索栏查找此页面,请选择子标题为监控的结果。

  2. 在查询构建器窗格的工具栏中,选择名为  MQL PromQL 的按钮。
  3. 验证已在PromQL 切换开关中选择 PromQL。语言切换开关位于同一工具栏中,用于设置查询的格式。
  4. 输入并运行以下查询:
    inference_model_request_total{cluster="CLUSTER_NAME", namespace="NAMESPACE_NAME"}

查看信息中心

Cloud Monitoring 集成包括 GKE 推理网关 Prometheus 概览信息中心。当您配置集成时,系统会自动安装信息中心。您还可以在不安装集成的情况下查看信息中心的静态预览。

如需查看已安装的信息中心,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往 信息中心页面:

    前往信息中心

    如果您使用搜索栏查找此页面,请选择子标题为监控的结果。

  2. 选择信息中心列表标签页。
  3. 选择集成类别。
  4. 点击信息中心的名称,例如 GKE 推理网关 Prometheus 概览

如需查看信息中心的静态预览,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往 集成页面:

    前往集成

    如果您使用搜索栏查找此页面,请选择子标题为监控的结果。

  2. 点击 Kubernetes Engine 部署平台过滤条件。
  3. 找到 GKE 推理网关集成,然后点击查看详情
  4. 选择信息中心标签页。

问题排查

如需了解如何排查指标注入问题,请参阅排查注入端问题中的从导出器收集的问题