# 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"