圖解 prometheus 聯邦制


prometheus 的聯邦制分爲兩種,分層聯邦和跨服務聯邦。

1. 分層聯邦

分層聯邦允許 Prometheus 能夠擴展到十幾個數據中心和上百萬的節點。在此場景下,聯邦拓撲類似一個樹形拓撲結構,上層的 Prometheus 服務器從大量的下層 Prometheus 服務器中收集和匯聚的時序數據。

例如,一個聯邦設置可能由多個數據中心中的 Prometheus 服務器和一套全局 Prometheus 服務器組成。每個數據中心中部署的 Prometheus 服務器負責收集本區域內細粒度的數據(實例級別),全局 Prometheus 服務器從這些下層 Prometheus 服務器中收集和匯聚數據(任務級別),並存儲聚合後的數據。這樣就提供了一個聚合的全局視角和詳細的本地視角。
分層聯邦

2. 跨服務聯邦

在跨服務聯邦中,一個服務的 Prometheus 服務器被配置來提取來自其他服務的 Prometheus 服務器的指定的數據,以便在一個 Prometheus 服務器中對兩個數據集啓用告警和查詢。

例如scene1,監控服務級別指標的 Prometheus 服務器也可以從集羣中 Prometheus 服務器拉取其特定服務的集羣資源使用率指標,以便可以在該 Prometheus 服務器中使用這兩組指標集。
跨服務聯邦場景 1

另外,如 scene2,運行在集羣上的服務只需要暴露指定應用程序級別的服務指標,這些指標集分別被不同的 Prometheus 服務器抓取。
跨服務聯邦場景 2

2.1. 實例

source prometheus 上部署有 node_exporter 服務,prometheus 配置如下

global:
  scrape_interval: 15s
  external_labels:
    monitor: 'codelab-monitor'

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 15s
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    scrape_interval: 15s
    static_configs:
      - targets: ['localhost:9100']

service prometheus 從 source prometheus 上拿取 node_exporter 的數據

global:
  scrape_interval: 15s
  evaluation_interval: 15s

# alert 配置項,也可以在 source 端配置
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# 告警規則文件
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: 'federate'
    scrape_interval: 15s
    honor_labels: true  # 不覆蓋源服務器公開的任何標籤
    metrics_path: '/federate'
    params:
        'match[]':
            #- '{job="prometheus"}'  # 去除 source 端 prometheus 狀態的指標
            - '{job="node_exporter"}'
            #- '{__name__=~"job:.*"}'
    static_configs:
    - targets: 
        - 'slave_ip1:9090'  #  source 端 pronetheus 啓動地址,可以配置多個 source
        - 'slave_ip2:9090' 

2.1.1. 運行結果

分別啓動 source prometheus 和 service prometheus,而後進入 http://service_ip:9090 查看結果,發現服務正常且能查詢到 source 上的數據。
查詢結果
targets 狀態


參考文檔

  1. https://www.yangcs.net/prometheus/3-prometheus/federation.html
  2. https://prometheus.io/docs/prometheus/latest/federation/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章