Service discovery integrates application metric endpoints in VKE clusters with VMP to collect custom metrics. This topic describes the service discovery methods supported by VKE clusters, the configuration steps, and how to verify metric collection.
Prometheus primarily uses the pull method to scrape the monitoring endpoints exposed by target services. Therefore, you need to configure service discovery rules in the cluster to specify scraping targets. In this way, you can collect data and write it to the workspace of VMP.
VKE clusters support the following CRD resources for configuring custom metric collection. The following table describes the concepts and use cases of the resources.
Implementing service discovery with ServiceMonitor
ServiceMonitor discovers backend Pods by matching Services. It is suitable for scenarios where an application exposes a metric port through a Service. After the configuration is complete, the system scrapes target metrics based on the port, path, and label rule settings defined in the ServiceMonitor. Procedure:
- In the left navigation bar, select Cluster. In the cluster list, find the target cluster.
- Click the cluster name to go to the Cluster configuration page.
- In the left navigation pane, click Workload > Resource Explorer, click Create with Yaml, and configure service discovery through ServiceMonitor.
- Select Custom from the Type drop-down menu.
- Edit the configurations in the Yaml box.
- Click Confirm to finish.
After you create the CRD in VKE, you can follow the steps below to view, edit, or delete the configuration.
- In the left navigation bar, select Cluster. In the cluster list, find the target cluster.
- Click the cluster name to go to the Cluster configuration page.
- In the left navigation pane, click Workload > Resource Explorer. From the Category drop-down list, select CRD to view created CRD resources.
- Click ServiceMonitor to go to the details page.
- Find the configuration you want to edit or delete. In the Actions column, click ..., then select Edit Yaml or Delete.
Implementing service discovery with PodMonitor
PodMonitor discovers scrape targets directly by Pod label. It is suitable for scenarios where target Pods do not expose metrics through a Service or when you want to scrape Pod metrics without using a Service. After the configuration is complete, the system scrapes target metrics based on the port, path, and label rule settings defined in the PodMonitor. Procedure:
- In the left navigation bar, select Cluster. In the cluster list, find the target cluster.
- Click the cluster name to go to the Cluster configuration page.
- In the left navigation pane, click Workload > Resource Explorer, click Create with Yaml, and configure service discovery through PodMonitor.
- Select Custom from the Type drop-down menu.
- Edit the configurations in the Yaml box.
- Click Confirm to finish.
After you create the CRD in VKE, you can follow the steps below to view, edit, or delete the configuration.
- In the left navigation bar, select Cluster. In the cluster list, find the target cluster.
- Click the cluster name to go to the Cluster configuration page.
- In the left navigation pane, click Workload > Resource Explorer. From the Category drop-down list, select CRD to view created CRD resources.
- Click PodMonitor to go to the details page.
- Find the configuration you want to edit or delete. In the Actions column, click ..., then select Edit Yaml or Delete.
Enable service discovery through VMNodeScrape
You can configure service discovery for node-level metrics through VMNodeScrape. This topic uses kubelet metric configuration as an example. Perform the following steps:
warning
- When configuring kubelet metrics, we recommend retaining the node discovery and TLS settings. Then, add the names of the additional metrics to collect, such as kubelet_evictions, to metricRelabelConfigs.
- Carefully select the scrape scope and metrics in custom collection rules. If your custom rules overlap with the cluster's default rules, metrics are scraped more than once, resulting in duplicate charges.
- In the left navigation bar, select Cluster. In the cluster list, find the target cluster.
- Click the cluster name to go to the Cluster configuration page.
- In the left navigation pane, click Workload > Resource Explorer, click Create with Yaml, and configure service discovery through VMNodeScrape.
- Select Custom from the Type drop-down menu.
- Edit the configurations in the Yaml box.
- Click Confirm to finish.
After you create a custom configuration in VKE, perform the following steps to view, edit, or delete it.
- In the left navigation bar, select Cluster. In the cluster list, find the target cluster.
- Click the cluster name to go to the Cluster configuration page.
- In the left navigation pane, click Workload > Resource Explorer. Select CRD from the Category drop-down list and search for vmnodescrape to view the configuration in the cluster.
- Click the resource name to go to its details page.
- Find the configuration you want to edit or delete. In the Actions column, click ..., then select Edit Yaml or Delete.
danger
Do not delete the default cluster configurations. Otherwise, cluster observability may not work as expected.
Implementing service discovery with service or pod annotations
You can use a ServiceMonitor or PodMonitor to implement service discovery. However, if a cluster contains many services or pods, the monitoring configuration becomes especially complex as you need to manually create multiple ServiceMonitor or PodMonitor objects.
To address this issue, VMP provides another method for service discovery: using service or pod annotations. You can add specified annotations when deploying a service or pod to enable rapid service discovery.
warning
- Using service or pod annotations is a fast method for service discovery. However, it also has some disadvantages. For example, it does not support metric relabeling. You should evaluate whether this method can meet your actual requirements.
- Service discovery based on service or pod annotations is not supported in the kube-system namespace. If necessary, we recommend using ServiceMonitor or PodMonitor.
When configuring a service or pod in the cluster, you can enable service discovery for custom targets by adding the following annotations.
tip
When you use prometheus.io/port: "9400" to specify the target port, explicitly declare the port in the container configuration as follows:
Configuring service discovery based on service annotations
When configuring a service in the cluster, you can add annotations to enable service discovery. This enables the service to be easily integrated into a monitoring solution. To do so, follow these steps:
- In the left navigation bar, click Cluster.
- On the Cluster page, click the name of the target cluster to go to the cluster management page.
- In the left navigation bar, click Services and Ingresses > Service. On the page that appears, click Create with Yaml to configure the service. During service configuration, add the annotations to enable easy service discovery.
Configuring service discovery based on pod annotations
When configuring a pod in the cluster, you can add annotations to enable service discovery. This enables the pod to be easily integrated into a monitoring solution. To do so, follow these steps:
- In the left navigation bar, click Cluster.
- On the Cluster page, click the name of the target cluster to go to the cluster management page.
- In the left navigation pane, click Workload > Deployment. Click Create with Yaml. Deploy the application and add annotations to enable service discovery.
tip
- This example uses a Deployment configuration for demonstration. For other workload types, the configuration is similar.
- The image address in this example is only for demonstration purposes. You need to replace it with your actual image address.
- Service discovery based on pod annotations does not support HTTP and HTTPS simultaneously.
After configuring service discovery, you can use the Explore feature of VMP to confirm whether the expected service has been discovered. Procedure:
- Select the target region in the top navigation bar.
- In the left navigation bar, click Explore to go to the Explore page.
- In the upper-right corner, specify the workspace from which to query metrics. In the Query text box, enter a statement in the up{job="xxx"} format to check whether the corresponding service is discovered. A metric value of 1 indicates that the corresponding service is correctly discovered.