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
下面是解釋每一個字段意思:node
- 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