TensorFlow Serving

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

  • 設定 TF Serving 以回報指標。
  • 為 Managed Service for Prometheus 設定 PodMonitoring 資源,以便收集匯出的指標。
  • 在 Cloud Monitoring 中存取資訊主頁,查看指標。

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

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

如要瞭解 TensorFlow Serving,請參閱「TF Serving」。如要瞭解如何在 Google Kubernetes Engine 上設定 TF Serving,請參閱 GKE TF Serving 指南

事前準備

如要使用 Managed Service for Prometheus 和代管收集作業,從 TF Serving 收集指標,您的部署作業必須符合下列規定:

  • 您的叢集必須執行 Google Kubernetes Engine 1.21.4-gke.300 以上版本。
  • 您必須在啟用代管收集作業的情況下,執行 Managed Service for Prometheus。詳情請參閱「 開始使用代管集合」。

--monitoring_config_file 標記用於指定含有 MonitoringConfig 通訊協定緩衝區的檔案時,TF Serving 會公開 Prometheus 格式指標。

以下是 MonitoringConfig 通訊協定緩衝區的範例:

prometheus_config {
  enable: true,
  path: "/monitoring/prometheus/metrics"
}

如果您按照 Google Kubernetes Engine 設定指南「在 GKE 中提供單一 GPU 的模型」進行設定,則 MonitoringConfig 通訊協定緩衝區會在預設設定中定義。

如果您自行設定 TF Serving,請按照下列步驟指定 MonitoringConfig 通訊協定緩衝區:

  1. 請先在模型目錄中建立名為 monitoring_config.txt 的檔案,其中包含 MonitoringConfig 通訊協定緩衝區,再將目錄上傳至 Cloud Storage 值區。

  2. 將模型目錄上傳至 Cloud Storage 值區:

    gcloud storage cp MODEL_DIRECTORY gs://CLOUD_STORAGE_BUCKET_NAME --recursive
    
  3. 將環境變數 PATH_TO_MONITORING_CONFIG 設為已上傳 monitoring_config.txt 檔案的路徑,例如:

    export PATH_TO_MONITORING_CONFIG=/data/tfserve-model-repository/monitoring_config.txt
    
  4. 在容器的部署 YAML 檔案中,將下列標記和值新增至容器的指令:

    "--monitoring_config=$PATH_TO_MONITORING_CONFIG"
    

    舉例來說,您的指令可能如下所示:

    command: [ "tensorflow_model_server", "--model_name=$MODEL_NAME", "--model_base_path=/data/tfserve-model-repository/$MODEL_NAME", "--rest_api_port=8000", "--monitoring_config_file=$PATH_TO_MONITORING_CONFIG" ]
    

修改 TF 內容提供設定

修改 TF Serving 設定,如以下範例所示:

# 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: apps/v1
kind: Deployment
metadata:
  name: tfserve-deployment
  labels:
    app: tfserve-server
spec:
  selector:
    matchLabels:
      app: tfserve
  replicas: 1
  template:
    metadata:
      labels:
        app: tfserve
      annotations:
        gke-gcsfuse/volumes: 'true'
    spec:
      nodeSelector:
        cloud.google.com/gke-accelerator: nvidia-l4
      containers:
        - name: tfserve-server
          image: 'tensorflow/serving:2.13.1-gpu'
          command:
            - tensorflow_model_server
            - '--model_name=$MODEL_NAME'
            - '--model_base_path=/data/tfserve-model-repository/$MODEL_NAME'
            - '--rest_api_port=8000'
+           - '--monitoring_config_file=$PATH_TO_MONITORING_CONFIG'
          ports:
            - name: http
              containerPort: 8000
            - name: grpc
              containerPort: 8500
          resources:
            ...
          volumeMounts:
            - name: gcs-fuse-csi-vol
              mountPath: /data
              readOnly: false
      serviceAccountName: $K8S_SA_NAME
      volumes:
        - name: gcs-fuse-csi-vol
          csi:
            driver: gcsfuse.csi.storage.gke.io
            readOnly: false
            volumeAttributes:
              bucketName: $GSBUCKET
              mountOptions: implicit-dirs

您必須在設定中加入任何前面有 + 符號的行。

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

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

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

如要確認 TF Serving 是否會在預期的端點上發出指標,請執行下列操作:

  1. 使用下列指令設定通訊埠轉送:
      kubectl -n NAMESPACE_NAME port-forward POD_NAME 8000
    
  2. 在其他終端機工作階段中,使用瀏覽器或 curl 公用程式存取端點 localhost:8000/monitoring/prometheus/metrics

定義 PodMonitoring 資源

針對目標探索,Managed Service for Prometheus Operator 需要在相同命名空間中對應 TF Serving 的 PodMonitoring 資源。

您可以使用下列 PodMonitoring 設定:

# 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: PodMonitoring
metadata:
  name: tfserve
  labels:
    app.kubernetes.io/name: tfserve
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  endpoints:
  - port: 8000
    scheme: http
    interval: 30s
    path: /monitoring/prometheus/metrics
  selector:
    matchLabels:
      app: tfserve

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

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

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

驗證設定

您可以使用 Metrics Explorer 驗證是否已正確設定 TF Serving。Cloud Monitoring 可能需要一或兩分鐘的時間才能擷取指標。

如要確認已擷取指標,請執行下列操作:

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

    前往 Metrics Explorer

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

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

查看資訊主頁

Cloud Monitoring 整合功能包含 TensorFlow Serving Prometheus 總覽資訊主頁。設定整合後,系統會自動安裝資訊主頁。您也可以不必安裝整合功能,直接查看資訊主頁的靜態預覽畫面。

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

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

    前往「Dashboards」(資訊主頁)

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

  2. 選取「資訊主頁清單」分頁標籤。
  3. 選擇「整合」類別。
  4. 按一下資訊主頁名稱,例如「TensorFlow Serving Prometheus 總覽」

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

  1. 在 Google Cloud 控制台中,前往「Integrations」(整合) 頁面:

    前往「Integrations」

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

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

疑難排解

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