【2020.07.06】kube-prometheus中如何添加新scrape api?

上一篇博客中,有说到用kube-prometheus安装prometheus全家桶,用来监控,其中演示了如何通过配置文件的形式来添加,它有点不好的地方,就要修改,可能还需要重启,不是自动的,不是特别方便。


下面介绍一个它自动服务发现的方式:

prometheus-operator

自动发现scrape api主要得力于prometheus-operator这个项目,这个项目是方便管理prometheus在k8s集群中的自定义资源、配置、监控对象等,十分的方便。

【2020.07.06】kube-prometheus中如何添加新scrape api?

其中,自定义资源ServiceMonitor它就能自动的获取到符合要求的metrics指标

自定义ServiceMonitor实现metrics指标自动获取(场景1)

  • 此方法适用于,你暴露metrics的service的namespacemonitoring下的
  • service中,添加annotations

  1. 【2020.07.06】kube-prometheus中如何添加新scrape api?
  2. 上面2个key,就是约定的,必须要有的,这样才能自动探测到,注意port要和service保持 一致
  • 新增servicemonitor定义

    • 【2020.07.06】kube-prometheus中如何添加新scrape api?
  • 验证

    • 【2020.07.06】kube-prometheus中如何添加新scrape api?
    • 至此就自动发现了,成功!
  • 附上完整文件
    • 【2020.07.06】kube-prometheus中如何添加新scrape api?
        1 apiVersion: apps/v1
        2 kind: Deployment
        3 metadata:
        4   name: rabbitmq-exporter-auto
        5   namespace: monitoring
        6   labels:
        7     app: rabbitmq-exporter-auto
        8     author: jamestao
        9 spec:
       10   replicas: 1
       11   selector:
       12     matchLabels:
       13       app: rabbitmqpod-auto
       14   template:
       15     metadata:
       16       name: rabbitmq-exporter-pod-auto
       17       labels:
       18         app: rabbitmqpod-auto
       19     spec:
       20       restartPolicy: Always
       21       containers:
       22         - name: rabbitmq-exporter-container-auto
       23           image: kbudde/rabbitmq-exporter:latest
       24           resources:
       25             requests:
       26               memory: 10Mi
       27           ports:
       28             - containerPort: 9419
       29           env:
       30             - name: RABBIT_URL
       31               value: http://192.168.26.91:15672
       32             - name: RABBIT_USER
       33               value: james
       34             - name: RABBIT_PASSWORD
       35               value: "666666"
       36             - name: INCLUDE_VHOST
       37               value: ".*"
       38             - name: INCLUDE_QUEUES
       39               value: ".*"
       40 ---
       41 apiVersion: v1
       42 kind: Service
       43 metadata:
       44   name: svc-rabbitmq-management-auto
       45   labels:
       46     app: rabbitmq-exporter-svc-auto
       47   namespace: monitoring
       48   annotations:
       49     prometheus.io/port: "39418"
       50     prometheus.io/scrape: "true"
       51 spec:
       52   selector:
       53     app: rabbitmqpod-auto
       54   ports:
       55     - protocol: TCP
       56       port: 39418
       57       targetPort: 9419
       58       name: metrics
       59 ---
       60 apiVersion: monitoring.coreos.com/v1
       61 kind: ServiceMonitor
       62 metadata:
       63   name: taozhiqiang
       64   namespace: monitoring
       65 spec:
       66   endpoints:
       67   - interval: 15s
       68     port: metrics
       69   namespaceSelector:
       70     matchNames:
       71     - monitoring
       72   selector:
       73     matchLabels:
       74       app: rabbitmq-exporter-svc-auto
      rabbitmq_exporter_auto.yaml

    自定义ServiceMonitor实现metrics指标自动获取(场景2)

    • 此方法适用于,你暴露metrics的service的namespace不在monitoring下(kube-prometheus它默认安装在monitoring),不在monitoring下的server想要被prometheus-operator所探测到,需要创建sa权限绑定。
    • 主要需要新增这2节:【2020.07.06】kube-prometheus中如何添加新scrape api?
    • 附上完整文件:
      【2020.07.06】kube-prometheus中如何添加新scrape api?
        1 apiVersion: apps/v1
        2 kind: Deployment
        3 metadata:
        4   name: rabbitmq-exporter-auto
        5   namespace: bookstore
        6   labels:
        7     app: rabbitmq-exporter-auto
        8     author: jamestao
        9 spec:
       10   replicas: 1
       11   selector:
       12     matchLabels:
       13       app: rabbitmqpod-auto
       14   template:
       15     metadata:
       16       name: rabbitmq-exporter-pod-auto
       17       labels:
       18         app: rabbitmqpod-auto
       19     spec:
       20       restartPolicy: Always
       21       containers:
       22         - name: rabbitmq-exporter-container-auto
       23           image: kbudde/rabbitmq-exporter:latest
       24           resources:
       25             requests:
       26               memory: 10Mi
       27           ports:
       28             - containerPort: 9419
       29           env:
       30             - name: RABBIT_URL
       31               value: http://192.168.26.91:15672
       32             - name: RABBIT_USER
       33               value: james
       34             - name: RABBIT_PASSWORD
       35               value: "666666"
       36             - name: INCLUDE_VHOST
       37               value: ".*"
       38             - name: INCLUDE_QUEUES
       39               value: ".*"
       40 ---
       41 apiVersion: v1
       42 kind: Service
       43 metadata:
       44   name: svc-rabbitmq-management-auto
       45   labels:
       46     app: rabbitmq-exporter-svc-auto
       47   namespace: bookstore
       48   annotations:
       49     prometheus.io/port: "39418"
       50     prometheus.io/scrape: "true"
       51 spec:
       52   selector:
       53     app: rabbitmqpod-auto
       54   ports:
       55     - protocol: TCP
       56       port: 39418
       57       targetPort: 9419
       58       name: metrics
       59 ---
       60 apiVersion: monitoring.coreos.com/v1
       61 kind: ServiceMonitor
       62 metadata:
       63   name: taozhiqiang-auto
       64   namespace: monitoring
       65 spec:
       66   endpoints:
       67   - interval: 15s
       68     port: metrics
       69   namespaceSelector:
       70     matchNames:
       71     - bookstore
       72   selector:
       73     matchLabels:
       74       app: rabbitmq-exporter-svc-auto
       75 ---
       76 apiVersion: rbac.authorization.k8s.io/v1
       77 kind: RoleList
       78 items:
       79 - apiVersion: rbac.authorization.k8s.io/v1
       80   kind: Role
       81   metadata:
       82     name: prometheus-k8s-rabbitmq
       83     namespace: bookstore
       84   rules:
       85   - apiGroups:
       86     - ""
       87     resources:
       88     - services
       89     - endpoints
       90     - pods
       91     verbs:
       92     - get
       93     - list
       94     - watch
       95 ---
       96 apiVersion: rbac.authorization.k8s.io/v1
       97 kind: RoleBindingList
       98 items:
       99 - apiVersion: rbac.authorization.k8s.io/v1
      100   kind: RoleBinding
      101   metadata:
      102     name: prometheus-k8s-rabbitmq
      103     namespace: bookstore
      104   roleRef:
      105     apiGroup: rbac.authorization.k8s.io
      106     kind: Role
      107     name: prometheus-k8s-rabbitmq
      108   subjects:
      109   - kind: ServiceAccount
      110     name: prometheus-k8s
      111     namespace: monitoring
      rabbitmq_exporter_auto_out_monitoring.yaml

    2种场景请仔细阅读文件信息,对比下差异,就好理解了。

    【2020.07.06】kube-prometheus中如何添加新scrape api?

    上一篇:django的View和drf的APIView源码解析


    下一篇:CBV的view源码和APIview源码分析