理解和使用多目標匯出器模式
本指南將向您介紹多目標匯出器模式。為此,我們將:
- 描述多目標匯出器模式及其用途,
- 執行 blackbox 匯出器作為該模式的示例,
- 為 blackbox 匯出器配置自定義查詢模組,
- 讓 blackbox 匯出器對 Prometheus 網站執行基本的指標查詢,
- 檢查一種流行的 Prometheus 配置模式,即使用重新標籤化來抓取匯出器。
多目標匯出器模式?
我們所說的多目標 匯出器 模式是指一種特定的設計,其中:
- 匯出器將透過網路協議獲取目標的指標。
- 匯出器不必執行在獲取指標的機器上。
- 匯出器將目標和查詢配置字串作為 Prometheus GET 請求的引數。
- 匯出器在收到 Prometheus 的 GET 請求後開始抓取,並在抓取完成後返回結果。
- 匯出器可以查詢多個目標。
此模式僅用於某些匯出器,例如 blackbox 和 SNMP 匯出器 。
原因是,我們要麼無法在目標上執行匯出器(例如,使用 SNMP 的網路裝置),要麼我們明確關注距離(例如,從網路外部的特定點到網站的延遲和可達性),這是 blackbox 匯出器的一個常見用例。
執行多目標匯出器
多目標匯出器在環境方面非常靈活,可以透過多種方式執行:作為常規程式、在容器中、作為後臺服務、在裸機上或虛擬機器上。由於它們透過網路進行查詢和被查詢,因此需要適當的開放埠。除此之外,它們對資源要求不高。
現在,親自動手嘗試一下吧!
在終端中執行以下命令,使用 Docker 啟動一個 blackbox 匯出器容器。根據您的系統配置,您可能需要在命令前加上 sudo
docker run -p 9115:9115 prom/blackbox-exporter
您應該會看到幾行日誌,如果一切順利,最後一行應該顯示 msg="Listening on address",如下所示
level=info ts=2018-10-17T15:41:35.4997596Z caller=main.go:324 msg="Listening on address" address=:9115
多目標匯出器的基本查詢
有兩種查詢方式:
- 查詢匯出器自身。它有自己的指標,通常可在
/metrics獲取。 - 查詢匯出器以抓取另一個目標。通常在“描述性”端點可用,例如
/probe。這很可能是您在使用多目標匯出器時主要感興趣的。
您可以在另一個終端中使用 curl 手動嘗試第一種查詢型別,或者使用此 連結
curl 'localhost:9115/metrics'
響應應類似於此
# HELP blackbox_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which blackbox_exporter was built.
# TYPE blackbox_exporter_build_info gauge
blackbox_exporter_build_info{branch="HEAD",goversion="go1.10",revision="4a22506cf0cf139d9b2f9cde099f0012d9fcabde",version="0.12.0"} 1
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 9
[…]
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.05
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1.048576e+06
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 7
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 7.8848e+06
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.54115492874e+09
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 1.5609856e+07
這些是 Prometheus 格式的指標。它們來自匯出器自身的儀表化,告訴我們匯出器在執行時的狀態。這稱為白盒監控,在日常運維實踐中非常有用。如果您好奇,請檢視我們的指南,瞭解如何對自己的應用程式進行儀表化。
對於第二種查詢型別,我們需要在 HTTP GET 請求中提供目標和模組作為引數。目標可以是 URI 或 IP,模組必須在匯出器的配置中定義。blackbox 匯出器容器帶有一個有意義的預設配置。我們將使用目標 prometheus.io 和預定義的模組 http_2xx。它告訴匯出器像瀏覽器訪問 prometheus.io 一樣發出 GET 請求,並期望收到 200 OK 響應。
您現在可以在終端中使用 curl 命令讓 blackbox 匯出器查詢 prometheus.io
curl 'localhost:9115/probe?target=prometheus.io&module=http_2xx'
這將返回大量指標
# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds
# TYPE probe_dns_lookup_time_seconds gauge
probe_dns_lookup_time_seconds 0.061087943
# HELP probe_duration_seconds Returns how long the probe took to complete in seconds
# TYPE probe_duration_seconds gauge
probe_duration_seconds 0.065580871
# HELP probe_failed_due_to_regex Indicates if probe failed due to regex
# TYPE probe_failed_due_to_regex gauge
probe_failed_due_to_regex 0
# HELP probe_http_content_length Length of http content response
# TYPE probe_http_content_length gauge
probe_http_content_length 0
# HELP probe_http_duration_seconds Duration of http request by phase, summed over all redirects
# TYPE probe_http_duration_seconds gauge
probe_http_duration_seconds{phase="connect"} 0
probe_http_duration_seconds{phase="processing"} 0
probe_http_duration_seconds{phase="resolve"} 0.061087943
probe_http_duration_seconds{phase="tls"} 0
probe_http_duration_seconds{phase="transfer"} 0
# HELP probe_http_redirects The number of redirects
# TYPE probe_http_redirects gauge
probe_http_redirects 0
# HELP probe_http_ssl Indicates if SSL was used for the final redirect
# TYPE probe_http_ssl gauge
probe_http_ssl 0
# HELP probe_http_status_code Response HTTP status code
# TYPE probe_http_status_code gauge
probe_http_status_code 0
# HELP probe_http_version Returns the version of HTTP of the probe response
# TYPE probe_http_version gauge
probe_http_version 0
# HELP probe_ip_protocol Specifies whether probe ip protocol is IP4 or IP6
# TYPE probe_ip_protocol gauge
probe_ip_protocol 6
# HELP probe_success Displays whether or not the probe was a success
# TYPE probe_success gauge
probe_success 0
請注意,幾乎所有指標的值都為 0。最後一個顯示 probe_success 0。這意味著探測器無法成功訪問 prometheus.io。原因隱藏在 probe_ip_protocol 指標中,其值為 6。預設情況下,探測器使用 IPv6 ,除非另有說明。但 Docker 守護程序預設會阻止 IPv6。因此,我們執行在 Docker 容器中的 blackbox 匯出器無法透過 IPv6 連線。
我們現在可以告訴 Docker 允許 IPv6,或者告訴 blackbox 匯出器使用 IPv4。在實際世界中,這兩種做法都可能合理,並且像往常一樣,問題的答案“該怎麼做?”是“視情況而定”。由於這是一份匯出器指南,我們將修改匯出器並藉此機會配置一個自定義模組。
配置模組
模組預定義在 docker 容器內一個名為 config.yml 的檔案中,該檔案是 github 倉庫中 blackbox.yml 的副本。
我們將複製此檔案,根據自己的需求進行調整 ,並告訴匯出器使用我們的配置檔案而不是容器中包含的那個。
首先使用 curl 或您的瀏覽器下載該檔案
curl -o blackbox.yml https://raw.githubusercontent.com/prometheus/blackbox_exporter/master/blackbox.yml
在編輯器中開啟它。前幾行如下所示:
modules:
http_2xx:
prober: http
http_post_2xx:
prober: http
http:
method: POST
YAML 使用空白縮排來表示層級結構,因此您可以看到定義了兩個名為 http_2xx 和 http_post_2xx 的 modules,並且它們都具有一個 http 探測器,其中一個的方法值被明確設定為 POST。您現在將透過將 http 探測器的 preferred_ip_protocol 明確設定為字串 ip4 來更改 http_2xx 模組。
modules:
http_2xx:
prober: http
http:
preferred_ip_protocol: "ip4"
http_post_2xx:
prober: http
http:
method: POST
如果您想了解更多關於可用探測器和選項的資訊,請查閱文件 。
現在我們需要告訴 blackbox 匯出器使用我們剛剛更改的檔案。您可以使用 --config.file="blackbox.yml" 標誌來做到這一點。但由於我們使用的是 Docker,我們首先必須使用 --mount 命令將此檔案在容器內部可用 。
注意如果您使用的是 macOS,您首先需要允許 Docker 守護程序訪問blackbox.yml所在的目錄。您可以透過點選選單欄中的小 Docker 鯨魚圖示,然後點選Preferences->File Sharing->+來完成此操作。之後,點選Apply & Restart。
首先,透過切換到舊容器的終端並按下 ctrl+c 來停止它。請確保您位於包含 blackbox.yml 的目錄中。然後執行此命令。它很長,但我們將解釋它。
docker \
run -p 9115:9115 \
--mount type=bind,source="$(pwd)"/blackbox.yml,target=/blackbox.yml,readonly \
prom/blackbox-exporter \
--config.file="/blackbox.yml"
透過此命令,您告訴 docker 執行以下操作:
run一個容器,將容器外部的9115埠對映到容器內部的9115埠。mount您當前目錄($(pwd)代表列印工作目錄)中的blackbox.yml檔案到容器內部的/blackbox.yml,以readonly模式。- 使用 Docker hub 中的
prom/blackbox-exporter映象。 - 執行 blackbox-exporter,並使用
--config.file標誌告訴它使用/blackbox.yml作為配置檔案。
如果一切正確,您應該會看到類似這樣的內容:
level=info ts=2018-10-19T12:40:51.650462756Z caller=main.go:213 msg="Starting blackbox_exporter" version="(version=0.12.0, branch=HEAD, revision=4a22506cf0cf139d9b2f9cde099f0012d9fcabde)"
level=info ts=2018-10-19T12:40:51.653357722Z caller=main.go:220 msg="Loaded config file"
level=info ts=2018-10-19T12:40:51.65349635Z caller=main.go:324 msg="Listening on address" address=:9115
現在您可以在終端中嘗試我們新的使用 IPv4 的 http_2xx 模組
curl 'localhost:9115/probe?target=prometheus.io&module=http_2xx'
這應該會返回如下所示的 Prometheus 指標:
# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds
# TYPE probe_dns_lookup_time_seconds gauge
probe_dns_lookup_time_seconds 0.02679421
# HELP probe_duration_seconds Returns how long the probe took to complete in seconds
# TYPE probe_duration_seconds gauge
probe_duration_seconds 0.461619124
# HELP probe_failed_due_to_regex Indicates if probe failed due to regex
# TYPE probe_failed_due_to_regex gauge
probe_failed_due_to_regex 0
# HELP probe_http_content_length Length of http content response
# TYPE probe_http_content_length gauge
probe_http_content_length -1
# HELP probe_http_duration_seconds Duration of http request by phase, summed over all redirects
# TYPE probe_http_duration_seconds gauge
probe_http_duration_seconds{phase="connect"} 0.062076202999999996
probe_http_duration_seconds{phase="processing"} 0.23481845699999998
probe_http_duration_seconds{phase="resolve"} 0.029594103
probe_http_duration_seconds{phase="tls"} 0.163420078
probe_http_duration_seconds{phase="transfer"} 0.002243199
# HELP probe_http_redirects The number of redirects
# TYPE probe_http_redirects gauge
probe_http_redirects 1
# HELP probe_http_ssl Indicates if SSL was used for the final redirect
# TYPE probe_http_ssl gauge
probe_http_ssl 1
# HELP probe_http_status_code Response HTTP status code
# TYPE probe_http_status_code gauge
probe_http_status_code 200
# HELP probe_http_uncompressed_body_length Length of uncompressed response body
# TYPE probe_http_uncompressed_body_length gauge
probe_http_uncompressed_body_length 14516
# HELP probe_http_version Returns the version of HTTP of the probe response
# TYPE probe_http_version gauge
probe_http_version 1.1
# HELP probe_ip_protocol Specifies whether probe ip protocol is IP4 or IP6
# TYPE probe_ip_protocol gauge
probe_ip_protocol 4
# HELP probe_ssl_earliest_cert_expiry Returns earliest SSL cert expiry in unixtime
# TYPE probe_ssl_earliest_cert_expiry gauge
probe_ssl_earliest_cert_expiry 1.581897599e+09
# HELP probe_success Displays whether or not the probe was a success
# TYPE probe_success gauge
probe_success 1
# HELP probe_tls_version_info Contains the TLS version used
# TYPE probe_tls_version_info gauge
probe_tls_version_info{version="TLS 1.3"} 1
您可以看到探測成功並獲得許多有用的指標,例如按階段劃分的延遲、狀態碼、SSL 狀態或 Unix 時間 格式的證書過期時間。blackbox 匯出器還在 localhost:9115 提供了一個小巧的 Web 介面,供您檢視最近的幾次探測、載入的配置和除錯資訊。它甚至提供了一個直接探測 prometheus.io 的連結。如果您想知道為什麼某些東西不起作用,這將非常方便。
使用 Prometheus 查詢多目標匯出器
到目前為止,一切順利。恭喜您!blackbox 匯出器已經正常工作,您可以手動指示它查詢遠端目標。您快成功了。現在您需要告訴 Prometheus 為我們執行這些查詢。
下面是一個最小的 Prometheus 配置。它告訴 Prometheus 抓取匯出器自身,就像我們之前使用 curl 'localhost:9115/metrics' 所做的那樣。
注意如果您使用 Docker for Mac 或 Docker for Windows,則不能在最後一行使用localhost:9115,而必須使用host.docker.internal:9115。這與在這些作業系統上實現 Docker 所使用的虛擬機器有關。您不應在生產環境中使用此配置。
適用於 Linux 的 prometheus.yml
global:
scrape_interval: 5s
scrape_configs:
- job_name: blackbox # To get metrics about the exporter itself
metrics_path: /metrics
static_configs:
- targets:
- localhost:9115
適用於 macOS 和 Windows 的 prometheus.yml
global:
scrape_interval: 5s
scrape_configs:
- job_name: blackbox # To get metrics about the exporter itself
metrics_path: /metrics
static_configs:
- targets:
- host.docker.internal:9115
現在執行一個 Prometheus 容器,並告訴它掛載我們上面提到的配置檔案。由於在容器中訪問主機網路的方式不同,您需要在 Linux 上使用與 macOS 和 Windows 略有不同的命令。
在 Linux 上執行 Prometheus(請勿在生產環境中使用 --network="host")
docker \
run --network="host"\
--mount type=bind,source="$(pwd)"/prometheus.yml,target=/prometheus.yml,readonly \
prom/prometheus \
--config.file="/prometheus.yml"
在 macOS 和 Windows 上執行 Prometheus
docker \
run -p 9090:9090 \
--mount type=bind,source="$(pwd)"/prometheus.yml,target=/prometheus.yml,readonly \
prom/prometheus \
--config.file="/prometheus.yml"
此命令的工作方式與使用配置檔案執行 blackbox 匯出器類似。
如果一切正常,您應該能夠訪問 localhost:9090/targets ,並在 blackbox 下看到一個狀態為綠色 UP 的端點。如果您看到紅色 DOWN,請確保您在上面啟動的 blackbox 匯出器仍在執行。如果您什麼也沒看到或看到黃色 UNKNOWN,說明您操作太快了,需要再等幾秒鐘,然後重新載入瀏覽器選項卡。
要告訴 Prometheus 查詢 "localhost:9115/probe?target=prometheus.io&module=http_2xx",您需要在 Prometheus 配置檔案 prometheus.yml 中新增另一個抓取任務 blackbox-http,其中將 metrics_path 設定為 /probe,並將引數設定在 params: 下。
global:
scrape_interval: 5s
scrape_configs:
- job_name: blackbox # To get metrics about the exporter itself
metrics_path: /metrics
static_configs:
- targets:
- localhost:9115 # For Windows and macOS replace with - host.docker.internal:9115
- job_name: blackbox-http # To get metrics about the exporter’s targets
metrics_path: /probe
params:
module: [http_2xx]
target: [prometheus.io]
static_configs:
- targets:
- localhost:9115 # For Windows and macOS replace with - host.docker.internal:9115
儲存配置檔案後,切換到您的 Prometheus Docker 容器終端,按下 ctrl+C 停止它,然後再次使用現有命令啟動它以重新載入配置。
終端應該返回訊息 "Server is ready to receive web requests.",幾秒鐘後,您應該開始在您的 Prometheus 中看到彩色的圖表。
這有效,但它有幾個缺點:
- 實際目標在引數配置中,這非常不尋常,以後很難理解。
instance標籤的值是 blackbox 匯出器的地址,這在技術上是正確的,但不是我們感興趣的。- 我們無法看到探測了哪個 URL。這不切實際,如果我們探測多個 URL,還會將不同的指標混淆在一起。
為了解決這個問題,我們將使用重新標籤化。重新標籤化在這裡很有用,因為在 Prometheus 內部,許多東西都是透過內部標籤配置的。細節很複雜,超出了本指南的範圍。因此,我們將僅限於必要的部分。但如果您想了解更多,請檢視這個演講 。目前,您只需理解以下內容即可:
- 所有以
__開頭的標籤在抓取後都會被丟棄。大多數內部標籤都以__開頭。 - 您可以設定名為
__param_<name>的內部標籤。這些標籤會為抓取請求設定鍵為<name>的 URL 引數。 - 有一個內部標籤
__address__,它由static_configs下的targets設定,其值是抓取請求的主機名。預設情況下,它稍後用於設定instance標籤的值,該標籤附在每個指標上,告訴您指標的來源。
這是您將用於此操作的配置。如果一下子資訊量有點大,請不要擔心,我們將逐步講解。
global:
scrape_interval: 5s
scrape_configs:
- job_name: blackbox # To get metrics about the exporter itself
metrics_path: /metrics
static_configs:
- targets:
- localhost:9115 # For Windows and macOS replace with - host.docker.internal:9115
- job_name: blackbox-http # To get metrics about the exporter’s targets
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- https://prometheus.golang.com.tw # Target to probe with http
- https://prometheus.golang.com.tw # Target to probe with https
- http://example.com:8080 # Target to probe with http on port 8080
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: localhost:9115 # The blackbox exporter’s real hostname:port. For Windows and macOS replace with - host.docker.internal:9115
那麼,與上次配置相比,有什麼新變化?
params 不再包含 target。相反,我們將實際目標新增到 static configs: targets 下。我們還使用了多個目標,因為現在我們可以這樣做。
params:
module: [http_2xx]
static_configs:
- targets:
- https://prometheus.golang.com.tw # Target to probe with http
- https://prometheus.golang.com.tw # Target to probe with https
- http://example.com:8080 # Target to probe with http on port 8080
relabel_configs 包含新的重新標籤化規則
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: localhost:9115 # The blackbox exporter’s real hostname:port. For Windows and macOS replace with - host.docker.internal:9115
在應用重新標籤化規則之前,Prometheus 發出的請求 URI 將如下所示:"https://prometheus.golang.com.tw/probe?module=http_2xx"。重新標籤化後,它將變成這樣:"https://:9115/probe?target=https://prometheus.golang.com.tw&module=http_2xx"。
現在讓我們探討每個規則是如何實現的。
首先,我們從標籤 __address__(其中包含 targets 的值)中取出值,並將它們寫入一個新標籤 __param_target,這將在 Prometheus 抓取請求中新增一個 target 引數。
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
在此之後,我們設想的 Prometheus 請求 URI 現在包含一個目標引數:"https://prometheus.golang.com.tw/probe?target=https://prometheus.golang.com.tw&module=http_2xx"。
然後,我們從標籤 __param_target 中取出值,並用這些值建立一個 instance 標籤。
relabel_configs:
- source_labels: [__param_target]
target_label: instance
我們的請求不會改變,但從請求返回的指標現在將帶有標籤 instance="https://prometheus.golang.com.tw"。
之後,我們將值 localhost:9115(我們匯出器的 URI)寫入標籤 __address__。這將用作 Prometheus 抓取請求的主機名和埠。這樣它就會查詢匯出器,而不是直接查詢目標 URI。
relabel_configs:
- target_label: __address__
replacement: localhost:9115 # The blackbox exporter’s real hostname:port. For Windows and macOS replace with - host.docker.internal:9115
我們的請求現在是 "localhost:9115/probe?target=https://prometheus.golang.com.tw&module=http_2xx"。透過這種方式,我們可以將實際目標放在那裡,將它們作為 instance 標籤值獲取,同時讓 Prometheus 對 blackbox 匯出器發出請求。
人們通常將這些與特定的服務發現結合使用。有關更多資訊,請檢視配置文件。使用它們沒有問題,因為它們像 static_configs 下定義的 targets 一樣寫入 __address__ 標籤。
就是這樣。重啟 Prometheus Docker 容器並檢視您的指標 。請注意選擇指標實際收集的時間段。
示例配置:一個 Prometheus 任務中的多個目標和多個模組
在上面的示例中,我們只使用了一個 blackbox 匯出器探測模組。module 被指定為 blackbox-http 任務的作業級引數。由於它是一個列表,它可以包含多個元素,但 blackbox 匯出器只使用第一個。然而,如果我們需要使用適當的模組探測不同的目標組,我們可以在一個任務中使用多個 blackbox 匯出器模組。此時,我們可以不在任務的 module 引數中指定,而是在每個 static_config 中指定一個 module 標籤。此外,relabel_configs 序列需要擴充套件一個新項,因為我們必須在此處將模組(作為 __param_module)注入到請求中。
global:
scrape_interval: 5s
scrape_configs:
- job_name: blackbox # To get metrics about the exporter itself
metrics_path: /metrics
static_configs:
- targets:
- localhost:9115 # The blackbox exporter’s real hostname:port
- job_name: blackbox-probes # To get metrics about the exporter’s targets with different modules
metrics_path: /probe
static_configs:
- targets: # static_config: targets and http_2xx module
- https://prometheus.golang.com.tw
- https://prometheus.golang.com.tw
- http://example.com:8080
labels:
module: http_2xx
- targets: # static_config: targets and icmp module
- prometheus.io
- example.com
labels:
module: icmp
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- source_labels: [module]
target_label: __param_module
- target_label: __address__
replacement: localhost:9115 # The blackbox exporter’s real hostname:port
摘要
在本指南中,您學習了多目標匯出器模式的工作原理,如何執行帶有自定義模組的 blackbox 匯出器,以及如何使用重新標籤化配置 Prometheus 來抓取帶有探測器標籤的指標。