1. 程式人生 > 實用技巧 >Prometheus聯邦

Prometheus聯邦

聯邦使得一個 Prometheus 伺服器可以從另一個 Prometheus 伺服器提取選定的時序。

1. 使用場景

Prometheus 聯邦有不同的使用場景。通常,聯邦被用來實現可擴充套件的 Prometheus 監控設定,或者將相關的指標從一個服務的 Prometheus 拉取到另一個 Prometheus 中。

1.1. 分層聯邦

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

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

1.2. 跨服務聯邦

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

例如,一個執行多種服務的叢集排程器可以暴露在叢集上執行的服務例項的資源使用資訊(例如記憶體和 CPU 使用率)。另一方面,執行在叢集上的服務只需要暴露指定應用程式級別的服務指標。通常,這兩種指標集分別被不同的 Prometheus 伺服器抓取。利用聯邦,監控服務級別指標的 Prometheus 伺服器也可以從叢集中 Prometheus 伺服器拉取其特定服務的叢集資源使用率指標,以便可以在該 Prometheus 伺服器中使用這兩組指標集

2. 配置聯邦

在 Prometheus 伺服器中,/federate 節點允許獲取服務中被選中的時間序列集合的值。至少一個 match[] URL 引數必須被指定為要暴露的序列。每個 match[] 變數需要被指定為一個不變的維度選擇器up 或者 {job="api-server"}。如果有多個 match[] 引數,則所有符合的時序資料的集合都會被選擇。

從一個 Prometheus 伺服器聯邦指標到另一個 Prometheus 伺服器,配置你的目標 Prometheus 伺服器從源伺服器的 /federate 節點抓取指標資料,同時也使用 honor_lables 抓取選項(不重寫源 Prometheus 服務暴露的標籤)並且傳遞需要的 match[]

引數。例如,下面的 scrape_configs 聯邦 source-prometheus-{1,2,3}:9090 三臺 Prometheus 伺服器,上層 Prometheus 抓取並彙總他們暴露的任何帶 job="prometheus" 標籤的序列或名稱以 job: 開頭的指標。

scrape_configs:
  - job_name: 'federate'
    scrape_interval: 15s

    honor_labels: true
    metrics_path: '/federate'

    params:
      'match[]':
        - '{job="prometheus"}'
        - '{__name__=~"job:.*"}'
        - '{job="node"}'

    static_configs:
      - targets:
        - 'source-prometheus-1:9090'
        - 'source-prometheus-2:9090'
        - 'source-prometheus-3:9090'

Read more: https://fuckcloudnative.io/prometheus/3-prometheus/federation.html#ixzz6V68Jlq2m