prometheus配置文件

454 阅读2分钟

prometheus的配置文件

安装完prometheus后,自带的配置文件,稍作下解析:
拿coredns的监控配置来说:

- job_name: serviceMonitor/kube-monitor/prometheus-stack-kube-prom-kube-proxy/0
  honor_timestamps: true
  scrape_interval: 30s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  authorization:
    type: Bearer
    credentials_file: /var/run/secrets/kubernetes.io/serviceaccount/token
  follow_redirects: true
  enable_http2: true
  relabel_configs:
  - source_labels: [job]
    separator: ;
    regex: (.*)
    target_label: __tmp_prometheus_job_name
    replacement: $1
    action: replace
  - source_labels: [__meta_kubernetes_service_label_app, __meta_kubernetes_service_labelpresent_app]
    separator: ;
    regex: (kube-prometheus-stack-kube-proxy);true
    replacement: $1
    action: keep
  - source_labels: [__meta_kubernetes_service_label_release, __meta_kubernetes_service_labelpresent_release]
    separator: ;
    regex: (prometheus-stack);true
    replacement: $1
    action: keep
  - source_labels: [__meta_kubernetes_endpoint_port_name]
    separator: ;
    regex: http-metrics
    replacement: $1
    action: keep
.....
  - source_labels: [__address__]
    separator: ;
    regex: (.*)
    modulus: 1
    target_label: __tmp_hash
    replacement: $1
    action: hashmod
  - source_labels: [__tmp_hash]
    separator: ;
    regex: "0"
    replacement: $1
    action: keep
  kubernetes_sd_configs:
  - role: endpoints
    kubeconfig_file: ""
    follow_redirects: true
    enable_http2: true
    namespaces:
      own_namespace: false
      names:
      - kube-system

该配置,使用的是kubernetes的服务发现,role是endpoints
可以参见:docs.victoriametrics.com/sd_configs.…

  • role: endpoints

    The role: endpoints discovers targets from listed endpoints of a service.

    Each discovered target has an __address__ label set to <addr>:<port>, where <addr> is the endpoint address, while <port> is the endpoint port. If the endpoint has multiple ports, then a single target per each port is generated. If the endpoint is backed by a pod, all additional container ports of the pod, not bound to an endpoint port, are discovered as targets as well.

    Available meta labels for role: endpoints during relabeling:

    • __meta_kubernetes_namespace: The namespace of the endpoints object.
    • __meta_kubernetes_endpoints_name: The names of the endpoints object.
    • __meta_kubernetes_endpoints_label_<labelname>: Each label from the endpoints object.
    • __meta_kubernetes_endpoints_labelpresent_<labelname>: "true" for each label from the endpoints object.

    For all targets discovered directly from the endpoints list (those not additionally inferred from underlying pods), the following labels are attached:

    • __meta_kubernetes_endpoint_hostname: Hostname of the endpoint.
    • __meta_kubernetes_endpoint_node_name: Name of the node hosting the endpoint.
    • __meta_kubernetes_endpoint_ready: Set to true or false for the endpoint's ready state.
    • __meta_kubernetes_endpoint_port_name: Name of the endpoint port.
    • __meta_kubernetes_endpoint_port_protocol: Protocol of the endpoint port.
    • __meta_kubernetes_endpoint_address_target_kind: Kind of the endpoint address target.
    • __meta_kubernetes_endpoint_address_target_name: Name of the endpoint address target.

    If the endpoints belong to a service, all labels of the role: service are attached. For all targets backed by a pod, all labels of the role: pod are attached.

    其中,keep标签是用来使用proxy服务的endpoint的label进行过滤的,可以看到与app和release等label对应的,需要keep的值

[root@xy-5-server14 ~]# kubectl -n kube-system describe ep prometheus-stack-kube-prom-kube-proxy
Name:         prometheus-stack-kube-prom-kube-proxy
Namespace:    kube-system
Labels:       app=kube-prometheus-stack-kube-proxy
              app.kubernetes.io/instance=prometheus-stack
              app.kubernetes.io/managed-by=Helm
              app.kubernetes.io/part-of=kube-prometheus-stack
              app.kubernetes.io/version=45.7.1
              chart=kube-prometheus-stack-45.7.1
              heritage=Helm
              jobLabel=kube-proxy
              release=prometheus-stack
              service.kubernetes.io/headless=
Annotations:  endpoints.kubernetes.io/last-change-trigger-time: 2023-03-23T09:43:05Z
Subsets:
  Addresses:          192.168.5.14,192.168.5.17,192.168.5.19
  NotReadyAddresses:  <none>
  Ports:
    Name          Port   Protocol
    ----          ----   --------
    http-metrics  10249  TCP

Events:  <none>

从上面的endpoint的描述可以看到:

addr是三个endpoint的addresses,即三个ip地址:192.168.5.14,192.168.5.17,192.168.5.19 下面是过滤掉非http-metrics的端口

- source_labels: [__meta_kubernetes_endpoint_port_name]
 separator: ;
 regex: http-metrics
 replacement: $1
 action: keep

过滤后,只剩下名为【http-metrics】的服务予以保留

relabel: www.modb.pro/db/116336