Alibaba Cloud Prometheus Service (Prometheus) allows you to create ServiceMonitors based on custom resource definitions (CRDs) to specify the Services to be discovered and monitored. A ServiceMonitor allows you to define the namespaces of Services to be discovered in a pod. A ServiceMonitor also allows you to use the matchLabels field to specify the Services to be monitored. This topic shows you how to create a ServiceMonitor based on the Spring Boot framework to discover and monitor Services.

Demo

You can download the demo to experience the features of ServiceMonitors.

Step 1: Add dependencies

  1. Create a Maven project and add the following dependencies to the pom.xml file:
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-registry-prometheus</artifactId>
                <version>1.6.6</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
  2. Add the following configuration to the src/resources/applications.properties file of the project:
    management.endpoints.web.exposure.include=prometheus
  3. Start the project and visit http://{host}:{port}/actuator/prometheus by using a browser.
    The information about Java Virtual Machine (JVM) monitoring is returned, as shown in the following figure.Sample return data

Step 2: Deploy a Kubernetes cluster

  1. Build an image and upload the Dockerfile of the image to the image repository.
  2. Create a Deployment based on the following sample code:
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: micrometer-prometheus
      namespace: default
      labels:
        app: demo-prometheus
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: demo-prometheus
      template:
        metadata:
          labels:
            app: demo-prometheus
        spec:
          containers:
            - name: micrometer-prometheus
              image: manjusakalza/micrometer-prometheus:latest
              ports:
                - containerPort: 8080
  3. Create a Service based on the following sample code:
    apiVersion: v1
    kind: Service
    metadata:
      name: prometheus-metrics-demo
      namespace: default
      labels:
        micrometer-prometheus-discovery: 'true'
    spec:
      selector:
        app: demo-prometheus
      ports:
        - protocol: TCP
          port: 8080
          targetPort: 8080
          name: metrics

Step 3: Create a ServiceMonitor

The following sample YAML file provides an example on how to configure a ServiceMonitor:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: micrometer-demo
  namespace: default
spec:
  endpoints:
    - interval: 15s
      path: /actuator/prometheus
      port: metrics
  namespaceSelector:
    any: true
  selector:
    matchLabels:
      micrometer-prometheus-discovery: 'true'

The sample YAML file contains the following fields:

  • The name and namespace fields in metadata: specify the key metadata required by the ServiceMonitor.
  • The endpoints field in spec: specifies the endpoints that Prometheus uses to collect metric data. The endpoints field is of the ARRAY type. Each element of the array is an endpoint. You can specify multiple endpoints. Each endpoint contains the following fields:
    • interval: the intervals at which Prometheus collects metric data by using the current endpoint. Unit: seconds. In this example, Prometheus collects metric data at intervals of 15s.
    • path: the path for storing the metric data collected by Prometheus. In this example, the path is set to /actuator/prometheus.
    • port: the port through which metric data is collected. Set this field to the value of the name field specified for the Service that you created in Step 2. In this example, the port is set to metrics.
  • The namespaceSelector field in spec: specifies the namespaces of Services to be discovered. The namespaceSelector field contains the following two mutually exclusive fields:
    • any: The value of this field is fixed to true. If this field is specified, the changes to all Services that meet the filter conditions specified by the selector field are monitored.
    • matchNames: an array value that specifies the namespaces of the Services to be monitored. For example, if you want to monitor only the Services in the default and arms-prom namespaces, use the following matchNames settings:
      namespaceSelector:
        matchNames:
        - default
        - arms-prom
  • The selector field in spec: the conditions used to filter Services. For more information, see LabelSelector v1 meta.

    In this example, the label of the Service that you created is micrometer-prometheus-discovery: 'true'. Therefore, the following selector settings are used:

    selector:
      matchLabels:
        micrometer-prometheus-discovery: 'true'

You can use Prometheus to create a ServiceMonitor based on the following two methods. Select a method as needed.

Use the Prometheus console to create a ServiceMonitor

  1. Log on to the ARMS console.
  2. In the left-side navigation pane, click Prometheus Monitoring.
  3. In the top navigation bar, select the region where your Kubernetes cluster is deployed.
  4. On the Prometheus Monitoring page, click the name of the Kubernetes cluster.
  5. In the left-side navigation pane, click Settings.
  6. On the page that appears, click the Service Discovery tab.
  7. On the Service Discovery tab, click the ServiceMonitor tab. Then, click Add ServiceMonitor.
  8. In the Add ServiceMonitor dialog box, enter the code in the YAML file and click OK:

Run a command to create a ServiceMonitor

  1. Save the YAML file to your local computer.
  2. Run the following command to make the YAML file take effect:
    kubectl apply -f [The path of the YAML file]
app: tomcat

Step 4: Verify the ServiceMonitor

Perform the following operations to check whether Prometheus can discover Services:

  1. Log on to the ARMS console.
  2. In the left-side navigation pane, click Prometheus Monitoring.
  3. In the top navigation bar, select the region where your Kubernetes cluster is deployed.
  4. On the Prometheus Monitoring page, click the name of the Kubernetes cluster.
  5. In the left-side navigation pane, click Settings.
  6. On the page that appears, click the Targets (beta) tab.
    On the Targets (beta) tab, check whether a Service whose name is in the {namespace}/{serviceMonitorName}/x format exists. Targets (beta) tab
  7. Click the Show icon before the Service name. Then, click the link in the Endpoint column of the table below the Service name.
    Check whether the metric data is as expected. Endpoint