1. 程式人生 > 實用技巧 >Prometheus基於service自動新增監控

Prometheus基於service自動新增監控

目錄

一:手動新增監控

1.建立SVC

apiVersion: v1
kind: Service
metadata:
  labels:
    app: apigateway-svc-exporter
    project: booyah
  name: apigateway-svc-exporter
  namespace: booyah-live
spec:
  clusterIP: None   # 沒有特殊要求
  ports:
  - name: prom
    port: 9500
    protocol: TCP
    targetPort: 9500
  selector:
    app: apigateway
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

2.基於DNS自動發現後端主機

- job_name: apigateway
  scrape_interval: 15s
  dns_sd_configs:
  - refresh_interval: 60s
    type: A
    port: 9500
    names:
    - apigateway-svc-exporter.booyah-live.svc.cluster.local

二:基於annotations自動新增監控

1.建立svc時,新增annotation注視資訊

apiVersion: v1
kind: Service
metadata:
  annotations:
    # Enable automatic monitoring of all instances when Prometheus is running in the cluster.
    prometheus.io/scrape: "true"   # 只有為true時,才會新增監控
    prometheus.io/path: "/metrics" # 監控的url
    prometheus.io/port: "9500" # 埠
  labels:
    app: apigateway-svc-exporter
    project: booyah
  name: apigateway-svc-exporter
  namespace: booyah-live
spec:
  clusterIP: None   # 沒有特殊要求
  ports:
  - name: prom
    port: 9500
    protocol: TCP
    targetPort: 9500
  selector:
    app: apigateway
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

2.配置prom發現規則(helm安裝的預設就有)

- job_name: kubernetes-service-endpoints
      kubernetes_sd_configs:
      - role: endpoints
      relabel_configs:
      - action: keep
        regex: true
        source_labels:
        - __meta_kubernetes_service_annotation_prometheus_io_scrape
      - action: replace
        regex: (https?)
        source_labels:
        - __meta_kubernetes_service_annotation_prometheus_io_scheme
        target_label: __scheme__
      - action: replace
        regex: (.+)
        source_labels:
        - __meta_kubernetes_service_annotation_prometheus_io_path
        target_label: __metrics_path__
      - action: replace
        regex: ([^:]+)(?::\d+)?;(\d+)
        replacement: $1:$2
        source_labels:
        - __address__
        - __meta_kubernetes_service_annotation_prometheus_io_port
        target_label: __address__
      - action: labelmap
        regex: __meta_kubernetes_service_label_(.+)
      - action: replace
        source_labels:
        - __meta_kubernetes_namespace
        target_label: kubernetes_namespace
      - action: replace
        source_labels:
        - __meta_kubernetes_service_name
        target_label: kubernetes_name
      - action: replace
        source_labels:
        - __meta_kubernetes_pod_node_name
        target_label: kubernetes_node