Prometheus Operator服務自動發現
阿新 • • 發佈:2021-12-16
# 1.編寫prometheus-additional並在monitoring名稱空間建立secret資源 cat > prometheus-additional.yaml << EOF - job_name: 'kubernetes-service-endpoints' kubernetes_sd_configs: - role: endpoints relabel_configs: - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape] action: keep regex: true - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme] action: replace target_label: __scheme__ regex: (https?) - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path] action: replace target_label: __metrics_path__ regex: (.+) - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port] action: replace target_label: __address__ regex: ([^:]+)(?::\d+)?;(\d+) replacement: $1:$2 - action: labelmap regex: __meta_kubernetes_service_label_(.+) - source_labels: [__meta_kubernetes_namespace] action: replace target_label: kubernetes_namespace - source_labels: [__meta_kubernetes_service_name] action: replace target_label: kubernetes_name EOF kubectl create secret generic additional-configs --from-file=prometheus-additional.yaml -n monitoring # 2.手動修改kube-prometheus/manifests中的prometheus-prometheus.yaml檔案增加additionalScrapeConfigs配置段 vim kube-prometheus/manifests/prometheus-prometheus.yaml additionalScrapeConfigs: name: additional-configs key: prometheus-additional.yaml # 並重新應用prometheus-prometheus.yaml檔案 kubectl apply -f kube-prometheus/manifests/prometheus-prometheus.yaml # 3.增加授權。執行Prometheus的ServiceAccount名為prometheus-k8s綁定了名為prometheus-k8s的ClusterRole。 vim prometheus-clusterRole.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus-k8s rules: - apiGroups: - "" resources: - nodes - services - endpoints - pods - nodes/proxy verbs: - get - list - watch - apiGroups: - "" resources: - configmaps - nodes/metrics verbs: - get - nonResourceURLs: - /metrics verbs: - get kubectl apply -f prometheus-clusterRole.yaml # 4.驗證。在需要監控的服務service增加annotations欄位以實現自動發現。 kubectl edit service grafana -n monitoring annotations: prometheus.io/scrape: "true" prometheus.io/port: "3000"