Skip to main content
Sumo Logic

Collect Apache Tomcat Logs and Metrics for Kubernetes environments

In a Kubernetes environment, we use the Telegraf Operator, which is packaged with our Kubernetes collection. You can learn more about it here. The diagram below illustrates how data is collected from Apache Tomcat in a Kubernetes environment. Four services in the architecture shown below make up the metric collection pipeline: Telegraf, Prometheus, Fluentd, and FluentBit.

The first service in the pipeline is Telegraf. Telegraf collects metrics from Apache Tomcat. Note that we’re running Telegraf in each pod we want to collect metrics from as a sidecar deployment, for example, Telegraf runs in the same pod as the containers it monitors. Telegraf uses the Apache Tomcat and Jolokia2 input plugin to obtain metrics. (For simplicity, the diagram doesn’t show the input plugins.) The injection of the Telegraf sidecar container is done by the Telegraf Operator. We also have Fluentbit that collects logs written to standard out and forwards them to FluentD, which in turn sends all the logs and metrics data to a Sumo Logic HTTP Source.

clipboard_e07c461bc6761a11d4d2e4c33bd41168b.png

Follow the below instructions to set up the metric collection:

  1. Configure Metrics Collection

    1. Setup Kubernetes Collection with the Telegraf operator
    2. Add annotations on your Apache Tomcat pods
  2. Configure Logs Collection

    1. Configure logging in Apache Tomcat.
    2. Add labels on your Apache Tomcat pods to capture logs from standard output.
    3. Collecting Apache Tomcat Logs from a Log file.

Prerequisites

It’s assumed that you are using the latest helm chart version if not upgrade using the instructions here.

Step 1 Configure Metrics Collection

This section explains the steps to collect Apache Tomcat metrics from a Kubernetes environment.

In a Kubernetes environment, we use the Telegraf Operator, which is packaged with our Kubernetes collection. You can learn more on this here. Follow the steps listed below to collect metrics from a Kubernetes environment:

  1. Set up Kubernetes Collection with the Telegraf Operator
    Please ensure that you are monitoring your Kubernetes clusters with the Telegraf operator enabled -  If you are not, then please follow these instructions to do so. 

  2. Install jolokia on your Tomcat Pod to use the Jolokia Telegraf Input Plugin

  • Download the latest version of the Jolokia war file from: https://jolokia.org/download.html.

  • Rename the file from jolokia-war-X.X.X.war to jolokia.war

  • Create a configMap jolokia from the binary file

    kubectl create configmap jolokia --from-file=jolokia.jar

  • Create volume mount the jolokia.war file to ${TOMCAT_HOME}/webapps.

spec:
  volumes:
    - name: jolokia
      configMap:
        name: jolokia
  containers:
    - name: XYZ
      image: XYZ
      env:
      - name: TOMCAT_OPTS
        value: "-javaagent:/opt/jolokia/jolokia.jar=port=8778,host=0.0.0.0"
      volumeMounts:
        - mountPath: "/opt/jolokia"
          name: jolokia

 

  • Add jolokia as role in tomcat-users.xml

<tomcat-users>
  <role rolename="manager-jmx"/>
  <user name="admin" password="admin" roles="admin-gui,admin-script,manager-gui,manager-status,manager-script,manager-jmx"/>
</tomcat-users>

Verification Step: You can ssh to Tomcat pod and run following commands to make sure Telegraf (and Jolokia) is scraping metrics from your Tomcat Pod: 

  • curl localhost:9273/metrics

  1. Add annotations on your Apache Tomcat pods

On your Apache Tomcat Pods, add the following annotations:

  annotations:
    telegraf.influxdata.com/class: sumologic-prometheus
    prometheus.io/scrape: "true"
    prometheus.io/port: "9273"
    telegraf.influxdata.com/inputs: |+
         [[inputs.tomcat]]
            url = "http://127.0.0.1:8080/manager/status/all?XML=true"
            username = "<username-CHANGME>"
            password = "<password-CHANGME>"
            [inputs.tomcat.tags]
              environment="dev_CHANGEME"
              component="webserver"
              webserver_system="tomcat"
              webserver_farm="tomcat_on_k8s_CHANGEME"
