k8s-prometheus 数据采集(node redis kubelet等)

apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: kube-ops
data:
  prometheus.yml: |
    global:
      scrape_interval: 20s
      scrape_timeout: 20s
    scrape_configs:
    - job_name: 'prometheus'
      static_configs:
      - targets: ['localhost:9090']
    - job_name: traefik
      static_configs:
        - targets: ['traefik-ingress-service.kube-system.svc.cluster.local:8080']
    - job_name: redis
      static_configs:
        - targets: ['redis:9121']
    - job_name: 'kubernetes-nodes'
      kubernetes_sd_configs:
      - role: node
      relabel_configs:
      - source_labels: [__address__]
        regex: '(.*):10250'
        replacement: '${1}:9100'
        target_label: __address__
        action: replace
      - action: labelmap
        regex: __meta_kubernetes_node_label_(.+)
    - job_name: 'kubelet'
      scheme: https
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      kubernetes_sd_configs:
      - role: node
      relabel_configs:
      - action: labelmap
        regex: __meta_kubernetes_node_label_(.+)
      - target_label: __address__
        replacement: kubernetes.default.svc:443
      - source_labels: [__meta_kubernetes_node_name]
        regex: (.+)
        target_label: __metrics_path__
        replacement: /api/v1/nodes/${1}/proxy/metrics

下面是解释每个字段意思:

- job_name: 'kubelet'
  # 通过https访问apiserver,通过apiserver的api获取数据
  scheme: https
  tls_config:
    ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
  bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
          
  #以k8s的角色(role)来定义收集,比如node,service,pod,endpoints,ingress等等 
  kubernetes_sd_configs:
  # 从k8s的node对象获取数据
  - role: node
  relabel_configs:
  # 用新的前缀代替原label name前缀,没有replacement的话功能就是去掉label_name前缀
  # 例如:以下两句的功能就是将__meta_kubernetes_node_label_kubernetes_io_hostname
  # 变为kubernetes_io_hostname
  - action: labelmap
    regex: __meta_kubernetes_node_label_(.+)
  # replacement中的值将会覆盖target_label中指定的label name的值,
  # 即__address__的值会被替换为kubernetes.default.svc:443
  - target_label: __address__
    replacement: kubernetes.default.svc:443
    #replacement: 10.142.21.21:6443
  # 获取__meta_kubernetes_node_name的值
  - source_labels: [__meta_kubernetes_node_name]
    #匹配一个或多个任意字符,将上述source_labels的值生成变量
    regex: (.+)
    # 将# replacement中的值将会覆盖target_label中指定的label name的值,
    # 即__metrics_path__的值会被替换为/api/v1/nodes/${1}/proxy/metrics,
    # 其中${1}的值会被替换为__meta_kubernetes_node_name的值
    target_label: __metrics_path__
    replacement: /api/v1/nodes/${1}/proxy/metrics
  #or:
  #- source_labels: [__address__]
  #  regex: '(.*):10250'
  #  replacement: '${1}:4194'
  #  target_label: __address__
  #- source_labels: [__meta_kubernetes_node_label_role]
  #  action: replace
  #  target_label: role

 

posted @ 2019-07-11 11:19  努力乄小白  阅读(1184)  评论(0编辑  收藏  举报