Monitor JVM with Prometheus
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
-
A Container Service for Kubernetes (ACK) cluster connected to Managed Service for Prometheus. For more information, see Container observability.
-
An image repository in Container Registry. For more information, see Step 2: Create an image repository.
Demo
To get started quickly, use the pre-instrumented demo project.
Workflow
The following figure shows the end-to-end JVM monitoring workflow.

Step 1: Instrument application
Add Prometheus client libraries to your application so that it exposes JVM metrics:
-
Add the Maven dependency to the pom.xml file.
<dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_hotspot</artifactId> <version>0.6.0</version> </dependency> -
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.
-
In the application.properties file, configure the port and path for Managed Service for Prometheus.
management.port: 8081 endpoints.prometheus.path: prometheus-metricsFor an example, see the /src/main/resources/application.properties file in the demo project.
-
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:
-
Run the following command to recompile the module.
mvn clean install -DskipTests -
Run the following command to build the image.
docker build -t <local-temporary-docker-image-name>:<local-temporary-docker-image-tag> . --no-cacheSample command:
docker build -t promethues-demo:v0 . --no-cache -
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 -
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:v0The uploaded application image appears on the Tags page of the Container Registry console.
Step 3: Deploy application
Deploy the application to the ACK cluster:
-
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
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-monitorSample 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-monitorThe prometheus-demo application appears on the Deployments page with a replica count of 2/2.
-
Create a Service.
-
In the left-side navigation pane, choose .
-
On the Services page, click Create from YAML.
-
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.