云容器引擎 CCE-使用云原生监控插件监控自定义指标:方法二:配置Service Annotations监控自定义指标

时间:2024-05-31 08:37:34

方法二:配置Service Annotations监控自定义指标

当Service的Annotations配置符合Prometheus采集规范的规则后,Prometheus会自动采集这些Service暴露的指标。

Service Annotations使用方法和Pod Annotations基本相同,主要是采集的指标的适用场景不同,Pod Annotations更关注Pod的资源使用情况,Service Annotations侧重于对该业务的请求等指标。

部署示例应用如下:

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-test
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-test
  template:
    metadata:
      labels:
        app: nginx-test
    spec:
      containers:
        - name: container-0
          image: 'nginx:exporter'      # 替换为您上传到SWR的镜像地址
          resources:
            limits:
              cpu: 250m
              memory: 512Mi
            requests:
              cpu: 250m
              memory: 512Mi
        - 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-test
  labels:
    app: nginx-test
  namespace: default
  annotations: 
    prometheus.io/scrape: "true"  # 配置为 true 表示开启服务发现
    prometheus.io/port: "9113"  # 配置为采集指标暴露的端口号
    prometheus.io/path: "/metrics" # 填写指标暴露的 URI 路径,一般是 /metrics
spec:
  selector:
    app: nginx-test
  externalTrafficPolicy: Cluster
  ports:
    - name: cce-service-0
      targetPort: 80
      nodePort: 0
      port: 8080
      protocol: TCP
    - name: cce-service-1
      protocol: TCP
      port: 9113
      targetPort: 9113
  type: NodePort

查看指标,通过Service名称可以判断出该指标是根据Service配置上报的。

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"}
图3 查看监控指标
support.huaweicloud.com/usermanual-cce/cce_10_0373.html