Prometheus Operator配置Consul服务自动发现

1、在monitoring名称空间部署consul服务,consul.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: consul
  namespace: monitoring
  labels:
    app: consul
spec:
  selector:
    matchLabels:
      app: consul
  replicas: 1
  template:
    metadata:
      name: consul
      labels:
        app: consul
    spec:
      containers:
        - name: consul
          image: consul:latest
          ports:
            - containerPort: 8500
              protocol: TCP
          imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  name: consul
  namespace: monitoring
  labels:
    app: consul
spec:
  selector:
    app: consul
  ports:
    - name: consul
      protocol: TCP
      port: 8500
      targetPort: 8500

2、编写prometheus-additional配置,prometheus-additional.yaml

- job_name: consul
  consul_sd_configs:
    - server: consul.monitoring.svc:8500
  relabel_configs:
    - source_labels: [__meta_consul_tags]
      regex: .*,prome,.*
      action: keep
    - source_labels: [__meta_consul_service]
      target_label: job

3、创建secret资源

kubectl create secret generic additional-configs --from-file=prometheus-additional.yaml -n monitoring

4、增加additionalScrapeConfigs配置段

kubectl edit prometheus k8s -n monitoring
spec:
  additionalScrapeConfigs:
    name: additional-configs
    key: prometheus-additional.yaml
    ...

5、修改prometheus-server的service account权限

kubectl edit clusterrole prometheus-k8s -n monitoring
---
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
posted @ 2021-12-15 19:09  wanghongwei-dev  阅读(229)  评论(0编辑  收藏  举报