Blackbox Exporter 作為 Prometheus 的重要元件,能有效監控外部服務的可用性與健康狀態。透過設定 HTTP、ICMP 和 DNS 探測,可以即時掌握服務的回應速度和狀態碼等關鍵指標。除了提取式指標收集,Pushgateway 提供了推播機制,適用於生命週期短或無法直接存取的應用程式。然而,Pushgateway 作為一種變通方案,需謹慎使用,避免成為系統瓶頸或單點故障。合理的組態和使用 Pushgateway,能有效擴充套件 Prometheus 的監控範圍,提升系統可靠性。

第10章:探測(Probing)

探測是監控系統中不可或缺的一環,尤其是在檢查服務和網站的可用性時。Prometheus 提供了一個名為 Blackbox Exporter 的工具,可以用來進行各種探測,如 HTTP、ICMP 和 DNS 檢查。

組態 Blackbox Exporter

Blackbox Exporter 是透過 YAML 組態檔案進行組態的,並透過命令列引數來控制。組態檔案中定義了各種檢查模組,每個模組對應一種特定的探測方式。例如,http_2xx_check 模組用於檢查 HTTP 服務是否傳回 2XX 狀態碼。

建立組態檔案

首先,建立一個名為 /etc/prober/prober.yml 的組態檔案,並新增以下內容:

modules:
  http_2xx_check:
    prober: http
    timeout: 5s
    http:
      valid_status_codes: []
      method: GET
  icmp_check:
    prober: icmp
    timeout: 5s
    icmp:
      preferred_ip_protocol: "ip4"
  dns_examplecom_check:
    prober: dns
    dns:
      preferred_ip_protocol: "ip4"
      query_name: "www.example.com"

內容解密:

  • modules:定義了多個檢查模組。
  • http_2xx_check:使用 http 探測器,檢查 HTTP 服務是否傳回 2XX 狀態碼。
    • timeout:設定超時時間為 5 秒。
    • valid_status_codes:預設接受任何 2XX 狀態碼。
  • icmp_check:使用 icmp 探測器,進行 ICMP 連通性檢查。
    • preferred_ip_protocol:指定使用 IPv4。
  • dns_examplecom_check:使用 dns 探測器,檢查 DNS 解析是否正常。
    • query_name:指定要查詢的網域名稱。

啟動 Blackbox Exporter

使用以下命令啟動 Blackbox Exporter,並指定剛才建立的組態檔案:

$ sudo blackbox_exporter --config.file="/etc/prober/prober.yml"

Exporter 預設執行在 9115 埠,可以透過瀏覽器存取 http://localhost:9115 檢視其控制檯頁面。

內容解密:

  • Blackbox Exporter 提供了一個控制檯頁面,可以檢視最近的檢查結果和除錯日誌,有助於排查問題。

建立 Prometheus Job

為了讓 Prometheus 能夠抓取 Blackbox Exporter 的資料,需要建立一個 Job。以下是 http_probe Job 的範例組態:

- job_name: 'http_probe'
  metrics_path: /probe
  params:
    module: [http_2xx_check]
  file_sd_configs:
    - files:
      - 'targets/probes/http_probes.json'
      refresh_interval: 5m
  relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target]
      target_label: instance
    - target_label: __address__
      replacement: prober.example.com:9115

內容解密:

  • job_name:定義了 Job 的名稱為 http_probe
  • metrics_path:指定了 metrics 的路徑為 /probe
  • params:傳遞引數給 Blackbox Exporter,指定使用 http_2xx_check 模組。
  • file_sd_configs:使用檔案服務發現機制,從 http_probes.json 檔案中讀取目標列表。
  • relabel_configs:進行標籤重寫,將目標地址傳遞給 Blackbox Exporter,並將 Exporter 的地址替換為實際執行的主機地址。

第10章:探測(Probing)與第11章:推播指標與Pushgateway

使用Blackbox Exporter進行資源探測