[[inputs.jolokia2_agent]]
  name_prefix = "tomcat_jmx_"
  urls = ["http://localhost:8088/jolokia"]
  username = "username-CHANGME"
  password = "password-CHANGME"
  [inputs.jolokia2_agent.tags]
    environment="dev_CHANGEME"
    component="webserver"
    webserver_system="tomcat"
    webserver_farm="tomcat_on_k8s_CHANGEME"
  ### JVM Generic
  [[inputs.jolokia2_agent.metric]]
    name  = "OperatingSystem"
    mbean = "java.lang:type=OperatingSystem"
    paths = ["ProcessCpuLoad","SystemLoadAverage","SystemCpuLoad","TotalPhysicalMemorySize","FreeSwapSpaceSize","TotalSwapSpaceSize","FreePhysicalMemorySize","AvailableProcessors"]
  [[inputs.jolokia2_agent.metric]]
    name  = "jvm_runtime"
    mbean = "java.lang:type=Runtime"
    paths = ["Uptime"]
  [[inputs.jolokia2_agent.metric]]
    name  = "jvm_memory"
    mbean = "java.lang:type=Memory"
    paths = ["HeapMemoryUsage", "NonHeapMemoryUsage", "ObjectPendingFinalizationCount"]

  [[inputs.jolokia2_agent.metric]]
    name     = "jvm_garbage_collector"
    mbean    = "java.lang:name=*,type=GarbageCollector"
    paths    = ["CollectionTime", "CollectionCount"]
    tag_keys = ["name"]
  [[inputs.jolokia2_agent.metric]]
    name       = "jvm_memory_pool"
    mbean      = "java.lang:name=*,type=MemoryPool"
    paths      = ["Usage", "PeakUsage", "CollectionUsage"]
    tag_keys   = ["name"]
    tag_prefix = "pool_"
  [[inputs.jolokia2_agent.metric]]
    name     = "GlobalRequestProcessor"
    mbean    = "Catalina:name=*,type=GlobalRequestProcessor"
    paths    = ["requestCount","bytesReceived","bytesSent","processingTime","errorCount"]
    tag_keys = ["name"]
  [[inputs.jolokia2_agent.metric]]
    name     = "JspMonitor"
    mbean    = "Catalina:J2EEApplication=*,J2EEServer=*,WebModule=*,name=jsp,type=JspMonitor"
    paths    = ["jspReloadCount","jspCount","jspUnloadCount"]
    tag_keys = ["J2EEApplication","J2EEServer","WebModule"]
  [[inputs.jolokia2_agent.metric]]
    name     = "ThreadPool"
    mbean    = "Catalina:name=*,type=ThreadPool"
    paths    = ["maxThreads","currentThreadCount","currentThreadsBusy"]
    tag_keys = ["name"]
  [[inputs.jolokia2_agent.metric]]
    name     = "Servlet"
    mbean    = "Catalina:J2EEApplication=*,J2EEServer=*,WebModule=*,j2eeType=Servlet,name=*"
    paths    = ["processingTime","errorCount","requestCount"]
    tag_keys = ["name","J2EEApplication","J2EEServer","WebModule"]
  [[inputs.jolokia2_agent.metric]]
    name     = "Cache"
    mbean    = "Catalina:context=*,host=*,name=Cache,type=WebResourceRoot"
    paths    = ["hitCount","lookupCount"]
    tag_keys = ["context","host"]

Please enter in values for the following parameters (marked in bold_CHANGEME above):

  • telegraf.influxdata.com/inputs - This contains the required configuration for the Telegraf Tomcat Input plugin. Please refer to this doc for more information on configuring the Tomcat input plugin for Telegraf. Note: As telegraf will be run as a sidecar the host should always be localhost.

    • In the input plugins section, which is [[inputs.Tomcat]]: 

      • servers - The URL to the Tomcat server. This can be a comma-separated list to connect to multiple Tomcat servers. Please see this doc for more information on additional parameters for configuring the Tomcat input plugin for Telegraf.

    • In the tags section, whis is [inputs.Tomcat.tags]

      • environment - This is the deployment environment where the Tomcat farm identified by the value of servers resides. For example: dev, prod or qa. While this value is optional we highly recommend setting it. 

      • webserver_farm - Enter a name to identify this Tomcat farm. This farm name will be shown in the Sumo Logic dashboards. 

    • In the input plugins section, which is [[inputs.jolokia2_agent]]: 

      • urls - The URL to the tomcat server. This can be a comma-separated list to connect to multiple tomcat servers. Please see this doc for more information on additional parameters for configuring the Tomcat input plugin for Telegraf.

    • In the tags section, which is [inputs.jolokia2_agent.tags]

      • environment - This is the deployment environment where the Tomcat farm identified by the value of servers resides. For example: dev, prod or qa. While this value is optional we highly recommend setting it. 

      • webserver_farm - Enter a name to identify this Tomcat farm. This farm name will be shown in the Sumo Logic dashboards. 

