開始使用
本指南是一個“Hello World”風格的教程,介紹瞭如何安裝、配置和使用一個簡單的 Prometheus 例項。您將在本地下載並執行 Prometheus,配置它以抓取其自身和一個示例應用程式,然後使用查詢、規則和圖表來使用收集到的時間序列資料。
下載並執行 Prometheus
為您的平臺下載最新版本的 Prometheus,然後解壓並執行它
tar xvfz prometheus-*.tar.gz
cd prometheus-*
在啟動 Prometheus 之前,我們先來配置它。
配置 Prometheus 以監控自身
Prometheus 透過抓取指標 HTTP 端點從目標收集指標。由於 Prometheus 也以同樣的方式暴露關於自身的資料,因此它也可以抓取並監控自身的健康狀況。
雖然僅收集自身資料的 Prometheus 伺服器用處不大,但它是一個很好的入門示例。將以下基本 Prometheus 配置儲存為名為 prometheus.yml 的檔案
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
有關配置選項的完整規格說明,請參閱配置文件。
啟動 Prometheus
要使用新建立的配置檔案啟動 Prometheus,請切換到包含 Prometheus 二進位制檔案的目錄並執行
# Start Prometheus.
# By default, Prometheus stores its database in ./data (flag --storage.tsdb.path).
./prometheus --config.file=prometheus.yml
Prometheus 應當會啟動。您還應該能夠透過瀏覽器在 localhost:9090 訪問其自身的狀態頁面。給它幾秒鐘時間,以便從其自身的 HTTP 指標端點收集關於自身的資料。
您還可以透過訪問其指標端點來驗證 Prometheus 是否正在提供關於其自身的指標:localhost:9090/metrics
使用表示式瀏覽器
讓我們探索一下 Prometheus 收集到的關於其自身的資料。要使用 Prometheus 內建的表示式瀏覽器,請訪問 https://:9090/query 並選擇“Graph”(圖表)標籤頁。
正如您從 localhost:9090/metrics 中可以瞭解到的,Prometheus 匯出的關於自身的一個指標名為 prometheus_target_interval_length_seconds(目標抓取之間的實際時間間隔)。在表示式控制檯中輸入以下內容,然後點選“Execute”(執行)
prometheus_target_interval_length_seconds
這應該會返回許多不同的時間序列(以及為每個時間序列記錄的最新值),每個時間序列的指標名稱都是 prometheus_target_interval_length_seconds,但帶有不同的標籤。這些標籤指定了不同的延遲百分位數和目標組間隔。
如果我們只對第 99 百分位數的延遲感興趣,我們可以使用此查詢
prometheus_target_interval_length_seconds{quantile="0.99"}
要計算返回的時間序列數量,您可以編寫
count(prometheus_target_interval_length_seconds)
有關表示式語言的更多資訊,請參閱表示式語言文件。
使用圖表介面
要繪製表示式的圖表,請訪問 https://:9090/query 並使用“Graph”(圖表)標籤頁。
例如,輸入以下表達式以繪製在自抓取的 Prometheus 中每秒建立塊(chunk)的速率圖
rate(prometheus_tsdb_head_chunks_created_total[1m])
嘗試調整圖表範圍引數和其他設定。
啟動一些示例目標
讓我們為 Prometheus 新增額外的抓取目標。
Node Exporter 被用作示例目標,有關使用它的更多資訊,請參閱這些說明。
tar -xzvf node_exporter-*.*.tar.gz
cd node_exporter-*.*
# Start 3 example targets in separate terminals:
./node_exporter --web.listen-address 127.0.0.1:8080
./node_exporter --web.listen-address 127.0.0.1:8081
./node_exporter --web.listen-address 127.0.0.1:8082
現在您應該有了監聽在 https://:8080/metrics 、https://:8081/metrics 和 https://:8082/metrics 的示例目標。
配置 Prometheus 以監控示例目標
現在我們將配置 Prometheus 來抓取這些新目標。讓我們將所有三個端點分到一個名為 node 的作業(job)中。我們可以假設前兩個端點是生產環境目標,而第三個端點代表金絲雀(canary)例項。要在 Prometheus 中對此進行建模,我們可以將多組端點新增到單個作業中,併為每組目標新增額外的標籤。在此示例中,我們將 group="production" 標籤新增到第一組目標,將 group="canary" 新增到第二組目標。
為此,請將以下作業定義新增到 prometheus.yml 的 scrape_configs 部分中,並重啟您的 Prometheus 例項
scrape_configs:
- job_name: 'node'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080', 'localhost:8081']
labels:
group: 'production'
- targets: ['localhost:8082']
labels:
group: 'canary'
進入表示式瀏覽器,驗證 Prometheus 現在是否已包含這些示例端點暴露的時間序列資訊,例如 node_cpu_seconds_total。
配置將抓取的資料聚合為新時間序列的規則
儘管在我們的示例中這不是問題,但在需要臨時計算時,對數千個時間序列進行聚合的查詢可能會變慢。為了提高效率,Prometheus 可以透過配置的記錄規則(recording rules)將表示式預先記錄到新的持久時間序列中。假設我們有興趣記錄在 5 分鐘視窗內,每個例項的所有 CPU 平均每秒 CPU 時間率(node_cpu_seconds_total)(但保留 job、instance 和 mode 維度)。我們可以將其編寫為
avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))
嘗試繪製該表示式的圖表。
要將此表示式產生的時間序列記錄到一個名為 job_instance_mode:node_cpu_seconds:avg_rate5m 的新指標中,請建立一個包含以下記錄規則的檔案,並將其儲存為 prometheus.rules.yml
groups:
- name: cpu-node
rules:
- record: job_instance_mode:node_cpu_seconds:avg_rate5m
expr: avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))
為了讓 Prometheus 識別這個新規則,請在您的 prometheus.yml 中新增一個 rule_files 宣告。配置現在應該如下所示
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # Evaluate rules every 15 seconds.
# Attach these extra labels to all timeseries collected by this Prometheus instance.
external_labels:
monitor: 'codelab-monitor'
rule_files:
- 'prometheus.rules.yml'
scrape_configs:
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080', 'localhost:8081']
labels:
group: 'production'
- targets: ['localhost:8082']
labels:
group: 'canary'
使用新配置重啟 Prometheus,並透過表示式瀏覽器查詢或繪製圖表,驗證現在是否可以使用名為 job_instance_mode:node_cpu_seconds:avg_rate5m 的新時間序列。
重新載入配置
正如在配置文件中所提到的,Prometheus 例項可以透過使用 SIGHUP 訊號在不重啟程序的情況下重新載入其配置。如果您在 Linux 上執行,可以透過使用 kill -s SIGHUP <PID> 來執行此操作,其中將 <PID> 替換為您的 Prometheus 程序 ID。
優雅地關閉您的例項。
雖然 Prometheus 在程序意外失敗時確實有恢復機制,但建議使用訊號或中斷來乾淨地關閉 Prometheus 例項。在 Linux 上,這可以透過向 Prometheus 程序傳送 SIGTERM 或 SIGINT 訊號來實現。例如,您可以使用 kill -s <SIGNAL> <PID>,將 <SIGNAL> 替換為訊號名稱,將 <PID> 替換為 Prometheus 程序 ID。或者,您可以在控制終端中按下中斷字元,預設情況下是 ^C (Control-C)。