在前一章中,我們學習瞭如何使用Blackbox Exporter來探測資源。Blackbox Exporter是一種用於監控網路資源的工具,它可以對HTTP、TCP、DNS等進行探測,並將結果以Prometheus指標的形式呈現。

HTTP檢查的指標

在進行HTTP檢查時,Blackbox Exporter會產生一系列指標,如下所示:

probe_dns_lookup_time_seconds 0.404881857
probe_duration_seconds 0.626351441
probe_failed_due_to_regex 0
probe_http_content_length -1
probe_http_duration_seconds{phase="connect"} 0.013192816999999999
probe_http_duration_seconds{phase="processing"} 0.013948647000000002
probe_http_duration_seconds{phase="resolve"} 0.531245733
probe_http_duration_seconds{phase="tls"} 0.073685882
probe_http_duration_seconds{phase="transfer"} 0.000128069
probe_http_redirects 1
probe_http_ssl 1
probe_http_status_code 200
probe_http_version 1.1
probe_ip_protocol 4
probe_ssl_earliest_cert_expiry 1.527696449e+09
probe_success 1

其中,probe_http_status_code 是最重要的指標,它顯示了HTTP請求傳回的狀態碼。如果狀態碼是2xx,則探測被視為成功,probe_success指標將被設為1。這些指標還提供了其他有用的資訊,如探測的時間和HTTP版本。

Prometheus任務組態

為了使用Blackbox Exporter進行探測,我們需要在Prometheus中組態相應的任務。這些任務使用相同的重新標記規則來找到正確的目標和Exporter的地址。

ICMP和DNS檢查

除了HTTP檢查之外,Blackbox Exporter還可以進行ICMP和DNS檢查。ICMP檢查會對主機名或IP地址進行ping操作,而DNS檢查則會測試DNS伺服器的解析能力。

使用Pushgateway推播指標

在某些情況下,我們無法直接從目標資源中抓取指標,例如:

  • 由於安全或連線性問題,無法存取目標資源。
  • 目標資源的生命週期太短,例如容器啟動、執行和停止。
  • 目標資源沒有可被抓取的端點,例如批次作業。

在這些情況下,我們需要使用Pushgateway來將指標推播到Prometheus伺服器。

Pushgateway的工作原理

Pushgateway是一個獨立的服務,它透過HTTP REST API接收Prometheus指標。Pushgateway位於應用程式和Prometheus伺服器之間,接收應用程式推播的指標,然後被Prometheus伺服器抓取。

何時不使用Pushgateway

Pushgateway本質上是一種變通方案,用於監控無法被Prometheus伺服器直接抓取的資源。它並不是一個完美的解決方案,應該只在有限的情況下使用。我們應該避免將Pushgateway變成單點故障或效能瓶頸。

安裝Pushgateway

Pushgateway可以從Prometheus.io下載頁面下載。 目前,Pushgateway支援多個平台,包括Linux、Mac OS X、FreeBSD、OpenBSD、NetBSD、Microsoft Windows和DragonFly。

在Linux上安裝Pushgateway

要在64位Linux主機上安裝Pushgateway,我們可以下載zip壓縮檔。使用wgetcurl從下載網站取得檔案。

$ cd /tmp
$ wget https://github.com/prometheus/pushgateway/releases/download/v0.5.1/pushgateway-0.5.1.linux-amd64.tar.gz

然後,我們可以解壓縮Pushgateway二進位制檔案,並將其移動到合適的位置。

第11章:推播指標與 Pushgateway

安裝 Pushgateway

要安裝 Pushgateway,首先需要根據不同的作業系統下載對應的二進位制檔案。以下將介紹如何在 Linux、Mac OS X 和 Microsoft Windows 上安裝 Pushgateway。

在 Linux 上安裝 Pushgateway

首先,下載 Pushgateway 的 Linux 版本二進位制檔案。下載完成後,解壓縮檔案並將二進位制檔案複製到 /usr/local/bin/ 目錄下。

