Monitoring Custom Metrics Using Cloud Native Cluster Monitoring
CCE provides the Cloud Native Cluster Monitoring add-on to monitor custom metrics using Prometheus.
The following procedure uses an Nginx application as an example to describe how to use Prometheus to monitor custom metrics:
- Installing and Accessing Cloud Native Cluster Monitoring
CCE provides an add-on that integrates Prometheus functions. You can install it with several clicks.
- Preparing an Application
Prepare an application image. The application must provide a metric monitoring API for Prometheus to collect data, and the monitoring data must comply with the Prometheus specifications.
- Monitoring Custom Metrics
Use the application image to deploy a workload in a cluster. Custom metrics will be automatically reported to Prometheus.
Use one of the following methods to monitor custom metrics:
- Method 1: Configuring Pod Annotations
- Method 2: Configuring Service Annotations
- Method 3: Configuring PodMonitor
- Method 4: Configuring ServiceMonitor
- Method 5: Configuring AdditionalScrapeConfigs
Prometheus Monitoring Data Collection
Prometheus periodically calls the metric monitoring API (/metrics by default) of an application to obtain monitoring data. The application needs to provide the metric monitoring API for Prometheus to call, and the monitoring data must meet the following specifications of Prometheus:
# TYPE nginx_connections_active gaugenginx_connections_active 2# TYPE nginx_connections_reading gaugenginx_connections_reading 0
Prometheus provides clients in various languages. For details about the clients, see Prometheus CLIENT LIBRARIES. For details about how to develop an exporter, see WRITING EXPORTERS. The Prometheus community provides various third-party exporters that can be directly used. For details, see EXPORTERS AND INTEGRATIONS.
Constraints
- To use Prometheus to monitor custom metrics, the application needs to provide a metric monitoring API. For details, see Prometheus Monitoring Data Collection.
- Currently, metrics in the kube-system and monitoring namespaces cannot be collected when pod and service annotations are used. To collect metrics in the two namespaces, use PodMonitor and ServiceMonitor.
- The nginx/nginx-prometheus-exporter:0.9.0 image is pulled for the Nginx application. You need to add an EIP for the node where the application is deployed or upload the image to SWR to prevent application deployment failures.
Installing and Accessing Cloud Native Cluster Monitoring
- Log in to the CCE console and click the cluster name to access the cluster console.
- In the navigation pane, choose Add-ons. On the displayed page, locate Cloud Native Cluster Monitoring and click Install.
When installing this add-on, pay attention to the following configurations. Configure other parameters as required. For details, see Cloud Native Cluster Monitoring.
- For 3.8.0 or later, enable custom metric collection.
- For 3.8.0 or earlier, do not enable custom metric collection.
- For 3.8.0 or later, enable custom metric collection.
- After this add-on is installed, deploy workloads and Services. The Prometheus server will be deployed as a StatefulSet in the monitoring namespace.
You can create a public network LoadBalancer Service so that Prometheus can be accessed from external networks.
- Log in to the CCE console and click the name of the cluster with Prometheus installed to access the cluster console. In the navigation pane, choose Services & Ingresses.
- Click Create from YAML in the upper right corner to create a public network LoadBalancer Service.apiVersion: v1kind: Servicemetadata:name: prom-lb # Service name, which is customizable.namespace: monitoringlabels:app: prometheuscomponent: serverannotations:kubernetes.io/elb.id: 038ff*** # Replace 038ff*** with the ID of the public network load balancer in the VPC that the cluster belongs to.spec:ports:- name: cce-service-0protocol: TCPport: 88 # Service port, which is customizable.targetPort: 9090 # Default Prometheus port. Retain the default value.selector: # The label selector can be adjusted based on the label of a Prometheus server instance.app.kubernetes.io/name: prometheusprometheus: servertype: LoadBalancer
- After the Service is created, enter Public IP address of the load balancer:Service port in the address box of the browser to access Prometheus.
Preparing an Application
User-developed applications must provide a metric monitoring API, and the monitoring data must comply with the Prometheus specifications. For details, see Prometheus Monitoring Data Collection.
This section uses Nginx as an example to describe how to collect monitoring data. There is a module named ngx_http_stub_status_module in Nginx, which provides basic monitoring functions. You can configure the nginx.conf file to provide an interface for external systems to access Nginx monitoring data.
- Log in to a Linux VM that can access to the Internet and run Docker commands.
- Create an nginx.conf file. Add the server configuration under http to enable Nginx to provide an interface for the external systems to access the monitoring data.user nginx;worker_processes auto;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events {worker_connections 1024;}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;include /etc/nginx/conf.d/*.conf;server {listen 8080;server_name localhost;location /stub_status {stub_status on;access_log off;}}}
- Use this configuration to create an image and a Dockerfile file.vi Dockerfile
The content of Dockerfile is as follows:
FROM nginx:1.21.5-alpineADD nginx.conf /etc/nginx/nginx.confEXPOSE 80CMD ["nginx", "-g", "daemon off;"] - Use this Dockerfile to create an image and upload it to SWR. The image name is nginx:exporter.
- In the navigation pane, choose My Images. In the upper right corner, click Upload Through Client. In the displayed dialog box, click Generate a temporary login command. Then, click
to copy the command.
- Run the login command copied in the previous step on the node. If the login is successful, the message "Login Succeeded" is displayed.
- Run the following command to build an image named nginx. The image version is exporter.docker build -t nginx:exporter .
- Tag the image and upload it to the image repository. Change the image repository address and organization name based on your requirements.docker tag nginx:exporter {swr-address}/{group}/nginx:exporterdocker push {swr-address}/{group}/nginx:exporter
- In the navigation pane, choose My Images. In the upper right corner, click Upload Through Client. In the displayed dialog box, click Generate a temporary login command. Then, click
- View application metrics.
- Use nginx:exporter to create a workload.
- Log in to the container and obtain the Nginx monitoring data through http://<ip_address>:8080/stub_status. <ip_address> indicates the IP address of the container.curl http://127.0.0.1:8080/stub_status
The command output is as follows:
Active connections: 3server accepts handled requests146269 146269 212Reading: 0 Writing: 1 Waiting: 2
Method 1: Configuring Pod Annotations
When the annotation settings of pods comply with the Prometheus data collection rules, Prometheus automatically collects the metrics exposed by the pods.
The format of the monitoring data provided by nginx:exporter does not meet the requirements of Prometheus. Convert the data format to the format required by Prometheus. To convert the format of Nginx metrics, use nginx-prometheus-exporter. Deploy nginx:exporter and nginx-prometheus-exporter in the same pod and add the following annotations during deployment. Then Prometheus can automatically collect metrics.
kind: DeploymentapiVersion: apps/v1metadata:name: nginx-exporternamespace: defaultspec:replicas: 1selector:matchLabels:app: nginx-exportertemplate:metadata:labels:app: nginx-exporterannotations:prometheus.io/scrape: "true"prometheus.io/port: "9113"prometheus.io/path: "/metrics"prometheus.io/scheme: "http"spec:containers:- name: container-0image: 'nginx:exporter' # Replace it with the address of the image you uploaded to SWR.resources:limits:cpu: 250mmemory: 512Mirequests:cpu: 250mmemory: 512Mi- name: container-1image: 'nginx/nginx-prometheus-exporter:0.9.0'command:- nginx-prometheus-exporterargs:- '-nginx.scrape-uri=http://127.0.0.1:8080/stub_status'imagePullSecrets:- name: default-secret
Where,
- prometheus.io/scrape indicates whether to enable Prometheus to collect pod monitoring data. The value is true.
- prometheus.io/port indicates the port for collecting monitoring data, which varies depending on the application. In this example, the port is 9113.
- prometheus.io/path indicates the URL of the API for collecting monitoring data. If this parameter is not set, the default value /metrics is used.
- prometheus.io/scheme: protocol used for data collection. The value can be http or https.
After the application is successfully deployed, access Prometheus to query custom metrics by job name.
The custom metrics related to Nginx can be queried. In the following, the job name indicates that the metrics are reported based on the pod configuration.
nginx_connections_accepted{cluster="2048c170-8359-11ee-9527-0255ac1000cf", cluster_category="CCE", cluster_name="cce-test", container="container-0", instance="10.0.0.46:9113", job="monitoring/kubernetes-pods", kubernetes_namespace="default", kubernetes_pod="nginx-exporter-77bf4d4948-zsb59", namespace="default", pod="nginx-exporter-77bf4d4948-zsb59", prometheus="monitoring/server"}
Method 2: Configuring Service Annotations
When the annotation settings of Services comply with the Prometheus data collection rules, Prometheus automatically collects the metrics exposed by the Services.
You can use Service annotations in the same way as pod annotations. However, their application scenarios are different. Pod annotations focus on pod resource usage metrics while Service annotations focus on metrics such as requests for a Service.
The following is an example configuration:
kind: DeploymentapiVersion: apps/v1metadata:name: nginx-testnamespace: defaultspec:replicas: 1selector:matchLabels:app: nginx-testtemplate:metadata:labels:app: nginx-testspec:containers:- name: container-0image: 'nginx:exporter' # Replace it with the address of the image you uploaded to SWR.resources:limits:cpu: 250mmemory: 512Mirequests:cpu: 250mmemory: 512Mi- name: container-1image: 'nginx/nginx-prometheus-exporter:0.9.0'command:- nginx-prometheus-exporterargs:- '-nginx.scrape-uri=http://127.0.0.1:8080/stub_status'imagePullSecrets:- name: default-secret
The following is an example Service configuration:
apiVersion: v1kind: Servicemetadata:name: nginx-testlabels:app: nginx-testnamespace: defaultannotations:prometheus.io/scrape: "true" # Value true indicates that service discovery is enabled.prometheus.io/port: "9113" # Set it to the port on which metrics are exposed.prometheus.io/path: "/metrics" # Enter the URI path under which metrics are exposed. Generally, the value is /metrics.spec:selector:app: nginx-testexternalTrafficPolicy: Clusterports:- name: cce-service-0targetPort: 80nodePort: 0port: 8080protocol: TCP- name: cce-service-1protocol: TCPport: 9113targetPort: 9113type: NodePort
After the application is successfully deployed, access Prometheus to query custom metrics. In the following, the Service name indicates the metrics are reported based on the Service configuration.
nginx_connections_accepted{app="nginx-test", cluster="2048c170-8359-11ee-9527-0255ac1000cf", cluster_category="CCE", cluster_name="cce-test", instance="10.0.0.38:9113", job="nginx-test", kubernetes_namespace="default", kubernetes_service="nginx-test", namespace="default", pod="nginx-test-78cfb65889-gtv7z", prometheus="monitoring/server", service="nginx-test"}
Method 3: Configuring PodMonitor
Cloud Native Cluster Monitoring allows you to configure metric collection tasks based on PodMonitor and ServiceMonitor. Prometheus Operator watches PodMonitor. The reload mechanism of Prometheus is used to trigger a hot update of the Prometheus collection tasks to the Prometheus instance.
To use CRDs defined by Prometheus Operator on GitHub, visit https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack/charts/crds/crds.
The following is an example configuration:
apiVersion: apps/v1kind: Deploymentmetadata:name: nginx-test2namespace: defaultspec:replicas: 1selector:matchLabels:app: nginx-test2template:metadata:labels:app: nginx-test2spec:containers:- image: nginx:exporter # Replace it with the address of the image you uploaded to SWR.name: container-0ports:- containerPort: 9113 # Port on which metrics are exposed.name: nginx-test2 # Application name used when PodMonitor is configured.protocol: TCPresources:limits:cpu: 250mmemory: 300Mirequests:cpu: 100mmemory: 100Mi- name: container-1image: 'nginx/nginx-prometheus-exporter:0.9.0'command:- nginx-prometheus-exporterargs:- '-nginx.scrape-uri=http://127.0.0.1:8080/stub_status'imagePullSecrets:- name: default-secret
The following is an example PodMonitor configuration:
apiVersion: monitoring.coreos.com/v1kind: PodMonitormetadata:name: podmonitor-nginx # PodMonitor namenamespace: monitoring # Namespace that PodMonitor belongs to. monitoring is recommended.spec:namespaceSelector: # An selector matching the namespace where the workload is locatedmatchNames:- default # Namespace that the workload belongs tojobLabel: podmonitor-nginxpodMetricsEndpoints:- interval: 15spath: /metrics # Path under which metrics are exposed by the workloadport: nginx-test2 # Port on which metrics are exposed by the workloadtlsConfig:insecureSkipVerify: trueselector:matchLabels:app: nginx-test2 # Label carried by the pod, which can be selected by the selector
After the application is successfully deployed, access Prometheus to query custom metrics. In the following, the job name indicates the metrics are reported based on the PodMonitor configuration.
nginx_connections_accepted{cluster="2048c170-8359-11ee-9527-0255ac1000cf", cluster_category="CCE", cluster_name="cce-test", container="container-0", endpoint="nginx-test2", instance="10.0.0.44:9113", job="monitoring/podmonitor-nginx", namespace="default", pod="nginx-test2-746b7f8fdd-krzfp", prometheus="monitoring/server"}
Method 4: Configuring ServiceMonitor
Cloud Native Cluster Monitoring allows you to configure metric collection tasks based on PodMonitor and ServiceMonitor. Prometheus Operator watches ServiceMonitor. The reload mechanism of Prometheus is used to trigger a hot update of the Prometheus collection tasks to the Prometheus instance.
To use CRDs defined by Prometheus Operator on GitHub, visit https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack/charts/crds/crds.
The following is an example configuration:
apiVersion: apps/v1kind: Deploymentmetadata:name: nginx-test3namespace: defaultspec:replicas: 1selector:matchLabels:app: nginx-test3template:metadata:labels:app: nginx-test3spec:containers:- image: nginx:exporter # Replace it with the address of the image you uploaded to SWR.name: container-0resources:limits:cpu: 250mmemory: 300Mirequests:cpu: 100mmemory: 100Mi- name: container-1image: 'nginx/nginx-prometheus-exporter:0.9.0'command:- nginx-prometheus-exporterargs:- '-nginx.scrape-uri=http://127.0.0.1:8080/stub_status'imagePullSecrets:- name: default-secret
The following is an example Service configuration:
apiVersion: v1kind: Servicemetadata:name: nginx-test3labels:app: nginx-test3namespace: defaultspec:selector:app: nginx-test3externalTrafficPolicy: Clusterports:- name: cce-service-0targetPort: 80nodePort: 0port: 8080protocol: TCP- name: servicemonitor-portsprotocol: TCPport: 9113targetPort: 9113type: NodePort
The following is an example ServiceMonitor configuration:
apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata:name: servicemonitor-nginxnamespace: monitoringspec:# Configure the name of the port on which metrics are exposed.endpoints:- path: /metricsport: servicemonitor-portsjobLabel: servicemonitor-nginx# Application scope of a collection task. If this parameter is not set, the default value default is used.namespaceSelector:matchNames:- defaultselector:matchLabels:app: nginx-test3
After the application is successfully deployed, access Prometheus to query custom metrics. In the following, the endpoint name indicates the metrics are reported based on the ServiceMonitor configuration.
nginx_connections_accepted{cluster="2048c170-8359-11ee-9527-0255ac1000cf", cluster_category="CCE", cluster_name="cce-test", endpoint="servicemonitor-ports", instance="10.0.0.47:9113", job="nginx-test3", namespace="default", pod="nginx-test3-6f8bccd9-f27hv", prometheus="monitoring/server", service="nginx-test3"}
Method 5: Configuring AdditionalScrapeConfigs
Cloud Native Cluster Monitoring 3.10.1 or later has been installed.
AdditionalScrapeConfigs allows you to specify a secret key to attach your additional Prometheus scrape configuration to Cloud Native Cluster Monitoring.
This mechanism bypasses the common scrape configuration generation logic and directly transfers the configuration to Prometheus. Therefore, you need to ensure that the configuration is correct. You are advised to refer to the official scrape_config documentation.
- Use kubectl to connect to the cluster. For details, see Accessing a Cluster Using kubectl.
- Use YAML to create the following secret:kind: SecretapiVersion: v1type: Opaquemetadata:name: additional-scrape-configsnamespace: monitoring # monitoring is only an example. The namespace must be the same as that of Cloud Native Cluster Monitoring.stringData:# The following is a metric collection example of Cloud Native Cluster Monitoring without local data storage. You need to replace the settings as needed.prometheus-additional.yaml: |-- job_name: custom-job-testmetrics_path: /metricsrelabel_configs:- action: keepsource_labels:- __meta_kubernetes_pod_label_app- __meta_kubernetes_pod_labelpresent_appregex: (prometheus-lightweight);true- action: keepsource_labels:- __meta_kubernetes_pod_container_port_nameregex: webkubernetes_sd_configs:- role: podnamespaces:names:- monitoring
- Edit the persistent-user-config configuration item to enable AdditionalScrapeConfigs.kubectl edit configmap persistent-user-config -n monitoring
Add --common.prom.default-additional-scrape-configs-key=prometheus-additional.yaml under operatorConfigOverride to enable AdditionalScrapeConfigs as follows:
...data:lightweight-user-config.yaml: |customSettings:additionalScrapeConfigs: []agentExtraArgs: []metricsDeprecated:globalDeprecateMetrics: []nodeExporterConfigOverride: []operatorConfigOverride:- --common.prom.default-additional-scrape-configs-key=prometheus-additional.yaml... - Go to the Grafana or AOM page to check whether your custom metrics are collected.
- Prometheus Monitoring Data Collection
- Constraints
- Installing and Accessing Cloud Native Cluster Monitoring
- Preparing an Application
- Method 1: Configuring Pod Annotations
- Method 2: Configuring Service Annotations
- Method 3: Configuring PodMonitor
- Method 4: Configuring ServiceMonitor
- Method 5: Configuring AdditionalScrapeConfigs