Here’s an explanation for additional values set by this configuration that we request you please do not modify as they will cause the Sumo Logic apps to not function correctly.

  • telegraf.influxdata.com/class: sumologic-prometheus - This instructs the Telegraf operator what output to use. This should not be changed.

  • prometheus.io/scrape: "true" - This ensures our Prometheus will scrape the metrics.

  • prometheus.io/port: "9273" - This tells prometheus what ports to scrape on. This should not be changed.

  • telegraf.influxdata.com/inputs

    • In the tags section, which is [inputs.Tomcat.tags]

      • component: “webserver” - This value is used by Sumo Logic apps to identify application components. 

      • webserver_system: “tomcat” - This value identifies the web server system.

    • In the tags section, which is [inputs.jolokia2_agent.tags]

      • component: “webserver” - This value is used by Sumo Logic apps to identify application components. 

      • webserver_system: “tomcat” - This value identifies the web server system.

For all other parameters please see this doc for more properties that can be configured in the Telegraf agent globally.

  1. Sumo Logic Kubernetes collection will automatically start collecting metrics from the pods having the labels and annotations defined in the previous step. 

  2. Verify metrics in Sumo Logic.

Step 2 Configure Logs Collection

This section explains the steps to collect Apache Tomcat logs from a Kubernetes environment.

  1. (Recommended Method) Add labels on your Apache Tomcat pods to capture logs from standard output.

Follow the instructions below to capture Apache Tomcat logs from stdout on Kubernetes.

  1. Apply following labels to the Apache Tomcat pods:

 labels:

    environment: "prod_CHANGEME"

    component: "webserver"

    webserver_system: "tomcat"

    webserver_farm: "tomcat_prod__CHANGEME

Please enter in values for the following parameters (marked in bold above):

  • environment - This is the deployment environment where the Tomcat farm identified by the value of servers resides. For example: dev, prod or qa. While this value is optional we highly recommend setting it.

  • Webserver_farm - Enter a name to identify this Tomcat farm. This farm name will be shown in the Sumo Logic dashboards.

Here’s an explanation for additional values set by this configuration that we request you please do not modify as they will cause the Sumo Logic apps to not function correctly.

  • component: “webserver” - This value is used by Sumo Logic apps to identify application components. 

  • webserver_system: “tomcat” - This value identifies the webserver system.

For all other parameters please see this doc for more properties that can be configured in the Telegraf agent globally.

  1. The Sumologic-Kubernetes-Collection will automatically capture the logs from stdout and will send the logs to Sumologic. For more information on deploying Sumologic-Kubernetes-Collection, visit here.

  2. Verify logs in Sumo Logic.

  1. (Optional) Collecting Apache Tomcat Logs from a Log File

Follow the  steps below to capture Apache Tomcat logs from a log file on Kubernetes.

  1. Determine the location of the Apache Tomcat log file on Kubernetes. This can be determined from the Tomcat.conf for your Tomcat farm along with the mounts on the Tomcat pods.

  2. Install the Sumo Logic tailing sidecar operator.

  3. Add the following annotation in addition to the existing annotations.

annotations:
  tailing-sidecar: sidecarconfig;<mount>:<path_of_Tomcat_log_file>/<Tomcat_log_file_name>

Example:

annotations:
  tailing-sidecar: sidecarconfig;data:/opt/tomcat/logs/tomcat.log
  1. Make sure that the Tomcat pods are running and annotations are applied by using the command: kubectl describe pod <Tomcat_pod_name>

  2. Sumo Logic Kubernetes collection will automatically start collecting logs from the pods having the annotations defined above. 

  3. Verify logs in Sumo Logic.

3. Add an FER to normalize the fields in Kubernetes environments

Labels created in Kubernetes environments automatically are prefixed with pod_labels. To normalize these for our app to work, we need to create a Field Extraction Rule if not already created for WebServer Application Components. To do so:

  1. Go to Manage Data > Logs > Field Extraction Rules.

  2. Click the + Add button on the top right of the table.

  3. The following form appears:

  1. Enter the following options:

    • Rule Name. Enter the name as App Observability - Webserver.

    • Applied At. Choose Ingest Time

    • Scope. Select Specific Data

    • Scope: Enter the following keyword search expression: 

pod_labels_environment=* pod_labels_component=webserver pod_labels_webserver_farm=* pod_labels_webserver_system=*

  • Parse Expression.Enter the following parse expression:

if (!isEmpty(pod_labels_environment), pod_labels_environment, "") as environment

| pod_labels_component as component

| pod_labels_webserver_system as webserver_system

| pod_labels_webserver_farm as webserver_farm

 
5. Click Save to create the rule.