Apache Tomcat

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

  • 設定 Tomcat 的匯出工具,以回報指標。
  • 為 Managed Service for Prometheus 設定 PodMonitoring 資源,以收集匯出的指標。
  • 在 Cloud Monitoring 中存取資訊主頁,即可查看指標。
  • 設定快訊規則來監控指標。

只有在使用 Managed Service for Prometheus 的 代管收集作業時,才適用這些操作說明。如果您使用自行部署的收集作業,請參閱 JMX 匯出工具的來源存放區,瞭解安裝資訊。

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

如需 Apache Tomcat 的相關資訊,請參閱 Apache Tomcat

必要條件

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

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

  • 如要使用 Cloud Monitoring 提供的 Tomcat 整合資訊主頁,請使用 jmx-exporter 0.17.0 以上版本。

    如要進一步瞭解可用的資訊主頁,請參閱「查看資訊主頁」。

Tomcat 支援 JMX,只要設定 CATALINA_OPTS 環境變數即可啟用。

安裝 Tomcat 匯出工具

建議您將 Tomcat 匯出工具 jmx-exporter 安裝為 Tomcat 工作負載的補充資訊。如要瞭解如何使用補充資訊,請參閱「在 Kubernetes 上使用多容器 Pod 擴充應用程式」。

如要將 jmx-exporter 安裝為 Tomcat 的 Sidecar,請按照下列範例修改 Tomcat 設定:

# Copyright 2023 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: v1
kind: ConfigMap
metadata:
  name: tomcat-exporter
data:
  config.yml: |
    hostPort: 127.0.0.1:9010
    lowercaseOutputLabelNames: true
    lowercaseOutputName: true
    rules:
    - pattern: 'Catalina<type=GlobalRequestProcessor, name=\"(\w+-\w+)-(\d+)\"><>(\w+):'
      name: tomcat_$3_total
      labels:
        port: "$2"
        protocol: "$1"
      help: Tomcat global $3
      type: COUNTER
    - pattern: 'Catalina<j2eeType=Servlet, WebModule=//([-a-zA-Z0-9+&@#/%?=~_|!:.,;]*[-a-zA-Z0-9+&@#/%=~_|]), name=([-a-zA-Z0-9+/$%~_-|!.]*), J2EEApplication=none, J2EEServer=none><>(requestCount|maxTime|processingTime|errorCount):'
      name: tomcat_servlet_$3_total
      labels:
        module: "$1"
        servlet: "$2"
      help: Tomcat servlet $3 total
      type: COUNTER
    - pattern: 'Catalina<type=ThreadPool, name="(\w+-\w+)-(\d+)"><>(currentThreadCount|currentThreadsBusy|keepAliveCount|pollerThreadCount|connectionCount):'
      name: tomcat_threadpool_$3
      labels:
        port: "$2"
        protocol: "$1"
      help: Tomcat threadpool $3
      type: GAUGE
    - pattern: 'Catalina<type=Manager, host=([-a-zA-Z0-9+&@#/%?=~_|!:.,;]*[-a-zA-Z0-9+&@#/%=~_|]), context=([-a-zA-Z0-9+/$%~_-|!.]*)><>(processingTime|sessionCounter|rejectedSessions|expiredSessions):'
      name: tomcat_session_$3_total
      labels:
        context: "$2"
        host: "$1"
      help: Tomcat session $3 total
      type: COUNTER
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
spec:
  selector:
    matchLabels:
+     app.kubernetes.io/name: tomcat
  template:
    metadata:
      labels:
+       app.kubernetes.io/name: tomcat
    spec:
      containers:
        - name: tomcat
          image: tomcat:9.0.46-jdk11-openjdk-buster
          ports:
            - containerPort: 8080
              name: http
          env:
+           - name: CATALINA_OPTS
+             value: "-Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=9010 -Dcom.sun.management.jmxremote.rmi.port=9010"
+       - name: exporter
+         image: bitnami/jmx-exporter:0.17.0
+         ports:
+           - containerPort: 9113
+             name: prometheus
+         command:
+           - java
+           - -jar
+           - jmx_prometheus_httpserver.jar
+         args:
+           - "9113"
+           - /opt/jmx_exporter/config.yml
+         volumeMounts:
+           - mountPath: /opt/jmx_exporter/config.yml
+             subPath: config.yml
+             name: tomcat-exporter
+     volumes:
+       - name: tomcat-exporter
+         configMap:
+           name: tomcat-exporter
+           items:
+             - key: config.yml
+               path: config.yml

您必須將以 + 符號開頭的任何行新增至設定。

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

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

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

定義 PodMonitoring 資源

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

您可以使用下列 PodMonitoring 設定:

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

請確認標籤選取器和通訊埠與「安裝 Tomcat 匯出工具」中使用的選取器和通訊埠相符。

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

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

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

定義規則和快訊

您可以使用下列 Rules 設定,針對 Tomcat 指標定義快訊:

# Copyright 2023 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: Rules
metadata:
  name: tomcat-rules
  labels:
    app.kubernetes.io/component: rules
    app.kubernetes.io/name: tomcat-rules
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  groups:
  - name: tomcat
    interval: 30s
    rules:
    - alert: TomcatHighRequestRate
      annotations:
        description: |-
          Tomcat high request rate
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Tomcat high request rate (instance {{ $labels.instance }})
      expr: rate(tomcat_requestcount_total[5m]) >= 100
      for: 5m
      labels:
        severity: warning
    - alert: TomcatLowRequestRate
      annotations:
        description: |-
          Tomcat low request rate
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Tomcat low request rate (instance {{ $labels.instance }})
      expr: rate(tomcat_requestcount_total[5m]) <= 10
      for: 5m
      labels:
        severity: warning
    - alert: TomcatHighErrorRate
      annotations:
        description: |-
          Tomcat high error rate
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Tomcat high error rate (instance {{ $labels.instance }})
      expr: rate(tomcat_errorcount_total[5m]) > 100
      for: 5m
      labels:
        severity: warning

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

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

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

如要進一步瞭解如何將規則套用至叢集,請參閱「受管理規則評估和快訊」。

您可以根據應用程式調整快訊閾值。

驗證設定

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

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

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

    前往 Metrics Explorer

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

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

查看資訊主頁

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

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

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

    前往「Dashboards」(資訊主頁)

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

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

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

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

    前往「整合」

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

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

疑難排解

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