This tutorial describes how to expose JVM data by instrumentation points in applications, capture JVM data by using Prometheus Monitoring of Application Real-Time Monitoring Service (ARMS), and display JVM data on the ARMS Prometheus Grafana dashboard, finally to monitor JVM data with ARMS Prometheus Monitoring.

Background information

The following figure shows the workflow.

How it works

Step 1: Expose JVM data by instrumentation points in applications

Use JVM Exporter to expose JVM data in applications.

  1. Add the following information to the pom.xml file:
    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_hotspot</artifactId>
        <version>0.6.0</version>
    </dependency>
  2. Add the JVM Exporter dependency in the location where initialization is available.

    For example, add the dependency in the \src\main\java\com\monitise\prometheus_demo\DemoController.java file of the demo project.

    @PostConstruct
        public void initJvmExporter() {
            io.prometheus.client.hotspot.DefaultExports.initialize();
        }
  3. Expose the Port and Path fields used by Prometheus Monitoring in the \src\main\resources\application.properties file.
    management.port: 8081
    endpoints.prometheus.path: prometheus-metrics

    Enable the HTTP port.

    @SpringBootApplication
    // Set the Prometheus endpoint /prometheus-metrics
    @EnablePrometheusEndpoint
    // Export the data at /metrics at a Prometheus endpoint.
    @EnableSpringBootMetricsCollector
    public class PrometheusDemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(PrometheusDemoApplication.class, args);
        }
    }

Step 2: Deploy the application to the Container Service Kubernetes cluster

Deploy the application to the Container Service Kubernetes cluster so that ARMS Prometheus Monitoring can monitor and capture the JVM data.

  1. Run the following commands in the buildDockerImage.sh file by row:
    mvn clean install -DskipTests
    docker build -t <name of the local temporary Docker image>:<version of the local temporary Docker image> . --no-cache
    sudo docker tag <name of the local temporary Docker image>:<version of the local temporary Docker image> <registry domain name>/<namespace>/<image name>:<image version>
    sudo docker push <registry domain name>/<namespace>/<image name>:<image version>
    Example:
    mvn clean install -DskipTests
    docker build -t promethues-demo:v0 . --no-cache
    sudo docker tag promethues-demo:v0 registry.cn-hangzhou.aliyuncs.com/fuling/promethues-demo:v0
    sudo docker push registry.cn-hangzhou.aliyuncs.com/fuling/promethues-demo:v0
    In this step, a Docker image named promethues-demo is created, and the image is pushed to Alibaba Cloud Docker Registry.
  2. In the left-side navigation pane, choose Clusters > Clusters. On the Clusters page, find the target cluster, and click Dashboard in the Actions column.
    Kubernetes cluster console button
  3. In the left-side navigation pane, choose Workloads > Deployments. In the upper-right corner, click CREATE, and enter the following information in the CREATE FROM TEXT INPUT section:
    Note In the following configuration file, values of prometheus.io/port and prometheus.io/path are the ARMS Prometheus Monitoring port and path exposed in Step 1:
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: prometheus-demo
    spec:
      replicas: 2
      template:
        metadata:
          annotations:
            prometheus.io/scrape: 'true'
            prometheus.io/path: '/prometheus-metrics'
            prometheus.io/port: '8081'
          labels:
            app: tomcat
        spec:
          containers:
          - name: tomcat
            imagePullPolicy: Always
            image: registry.cn-hangzhou.aliyuncs.com/fuling/promethues-demo:v0
            ports:
            - containerPort: 8080
              name: tomcat-normal
            - containerPort: 8081
              name: tomcat-monitor
    In this step, the promethues-demo Docker image created in 1 is deployed to the Container Service Kubernetes cluster.
  4. In the left-side navigation pane, choose Discovery and Load Balancing > Services. In the upper-right corner, click CREATE, and enter the following information in the CREATE FROM TEXT INPUT section:
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: tomcat
      name: tomcat
      namespace: default
    spec:
      ports:
      - name: tomcat-normal
        port: 8080
        protocol: TCP
        targetPort: 8080
      - name: tomcat-monitor
        port: 8081
        protocol: TCP
        targetPort: 8081
      type: NodePort
      selector:
        app: tomcat

Step 3: Configure ARMS Prometheus Monitoring to capture the JVM data

Configure ARMS Prometheus Monitoring in the ARMS console to capture the JVM data.

  1. In the left-side navigation pane, click Prometheus Monitoring.
  2. On the top of the Prometheus monitoring page, select the region where the Container Service Kubernetes cluster is located. Find the target cluster, and click Installation in the Actions column.
  3. After the ARMS Prometheus agent is installed, find the target cluster, and click Settings in the Actions column.
  4. On the Details tab, click Add ServiceMonitor. In the Add ServiceMonitor dialog box, enter the following information:
    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      # Enter a unique name.
      name: tomcat-demo
      # Enter the target namespace.
      namespace: default
    spec:
      endpoints:
      - interval: 30s
        # Enter the value of the Name field for Port of Prometheus Exporter.
        port: tomcat-monitor
        # Enter the value of the Path field for Prometheus Exporter.
        path: /prometheus-metrics
      namespaceSelector:
        any: true
      selector:
        matchLabels:
          app: tomcat

Step 4: Display the JVM data on the Grafana dashboard

Import the Grafana dashboard template in the ARMS console and specify the Container Service Kubernetes cluster where the Prometheus data source is located.

  1. Go to Host Dashboard.
  2. In the left-side navigation pane, choose + > Import, enter 10877 in the Grafana.com Dashboard field, and click Load.
    Import Grafana dashboard
  3. On the Import page, set the following information and click Import.
    Import Grafana dashboard with options
    1. Enter a custom dashboard name in the Name field.
    2. Select your Container Service Kubernetes cluster from the Folder drop-down list.
    3. Select your Container Service Kubernetes cluster from the Select a Prometheus data source drop-down list.
    After the configuration is complete, the ARMS Prometheus Grafana JVM dashboard appears, as shown in the following figure.ARMS Prometheus Grafana dashboard for JVM

Step 5: Create an alert

What to do next

After the ARMS Prometheus Grafana JVM dashboard is configured, you can view Prometheus Monitoring metrics and customize the dashboard. For more information, see the following documents.