Apache Tomcat

このドキュメントでは、Google Cloud Managed Service for Prometheus を使用して Apache Tomcat から指標を収集できるように、Google Kubernetes Engine の Deployment を構成する方法について説明します。このドキュメントでは、次の方法について説明します。

  • Tomcat のエクスポータを設定して、指標を報告する。
  • エクスポートされた指標を収集するために、Managed Service for Prometheus の PodMonitoring リソースを構成する。
  • Cloud Monitoring のダッシュボードにアクセスして指標を表示する。
  • 指標をモニタリングするようにアラートルールを構成する。

以下の手順は、Managed Service for Prometheus でマネージド コレクションを使用している場合にのみ適用されます。セルフデプロイ コレクションを使用している場合は、JMX エクスポータのソース リポジトリでインストール情報をご覧ください。

以下の手順は一例であり、ほとんどの Kubernetes 環境で機能します。セキュリティ ポリシーや組織のポリシーの制限により、アプリケーションやエクスポータのインストールに問題がある場合は、オープンソース ドキュメントでサポートを確認することをおすすめします。

Tomcat の詳細については、Apache Tomcat をご覧ください。

前提条件

Managed Service for Prometheus とマネージド コレクションを使用して Tomcat から指標を収集するには、Deployment が次の要件を満たしている必要があります。

  • クラスタで Google Kubernetes Engine バージョン 1.21.4-gke.300 以降を実行している必要があります。
  • マネージド コレクションを有効にして、Managed Service for Prometheus を実行する必要があります。詳細については、マネージド コレクションを使ってみるをご覧ください。

  • Tomcat とのインテグレーションに Cloud Monitoring で利用可能なダッシュボードを使用するには、jmx-exporter バージョン 0.17.0 以降を使用する必要があります。

    利用可能なダッシュボードの詳細については、ダッシュボードを表示するをご覧ください。

Tomcat は JMX をサポートしています。これは、CATALINA_OPTS 環境変数を構成することで有効にできます。

Tomcat エクスポータをインストールする

Tomcat ワークロードにサイドカーとして Tomcat エクスポータ jmx-exporter をインストールすることをおすすめします。サイドカーの使用方法については、マルチコンテナ Pod を使用した Kubernetes での拡張アプリケーションをご覧ください。

jmx-exporter をサイドカーとして Tomcat にインストールするには、次の例のように 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 には、同じ Namespace 内の Tomcat エクスポータに対応する 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 を使用すると、Tomcat エクスポータが正しく構成されていることを確認できます。Cloud Monitoring が指標を取り込むまでに 1~2 分かかる場合があります。

指標が取り込まれていることを確認します。

  1. Google Cloud コンソールのナビゲーション パネルで [Monitoring] を選択し、次に [ Metrics Explorer] を選択します。

    Metrics Explorer に移動

  2. クエリビルダー ペインのツールバーで、[MQL] または [PROMQL] という名前のボタンを選択します。
  3. [言語] で [PromQL] が選択されていることを確認します。言語切り替えボタンは、クエリの書式設定と同じツールバーにあります。
  4. 次のクエリを入力して実行します。
    up{job="tomcat", cluster="CLUSTER_NAME", namespace="NAMESPACE_NAME"}

ダッシュボードを表示する

Cloud Monitoring インテグレーションには、Tomcat Prometheus の概要ダッシュボードが含まれています。ダッシュボードは、インテグレーションを構成すると自動的にインストールされます。インテグレーションをインストールすることなく、ダッシュボードの静的プレビューを表示することもできます。

インストールされているダッシュボードを表示する手順は次のとおりです。

  1. Google Cloud コンソールのナビゲーション パネルで、[Monitoring] を選択してから、[ダッシュボード] を選択します。

    [ダッシュボード] に移動

  2. [ダッシュボード リスト] タブを選択します。
  3. [統合] カテゴリを選択します。
  4. ダッシュボードの名前(Tomcat Prometheus Overview など)をクリックします。

ダッシュボードの静的プレビューを表示する手順は次のとおりです。

  1. Google Cloud コンソールのナビゲーション パネルで、[Monitoring] を選択してから、[インテグレーション] を選択します。

    [インテグレーション] に移動

  2. [デプロイメント プラットフォーム] フィルタの [Kubernetes Engine] をクリックします。
  3. Apache Tomcat インテグレーションを見つけ、[詳細を表示] をクリックします。
  4. [ダッシュボード] タブを選択します。

トラブルシューティング

指標の取り込みに関する問題のトラブルシューティングについては、取り込み側の問題のトラブルシューティングエクスポータからの収集に関する問題をご覧ください。