All Products
Search
Document Center

Application Real-Time Monitoring Service:Use ServiceMonitors to discover and monitor Services

Last Updated:Nov 20, 2023

Alibaba Cloud Managed Service for Prometheus allows you to create ServiceMonitors based on custom resource definitions (CRDs) to specify the Services that need to be discovered and monitored. By using a ServiceMonitor, you can define the namespaces of Services to be discovered in a pod, and use the matchLabel field to specify the Services to be monitored. This topic describes how to create a ServiceMonitor based on the Spring Boot framework to discover and monitor Services.

Demo

You can download the demo to try 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.ServiceMonitor返回示例

Step 2: Deploy a Kubernetes cluster

  1. Build an image and upload the Dockerfile of the image to the image repository. For more information, see Bind a source code hosting platform.

  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    ## Note: Specify a port name, rather than a port number. 
  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 Managed Service for Prometheus uses to collect metric data. The value of 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 Managed Service for Prometheus collects metric data by using the current endpoint. Unit: seconds. In this example, Managed Service for Prometheus collects metric data at intervals of 15s.

    • path: the path for storing the metric data collected by Managed Service for 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.

      Important

      Specify a port name, rather than a port number.

  • 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 specifies the conditions used to filter Services.

    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 Managed Service for Prometheus to create a ServiceMonitor based on the following two methods. Select a method as needed.

Use the ARMS or Managed Service for Prometheus console to create a ServiceMonitor

  1. Log on to the ARMS console.
  2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
  3. Click the name of the Prometheus instance to which you want to connect.
  4. In the left-side navigation pane, click Service Discovery. Then, click the Configure tab.

  5. On the Configure tab, click the ServiceMonitor tab. Then, click Add ServiceMonitor.

  6. In the dialog box that appears, enter the code in the YAML file and click OK:

Run a command to create a ServiceMonitor

  1. Save the YAML file to your computer.

  2. Run the following command to allow the YAML file to take effect:

    kubectl apply -f {Path of the YAML file}

Step 4: Verify the ServiceMonitor

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

  1. Log on to the ARMS console.
  2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
  3. Click the name of the Prometheus instance to which you want to connect.
  4. In the left-side navigation pane, click Service Discovery. Then, click the Targets tab.

    On the Targets tab, check whether a Service whose name is in the {namespace}/{serviceMonitorName}/x format exists.ServiceMonitor在Target页签显示

  5. Click the Show icon to the left of the Service name. Then, click the link in the Endpoint column of the table below the Service name.

    Check whether the metric data is collected as expected.ServiceMonitor的Target的endpoint