Monitor JVM with Prometheus

Updated at:

Expose JVM metrics from your application, collect them with Managed Service for Prometheus, visualize them on a Grafana dashboard, and create alert rules. This topic uses a Container Service for Kubernetes (ACK) cluster and Container Registry as an example.

Prerequisites

Demo

To get started quickly, use the pre-instrumented demo project.

Workflow

The following figure shows the end-to-end JVM monitoring workflow.

flow

Step 1: Instrument application

Add Prometheus client libraries to your application so that it exposes JVM metrics:

  1. Add the Maven dependency to the pom.xml file.

    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_hotspot</artifactId>
        <version>0.6.0</version>
    </dependency>
  2. In your application code, add a method to initialize the JVM exporter.

    @PostConstruct
        public void initJvmExporter() {
            io.prometheus.client.hotspot.DefaultExports.initialize();
        }

    For an example, see the /src/main/java/com/monitise/prometheus_demo/DemoController.java file in the demo project.

  3. In the application.properties file, configure the port and path for Managed Service for Prometheus.

    management.port: 8081
    endpoints.prometheus.path: prometheus-metrics

    For an example, see the /src/main/resources/application.properties file in the demo project.

  4. In your application code, add a method to open the HTTP port.

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

    For an example, see the /src/main/java/com/monitise/prometheus_demo/PrometheusDemoApplication.java file in the demo project.

Step 2: Build and push image

Build an image for the instrumented application and push it to Container Registry:

  1. Run the following command to recompile the module.

    mvn clean install -DskipTests
  2. Run the following command to build the image.

    docker build -t <local-temporary-docker-image-name>:<local-temporary-docker-image-tag> . --no-cache

    Sample command:

    docker build -t promethues-demo:v0 . --no-cache
  3. Run the following command to tag the image.

    sudo docker tag <local-temporary-docker-image-name>:<local-temporary-docker-image-tag> <registry-domain>/<namespace>/<image-name>:<image-tag>

    Sample command:

    sudo docker tag promethues-demo:v0 registry.cn-hangzhou.aliyuncs.com/testnamespace/promethues-demo:v0
  4. Run the following command to push the image to the image repository.

    sudo docker push <registry-domain>/<namespace>/<image-name>:<image-tag>

    Sample command:

    sudo docker push registry.cn-hangzhou.aliyuncs.com/testnamespace/promethues-demo:v0

    The uploaded application image appears on the Tags page of the Container Registry console.

Step 3: Deploy application

Deploy the application to the ACK cluster:

  1. Log on to the ACK console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

  3. On the Deployments page, click Create from YAML.

    apiVersion: apps/v1 # for versions before 1.8.0 use apps/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-domain>/<namespace>/<image-name>:<image-tag>
            ports:
            - containerPort: 8080
              name: tomcat-normal
            - containerPort: 8081
              name: tomcat-monitor

    Sample code:

    apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
    kind: Deployment
    metadata:
      name: prometheus-demo
      labels:
        app: tomcat
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: tomcat
      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/peiyu-test/prometheus-demo:v0
            ports:
            - containerPort: 8080
              name: tomcat-normal
            - containerPort: 8081
              name: tomcat-monitor

    The prometheus-demo application appears on the Deployments page with a replica count of 2/2.

  4. Create a Service.

    1. In the left-side navigation pane, choose Network > Services.

    2. On the Services page, click Create from YAML.

    3. On the Create page, paste the following content into the Template code editor and click Create.

      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

    The new Service appears on the Services page.

Step 4: Configure service discovery

Create a ServiceMonitor so that Managed Service for Prometheus can discover your application and scrape JVM metrics. For more information, see Create service discovery by using a ServiceMonitor.