一、下载
1、下载文件
$ cd /opt/k8s/work/ $ wget -O kube-state-metrics.zip https://github.com/kubernetes/kube-state-metrics/archive/master.zip $ unzip kube-state-metrics.tar.gz $ cd kube-state-metrics-master/examples/standard $ ls cluster-role-binding.yaml cluster-role.yaml deployment.yaml service-account.yaml service.yaml
2、修改service.yaml
annotations: prometheus.io/scraped: "true"
cat service.yaml apiVersion: v1 kind: Service metadata: annotations: prometheus.io/scraped: "true" labels: app.kubernetes.io/name: kube-state-metrics app.kubernetes.io/version: 1.9.5 name: kube-state-metrics namespace: monitoring spec: clusterIP: None ports: - name: http-metrics port: 8080 targetPort: http-metrics - name: telemetry port: 8081 targetPort: telemetry selector: app.kubernetes.io/name: kube-state-metrics
二、修改prometheus配置文件
1、prometheus 追加配置,仅抓取带有prometheus.io/scraped: "true"
注解的endpoint
- job_name: "kubernetes-service-endpoints" kubernetes_sd_configs: - role: endpoints relabel_configs: - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scraped] action: keep regex: true - 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: service_name
2、prometheus重新加载配置后,查看对应的target