prometheus2.0 聯邦的配置

  • 2019 年 10 月 4 日
  • 筆記

prometheus2.0 在參數上有原先的1.X有較大的變化,很多參數被去掉了。另外優化了CPU佔用、存儲空間佔用等,具體可以看官方文檔。

這裡貼一下prometheus2.0的聯邦(又稱Shard)搭建方法,一些理論知識就不貼了,自己去google扒吧:

官網對於聯邦的介紹:https://prometheus.io/docs/prometheus/latest/federation/

聯邦有不同的用例。它通常用於實現可擴展的prometheus,或者將metrics從一個服務的prometheus拉到另一個Prometheus上用於展示。

分層聯邦:

分層聯邦允許普羅米修斯擴展到數十個數據中心和數百萬個節點的環境。在這種用例中,聯邦拓撲類似於一棵樹,更高級別的普羅米修斯伺服器從大量的從屬伺服器收集匯總的時間序列數據。

跨服務聯邦:

In cross-service federation, a Prometheus server of one service is configured to scrape selected data from another service's Prometheus server to enable alerting and queries against both datasets within a single server.

在跨服務聯合中,一個服務的普羅米修斯伺服器被配置為從另一個服務的普羅米修斯伺服器中刮取選定的數據,以使得能夠針對單個伺服器內的兩個數據集進行警報和查詢。

For example, a cluster scheduler running multiple services might expose resource usage information (like memory and CPU usage) about service instances running on the cluster. On the other hand, a service running on that cluster will only expose application-specific service metrics. Often, these two sets of metrics are scraped by separate Prometheus servers. Using federation, the Prometheus server containing service-level metrics may pull in the cluster resource usage metrics about its specific service from the cluster Prometheus, so that both sets of metrics can be used within that server.

例如,運行多個服務的集群調度程式可能會暴露有關在集群上運行的服務實例的資源使用情況資訊(如記憶體和CPU使用情況)。另一方面,在該群集上運行的服務將僅公開特定於應用程式的服務度量標準。通常,這兩套指標是由單獨的普羅米修斯伺服器來抓取的。使用聯邦,包含服務級別度量的普羅米修斯伺服器可以從集群普羅米修斯拉入有關其特定服務的集群資源使用度量,以便這兩組度量可以在該伺服器內使用。

【舉個例子:我們要監控mysqld的運行狀態,可以使用1個主Prometheus+2個分片Prometheus(一個用來採集node_exporter的metrics、一個用來採集mysql_exporter的metrics),然後在主Prometheus上做匯總】

安裝prometheus和mysqld_exporter、postgres_exporter的步驟也不寫了,很簡單的,exporter的部署我們通常用ansible或saltstack之類的工具批量分發。

我這裡實驗起見,在一台機器上跑了3個shard節點,1個global節點。

Node1: 10.0.20.25 (跑了老版本的prometheus1.7、mysql_exporter、postgres_exporter,這是之前做實驗搭建的環境)

Node2: 10.0.20.26 (跑了prometheus2.0、mysql_exporter、postgres_exporter)

下面開始在Node2上開始我們的聯邦的配置吧。

注意: 我這裡做實驗的時候,跨服務聯邦的配置還不太規範,照官方的說法是 採用多個獨立的Prometheus節點分別採集node_exporter、mysql_exporter、postgres_exporter 這些metrics,然後再在GLOBAL節點做匯總。

cd /usr/local/prometheus

編寫存放要採集主機的文件:

cat mysqld.json 

[      {          "targets": [              "10.0.20.26:9104",              "10.0.20.26:9100",              "10.0.20.25:9104",              "10.0.20.25:9100"          ],          "labels": {              "services": "dba_test",          }      }  ]

cat pgsql.json 

[      {          "targets": [              "10.0.20.26:9187",              "10.0.20.25:9187"          ],          "labels": {              "services": "dba_test_pgsql",          }      }  ]

3個Shard節點配置文件如下:

節點1,搜集的是mysql的資訊

cat prometheus1.yml

global:    scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.    evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.    # scrape_timeout is set to the global default (10s).  alerting:    alertmanagers:    - static_configs:      - targets:        # - alertmanager:9093  rule_files:    # - "first_rules.yml"    # - "second_rules.yml"  scrape_configs:    - job_name: 'mysql'      file_sd_configs:        - files: ['./mysqld.json']

節點2,搜集的是pgsql的資訊

cat prometheus2.yml

global:    scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.    evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.    # scrape_timeout is set to the global default (10s).  alerting:    alertmanagers:    - static_configs:      - targets:        # - alertmanager:9093  rule_files:    # - "first_rules.yml"    # - "second_rules.yml"  scrape_configs:    - job_name: 'pgsql'      file_sd_configs:        - files: ['./pgsql.json']

節點3,搜集的是prometheus節點的資訊

cat prometheus3.yml

global:    scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.    evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.    # scrape_timeout is set to the global default (10s).  alerting:    alertmanagers:    - static_configs:      - targets:        # - alertmanager:9093  rule_files:    # - "first_rules.yml"    # - "second_rules.yml"  scrape_configs:    - job_name: 'prometheus25'      static_configs:        - targets: ['10.0.20.25:9090']    - job_name: 'prometheus26'      static_configs:        - targets: ['10.0.20.26:9090']

然後,啟動3個分片節點:

./prometheus --web.listen-address="0.0.0.0:9091" --storage.tsdb.path="data1/" --config.file="prometheus1.yml" --web.enable-admin-api    ./prometheus --web.listen-address="0.0.0.0:9092" --storage.tsdb.path="data2/" --config.file="prometheus2.yml" --web.enable-admin-api     ./prometheus --web.listen-address="0.0.0.0:9093" --storage.tsdb.path="data3/" --config.file="prometheus3.yml" --web.enable-admin-api

再來配置GLOBAL節點:

cat prometheus.yml 內容如下:

global:    scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.    evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.    # scrape_timeout is set to the global default (10s).  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.*"}'          - '{job="mysql"}'          - '{job="pgsql"}'      static_configs:        - targets:          - '10.0.20.26:9091'          - '10.0.20.26:9092'          - '10.0.20.26:9093'

啟動GLOBAL節點:

./prometheus --web.listen-address="0.0.0.0:9090" --storage.tsdb.path="data_global/" --config.file="prometheus.yml" --web.enable-admin-api

配好後,整個目錄 如下圖:

然後,在瀏覽器訪問http://10.0.20.26:9090/targets  這個GLOBAL節點,效果如下圖:

可以訪問原先的3個shard節點,獲取到對應的mysql、pgsql、prometheus的採集資訊:

http://10.0.20.26:9091/graph

http://10.0.20.26:9092/graph

http://10.0.20.26:9093/graph

我們可以自測下,在http://10.0.20.26:9090/graph 這裡可以採集到後端3個shard節點的全部數據的。

這樣,我們就配好了prometheus的聯邦啦。是不是很簡單??

注意: 有些時候 ,我們要求告警資訊實時性特別高,這種情況下,告警的採集(以granfana為例)不建議使用連接到聯邦GLOBAL節點的方式,建議直連後端對應的Shard節點,以免因為GLOBAL採集數據延遲,而導致發送告警的延遲情況出現。

結合grafana:

然後,可以在grafana裡面做展示了,直接上圖:

這裡填instance 的時候,我用了個通配符,一次性列出符合條件的全部主機,但是唯一的缺點是,這樣就不能用grafana自帶的告警啦,因為目前grafana的告警還不支援動態寫法,除非我們寫死了node_load5{instance=~'10.0.20.26:9100'} 這樣才行。

下面是我配好的部分截圖:

Tags: