云容器引擎 CCE-使用云原生监控插件监控自定义指标:方法四:配置Service Monitor监控自定义指标
方法四:配置Service Monitor监控自定义指标
云原生监控插件提供了基于PodMonitor与ServiceMonitor配置指标采集任务的能力。Prometheus Operator将watch的ServiceMonitor的变化,通过Prometheus的reload机制,将Prometheus的采集任务热更新至Prometheus的实例中。
Prometheus Operator定义的CRD资源github地址:https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack/charts/crds/crds。
部署示例应用如下:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-test3 namespace: default spec: replicas: 1 selector: matchLabels: app: nginx-test3 template: metadata: labels: app: nginx-test3 spec: containers: - image: nginx:exporter # 替换为您上传到SWR的镜像地址 name: container-0 resources: limits: cpu: 250m memory: 300Mi requests: cpu: 100m memory: 100Mi - name: container-1 image: 'nginx/nginx-prometheus-exporter:0.9.0' command: - nginx-prometheus-exporter args: - '-nginx.scrape-uri=http://127.0.0.1:8080/stub_status' imagePullSecrets: - name: default-secret
部署示例Service如下:
apiVersion: v1 kind: Service metadata: name: nginx-test3 labels: app: nginx-test3 namespace: default spec: selector: app: nginx-test3 externalTrafficPolicy: Cluster ports: - name: cce-service-0 targetPort: 80 nodePort: 0 port: 8080 protocol: TCP - name: servicemonitor-ports protocol: TCP port: 9113 targetPort: 9113 type: NodePort
配置Service Monitor示例如下:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: servicemonitor-nginx
namespace: monitoring
spec:
# 配置service中的暴露指标的port的名称
endpoints:
- path: /metrics
port: servicemonitor-ports
jobLabel: servicemonitor-nginx
# 采集任务的作用范围,如果不配置,默认为default
namespaceSelector:
matchNames:
- default
selector:
matchLabels:
app: nginx-test3
查看指标,通过endpoint名称可以判断出该指标是根据ServiceMonitor配置上报的。
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"}