$ tar -xzf pushgateway-0.5.1.linux-amd64.tar.gz
$ sudo cp pushgateway-0.5.1.linux-amd64/pushgateway /usr/local/bin/

接下來,可以透過檢查版本來確認 Pushgateway 是否安裝成功。

$ pushgateway --version
pushgateway, version 0.5.1 (branch: HEAD, revision: 30dd0426c08b6479d9a26259ea5efd63bc1ee273)
build user: root@3e103e3fc918
build date: 20171116-17:45:26
go version: go1.9.2

在 Mac OS X 上安裝 Pushgateway

在 Mac OS X 上安裝 Pushgateway 的步驟與 Linux 類別似,只需要下載對應的 Darwin 版本二進位制檔案即可。

在 Microsoft Windows 上安裝 Pushgateway

在 Windows 上安裝 Pushgateway 需要下載 pushgateway.exe 可執行檔案。首先,使用 Powershell 建立一個目錄來存放可執行檔案。

C:\> MKDIR pushgateway
C:\> CD pushgateway

然後,從 GitHub 下載 pushgateway.exe 並解壓縮到 C:\pushgateway 目錄下。最後,將 C:\pushgateway 目錄新增到系統路徑中。

$env:Path += ";C:\pushgateway"

完成後,可以透過檢查版本來確認 Pushgateway 是否安裝成功。

C:\> pushgateway.exe --version
pushgateway, version 0.5.1 (branch: HEAD, revision: 30dd0426c08b6479d9a26259ea5efd63bc1ee273)
build user: root@3e103e3fc918
build date: 20171116-17:45:26
go version: go1.9.2

透過組態管理工具安裝 Pushgateway

除了手動安裝外,也可以使用組態管理工具(如 Puppet 或 Chef)來安裝 Pushgateway。

組態和執行 Pushgateway

Pushgateway 無需額外組態即可執行,但可以透過命令列引數進行自定義。例如,可以使用 --web.listen-address 引數來指定監聽的地址和埠。

$ pushgateway --web.listen-address="0.0.0.0:9091"

預設情況下,Pushgateway 將指標儲存在記憶體中。如果需要持久化儲存,可以使用 --persistence.file 引數指定儲存檔案。

$ pushgateway --persistence.file="/tmp/pushgateway_persist"

向 Pushgateway 傳送指標

一旦 Pushgateway 執行起來,就可以開始向它傳送指標。大多數 Prometheus 客戶端函式庫都支援向 Pushgateway 傳送指標。最簡單的方式是使用 curl 命令列工具來推播指標。

$ echo 'batchjob1_user_counter 2' | curl --data-binary @- http://localhost:9091/metrics/job/batchjob1

推播指標的 URL 路徑是根據標籤構建的,例如 /metrics/job/<jobname>。也可以在 URL 路徑中新增額外的標籤,例如 /metrics/job/<jobname>/instance/<instance>

$ echo 'batchjob1_user_counter 2' | curl --data-binary @- http://localhost:9091/metrics/job/batchjob1/instance/sidekiq_server

內容解密:

此處使用 curl 命令向 Pushgateway 發送了一個名為 batchjob1_user_counter 的指標,值為 2。URL 路徑 /metrics/job/batchjob1/instance/sidekiq_server 指定了兩個標籤:job=batchjob1instance=sidekiq_server。這將建立一個新的指標分組,包含該指標。

指標分組與管理

Pushgateway 中的指標是以分組的形式管理的。每個分組可以包含多個指標,並且可以對分組進行新增、刪除和查詢等操作。當 Pushgateway 重啟時,所有儲存在記憶體中的指標都會丟失,除非使用了持久化儲存。

內容解密:

Pushgateway 的設計是作為一個快取,而不是一個聚合器。因此,它不會對指標進行聚合計算,而是簡單地儲存和提供指標資料。這意味著,當 Pushgateway 重啟時,所有儲存在記憶體中的指標都會丟失。為了避免資料丟失,應該使用持久化儲存。