‫Apache Tomcat

במאמר הזה מוסבר איך להגדיר את הפריסה של Google Kubernetes Engine כדי שתוכלו להשתמש בשירות המנוהל של Google Cloud ל-Prometheus לאיסוף מדדים מ-Apache Tomcat. במאמר הזה מוסבר איך:

  • מגדירים את כלי הייצוא של Tomcat לדיווח על מדדים.
  • כדי לראות את המדדים, נכנסים למרכז בקרה מוגדר מראש ב-Cloud Monitoring.
  • הגדרת כללי התראות למעקב אחרי המדדים.

ההוראות האלה רלוונטיות רק אם אתם משתמשים באיסוף מנוהל עם שירות מנוהל ל-Prometheus. אם אתם משתמשים באיסוף בפריסה עצמית, עיינו במאגר המקור של JMX exporter כדי לקבל מידע על ההתקנה.

ההוראות האלה הן דוגמה, והן אמורות לפעול ברוב סביבות Kubernetes. אם אתם נתקלים בבעיות בהתקנה של אפליקציה או של כלי ייצוא בגלל מדיניות אבטחה או מדיניות ארגונית מגבילה, מומלץ לעיין במסמכי התיעוד של קוד פתוח כדי לקבל תמיכה.

מידע נוסף על Apache Tomcat זמין במאמר Apache Tomcat.

דרישות מוקדמות

כדי לאסוף מדדים מ-Tomcat באמצעות שירות מנוהל ל-Prometheus ואיסוף מנוהל, הפריסה צריכה לעמוד בדרישות הבאות:

  • באשכול צריכה לפעול גרסה 1.28.15-gke.2475000 ומעלה של Google Kubernetes Engine.
  • צריך להפעיל את השירות המנוהל ל-Prometheus עם איסוף מנוהל. מידע נוסף זמין במאמר תחילת העבודה עם גבייה מנוהלת.

  • כדי להשתמש בלוחות הבקרה שזמינים ב-Cloud Monitoring לשילוב של Tomcat, צריך להשתמש בגרסה 0.17.0 ומעלה של jmx-exporter.

    למידע נוסף על מרכזי הבקרה הזמינים, ראו הצגת מרכזי בקרה.

‫Tomcat תומך ב-JMX, ואפשר להפעיל אותו על ידי הגדרת משתנה הסביבה CATALINA_OPTS.

התקנת המייצא של Tomcat

מומלץ להתקין את המייצא של Tomcat,‏ jmx-exporter, כ-sidecar לעומס העבודה של Tomcat. למידע נוסף על השימוש ב-sidecars, קראו את המאמר אפליקציות מורחבות ב-Kubernetes עם pod-ים מרובי קונטיינרים.

כדי להתקין את jmx-exporter כ-sidecar ל-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

כדי לגלות יעדים, המפעיל של השירות המנוהל ל-Prometheus דורש משאב PodMonitoring שתואם למייצא ה-JMX באותו מרחב שמות.

אפשר להשתמש בהגדרת 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

מוודאים שבוררי התוויות והיציאה תואמים לבוררים וליציאה שבהם השתמשתם בהתקנת ה-exporter של 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 exporter. יכול להיות שיעברו דקה או שתיים עד ש-Cloud Monitoring יקלוט את המדדים.

כדי לוודא שהמדדים נקלטים, מבצעים את הפעולות הבאות:

  1. במסוף Google Cloud , נכנסים לדף  Metrics explorer:

    כניסה לדף Metrics Explorer

    אם אתם משתמשים בשורת החיפוש כדי למצוא את הדף הזה, בחרו בתוצאה שכותרת המשנה שלה היא Monitoring.

  2. בסרגל הכלים של החלונית query-builder, לוחצים על הכפתור  PromQL.
  3. מזינים ומריצים את השאילתה הבאה:
    up{job="tomcat", cluster="CLUSTER_NAME", namespace="NAMESPACE_NAME"}

צפייה במרכזי הבקרה

השילוב של Cloud Monitoring כולל את לוח הבקרה Tomcat Prometheus Overview. מרכזי הבקרה מותקנים באופן אוטומטי כשמגדירים את השילוב. אתם יכולים גם לראות תצוגות מקדימות סטטיות של מרכזי הבקרה בלי להתקין את השילוב.

כדי לראות מרכז בקרה שהותקן, מבצעים את הפעולות הבאות:

  1. נכנסים לדף  Dashboards במסוף Google Cloud :

    עוברים אל מרכזי בקרה.

    אם אתם משתמשים בשורת החיפוש כדי למצוא את הדף הזה, בחרו בתוצאה שכותרת המשנה שלה היא Monitoring.

  2. לוחצים על הכרטיסייה רשימת מרכזי הבקרה.
  3. בוחרים בקטגוריה Integrations (שילובים).
  4. לוחצים על שם לוח הבקרה, לדוגמה Tomcat Prometheus Overview.

כדי לראות תצוגה מקדימה סטטית של מרכז הבקרה:

  1. נכנסים לדף  Integrations במסוף Google Cloud :

    עוברים אל שילובים.

    אם אתם משתמשים בשורת החיפוש כדי למצוא את הדף הזה, בחרו בתוצאה שכותרת המשנה שלה היא Monitoring.

  2. לוחצים על המסנן Kubernetes Engine של פלטפורמת הפריסה.
  3. מאתרים את השילוב של Apache Tomcat ולוחצים על View Details.
  4. לוחצים על הכרטיסייה מרכזי בקרה.

פתרון בעיות

למידע על פתרון בעיות בהטמעת מדדים, קראו את הקטע בעיות באיסוף ממייצאים במאמר פתרון בעיות בצד ההטמעה.