在本指南中,我們將詳細(xì)介紹 Prometheus 架構(gòu)。
Prometheus 是一個(gè)用 Golang 編寫的開源監(jiān)控和告警系統(tǒng),能夠收集和處理來自各種目標(biāo)的指標(biāo)。您還可以查詢、查看、分析指標(biāo),并根據(jù)閾值收到警報(bào)。
此外,在當(dāng)今世界,可觀測性對(duì)每個(gè)組織來說都變得至關(guān)重要,而 Prometheus 是開源領(lǐng)域的關(guān)鍵可觀測性工具之一。
在這篇博客中,我們將了解 Prometheus 的所有關(guān)鍵組件,以及它們?nèi)绾螀f(xié)同工作以使整個(gè)監(jiān)控系統(tǒng)正常工作。
Prometheus 架構(gòu)
以下是 Prometheus 架構(gòu)的高級(jí)概述。
Prometheus 主要由以下部分組成。
- Prometheus 服務(wù)器
- 服務(wù)發(fā)現(xiàn)
- 時(shí)序數(shù)據(jù)庫(TSDB)
- 目標(biāo)
- 導(dǎo)出器
- 推送網(wǎng)關(guān)
- 警報(bào)管理器
- 客戶端庫
- PromQL系列
讓我們?cè)敿?xì)看一下每個(gè)組件。
Prometheus 服務(wù)器
Prometheus 服務(wù)器是基于指標(biāo)的監(jiān)控系統(tǒng)的大腦。服務(wù)器的主要工作是使用拉取模型從各種目標(biāo)收集指標(biāo)。
Target 只不過是服務(wù)器、pod、端點(diǎn)等,我們將在下一主題中詳細(xì)介紹。
使用 Prometheus 從目標(biāo)收集指標(biāo)的一般術(shù)語稱為抓?。╬ull)。
Prometheus 會(huì)根據(jù)?Prometheus 配置文件中提到的抓取間隔定期抓取指標(biāo)。
下面是一個(gè)示例配置。
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_timeout: 10s
rule_files:
- "rules/*.rules"
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter:9100']
alerting:
alertmanagers:
- static_configs:
- targets: ['alertmanager:9093']
時(shí)序數(shù)據(jù)庫(TSDB)
prometheus 接收的指標(biāo)數(shù)據(jù)會(huì)隨時(shí)間變化(CPU、內(nèi)存、網(wǎng)絡(luò) IO 等)。它稱為時(shí)間序列數(shù)據(jù)。因此,Prometheus 使用時(shí)間序列數(shù)據(jù)庫?(TSDB) 來存儲(chǔ)其所有數(shù)據(jù)。
默認(rèn)情況下,Prometheus 將其所有數(shù)據(jù)以有效的格式(塊)存儲(chǔ)在本地磁盤中。隨著時(shí)間的流逝,它會(huì)壓縮所有舊數(shù)據(jù)以節(jié)省空間。它還具有保留策略來刪除舊數(shù)據(jù)。
Prometheus 還提供遠(yuǎn)程存儲(chǔ)選項(xiàng)。這主要是存儲(chǔ)可擴(kuò)展性、長期存儲(chǔ)、備份和災(zāi)難恢復(fù)等所必需的。
Prometheus 目標(biāo)
Target 是 Prometheus 抓取指標(biāo)的來源。目標(biāo)可以是服務(wù)器、服務(wù)、Kubernetes Pod、應(yīng)用程序端點(diǎn)等。
默認(rèn)情況下,prometheus 在目標(biāo)路徑下查找指標(biāo)??梢栽谀繕?biāo)配置中更改默認(rèn)路徑。這意味著,如果您未指定自定義指標(biāo)路徑,Prometheus 會(huì)在?/metrics
?下查找指標(biāo)。/metrics
目標(biāo)配置位于配置文件的scrape_configs下。下面是一個(gè)示例配置。Prometheus
scrape_configs:
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter1:9100', 'node-exporter2:9100']
- job_name: 'my_custom_job'
static_configs:
- targets: ['my_service_address:port']
metrics_path: '/custom_metrics'
- job_name: 'blackbox-exporter'
static_configs:
- targets: ['blackbox-exporter1:9115', 'blackbox-exporter2:9115']
metrics_path: /probe
- job_name: 'snmp-exporter'
static_configs:
- targets: ['snmp-exporter1:9116', 'snmp-exporter2:9116']
metrics_path: /snmp
從目標(biāo)端點(diǎn),prometheus?需要特定文本格式的數(shù)據(jù)。每個(gè)指標(biāo)都必須位于新行上。
通常,這些指標(biāo)使用在目標(biāo)上運(yùn)行的?prometheus 導(dǎo)出器在目標(biāo)節(jié)點(diǎn)上公開。
Prometheus 導(dǎo)出器
導(dǎo)出器就像在目標(biāo)上運(yùn)行的代理。它將指標(biāo)從特定系統(tǒng)轉(zhuǎn)換為 prometheus 理解的格式。
它可以是 CPU、內(nèi)存等系統(tǒng)指標(biāo),也可以是 Java JMX 指標(biāo)、MySQL 指標(biāo)等。
默認(rèn)情況下,這些轉(zhuǎn)換后的指標(biāo)由導(dǎo)出器在目標(biāo)的?/metrics
?路徑(HTTPS 端點(diǎn))上公開。
例如,如果要監(jiān)視服務(wù)器的 CPU 和內(nèi)存,則需要在該服務(wù)器上安裝節(jié)點(diǎn)導(dǎo)出器,并且節(jié)點(diǎn)導(dǎo)出器會(huì)在?/metrics
?上以 prometheus 指標(biāo)格式公開?CPU 和內(nèi)存指標(biāo)。
一旦 Prometheus?提取了指標(biāo),它將組合指標(biāo)名稱、標(biāo)簽、值和時(shí)間戳,為該數(shù)據(jù)提供結(jié)構(gòu)。
有很多社區(qū)導(dǎo)出器可用,但只有其中一些得到了 Prometheus 的正式批準(zhǔn)。如果需要更多自定義項(xiàng),則需要?jiǎng)?chuàng)建自己的導(dǎo)出器。
Prometheus 將導(dǎo)出器分為各個(gè)部分,例如數(shù)據(jù)庫、硬件、問題跟蹤器和持續(xù)集成、消息傳遞系統(tǒng)、存儲(chǔ)、公開 Prometheus 指標(biāo)的軟件、其他第三方實(shí)用程序等。
您可以從官方文檔中查看每個(gè)類別的出口商列表。
在 Prometheus 配置文件中,所有導(dǎo)出器的詳細(xì)信息都將在 .scrape_configs
scrape_configs:
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter1:9100', 'node-exporter2:9100']
- job_name: 'blackbox-exporter'
static_configs:
- targets: ['blackbox-exporter1:9115', 'blackbox-exporter2:9115']
metrics_path: /probe
- job_name: 'snmp-exporter'
static_configs:
- targets: ['snmp-exporter1:9116', 'snmp-exporter2:9116']
metrics_path: /snmp
Prometheus 服務(wù)發(fā)現(xiàn)
Prometheus 使用兩種方法從目標(biāo)中抓取指標(biāo)。
- 靜態(tài)配置:當(dāng)目標(biāo)具有靜態(tài)?IP 或 DNS 端點(diǎn)時(shí),我們可以將這些端點(diǎn)用作目標(biāo)。
- 服務(wù)發(fā)現(xiàn):在大多數(shù)自動(dòng)縮放系統(tǒng)和分布式系統(tǒng)(如 Kubernetes)中,目標(biāo)不會(huì)有靜態(tài)終結(jié)點(diǎn)。在這種情況下,將使用 prometheus 服務(wù)發(fā)現(xiàn)來發(fā)現(xiàn)目標(biāo)端點(diǎn),并將目標(biāo)自動(dòng)添加到 prometheus 配置中。
在繼續(xù)之前,讓我展示一個(gè)使用?Prometheus 配置文件的?Kubernetes 服務(wù)發(fā)現(xiàn)塊的小示例。kubernetes_sd_configs
scrape_configs:
- job_name: 'kubernetes-apiservers'
kubernetes_sd_configs:
- role: endpoints
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
relabel_configs:
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
action: keep
regex: default;kubernetes;https
Kubernetes 是動(dòng)態(tài)目標(biāo)的完美示例。在這里,您不能使用靜態(tài)目標(biāo)方法,因?yàn)?Kubernetes 集群中的目標(biāo)(Pod)本質(zhì)上是短暫的,并且很可能是短暫的。
Kubernetes 中還有基于文件的服務(wù)發(fā)現(xiàn)。它適用于靜態(tài)目標(biāo),但經(jīng)典靜態(tài)配置與靜態(tài)配置之間的主要區(qū)別在于,在這種情況下,我們創(chuàng)建單獨(dú)的 JSON 或 YAML 文件并將目標(biāo)信息保存在其中file_sd_configs
。Prometheus 將讀取文件以識(shí)別目標(biāo)。static_configs?
file_sd_configs
不僅這兩個(gè),還有各種服務(wù)發(fā)現(xiàn)方法可用,例如 consul_sd_configs(prometheus 從?consul?獲取目標(biāo)詳細(xì)信息)、ec2_sd_configs等。
要了解有關(guān)配置詳細(xì)信息的更多信息,請(qǐng)?jiān)L問官方文檔。
Prometheus 推送網(wǎng)關(guān)
默認(rèn)情況下,Prometheus 使用拉取機(jī)制來獲取指標(biāo)。
但是,在某些情況下,需要將指標(biāo)推送到 prometheus。
讓我們舉一個(gè)在?Kubernetes cronjob?上運(yùn)行的批處理作業(yè)的例子,該作業(yè)每天根據(jù)某些事件運(yùn)行 5 分鐘。在這種情況下,Prometheus 將無法使用拉取機(jī)制正確抓取服務(wù)級(jí)別指標(biāo)。
因此,為了等待 prometheus 拉取指標(biāo),我們需要將指標(biāo)推送到 prometheus。為了推送指標(biāo),prometheus 提供了一個(gè)名為?Pushgateway 的解決方案。?它是一種中間網(wǎng)關(guān)。
Pushgateway?需要作為獨(dú)立組件運(yùn)行。批處理作業(yè)可以將指標(biāo)推送到 pushgateway 端點(diǎn),Pushgateway?會(huì)公開這些指標(biāo)。然后 prometheus 從 Pushgateway 中抓取這些指標(biāo)。
Pushgateway 將指標(biāo)數(shù)據(jù)臨時(shí)存儲(chǔ)在內(nèi)存存儲(chǔ)中。它更像是一個(gè)臨時(shí)緩存。
Pushgateway?配置也將在配置中的部分下進(jìn)行配置。scrape_configs?
Prometheus
scrape_configs:
- job_name: "pushgateway"
honor_labels: true
static_configs:
- targets: [pushgateway.monitoring.svc:9091]
要將指標(biāo)發(fā)送到 Pushgateway,您需要使用 prometheus?客戶端庫并檢測應(yīng)用程序或腳本以公開所需的指標(biāo)。
Prometheus 客戶端庫
Prometheus?客戶端庫是軟件庫,可用于檢測應(yīng)用程序代碼,以 Prometheus 理解的方式公開指標(biāo)。
如果需要自定義檢測或想要?jiǎng)?chuàng)建自己的導(dǎo)出器,可以使用客戶端庫。
一個(gè)非常好的用例是需要將指標(biāo)推送到 Pushgateway 的批處理作業(yè)。批處理作業(yè)需要使用客戶端庫進(jìn)行檢測,以 prometheus 格式公開需求指標(biāo)。
以下示例公開了名為?batch_job_records_processed_total
?的自定義指標(biāo)。Python Client Library
from prometheus_client import start_http_server, Counter
import time
import random
RECORDS_PROCESSED = Counter('batch_job_records_processed_total', 'Total number of records processed by the batch job')
def process_record():
time.sleep(random.uniform(0.01, 0.1))
RECORDS_PROCESSED.inc()
def batch_job():
for _ in range(100):
process_record()
if __name__ == '__main__':
start_http_server(8000)
print("Metrics server started on port 8000")
batch_job()
print("Batch job completed")
while True:
time.sleep(1)
此外,在使用客戶端庫時(shí),HTTP 服務(wù)器prometheus_client
端點(diǎn)中公開指標(biāo)。/metrics
Prometheus 幾乎為每種編程語言提供了客戶端庫,如果您想創(chuàng)建客戶端庫,也可以這樣做。
要了解有關(guān)創(chuàng)建指南的更多信息并查看客戶端庫列表,您可以參考官方文檔。
Prometheus 警報(bào)管理器
Alertmanager是Prometheus監(jiān)控系統(tǒng)的關(guān)鍵部分。它的主要工作是根據(jù)?Prometheus 警報(bào)配置中設(shè)置的指標(biāo)閾值發(fā)送警報(bào)。
警報(bào)由 Prometheus 觸發(fā)并發(fā)送到 Alertmanager。它反過來將警報(bào)發(fā)送到警報(bào)管理器配置中配置的相應(yīng)通知系統(tǒng)/接收器(電子郵件、松弛等)。
此外,警報(bào)管理器還負(fù)責(zé)以下工作。
- 警報(bào)重復(fù)數(shù)據(jù)刪除:靜默重復(fù)警報(bào)的過程。
- 分組:將相關(guān)警報(bào)分組到其他位置的過程。
- 靜音:靜音警報(bào),用于維護(hù)或誤報(bào)。
- 路由:根據(jù)嚴(yán)重性將警報(bào)路由到適當(dāng)?shù)慕邮掌鳌?/li>
- 禁止:當(dāng)存在中等高嚴(yán)重性警報(bào)時(shí)停止低嚴(yán)重性警報(bào)的過程。
下面是警報(bào)規(guī)則的示例配置。
groups:
- name: microservices_alerts
rules:
- record: http_latency:average_latency_seconds
expr: sum(http_request_duration_seconds_sum) / sum(http_request_duration_seconds_count)
- alert: HighLatencyAlert
expr: http_latency:average_latency_seconds > 0.5
for: 5m
labels:
severity: critical
annotations:
summary: "High latency detected in microservices"
description: "The average HTTP latency is high ({{ $value }} seconds) in the microservices cluster."
這是 Alertmanager 配置文件的路由配置示例
routes:
- match:
severity: 'critical'
receiver: 'pagerduty-notifications'
- match:
severity: 'warning'
receiver: 'slack-notifications'
警報(bào)管理器支持大多數(shù)消息和通知系統(tǒng),例如 Discord、電子郵件、Slack 等,以將警報(bào)作為通知發(fā)送給接收者。
PromQL系列
PromQL 是一種靈活的查詢語言,可用于從 Prometheus 查詢時(shí)間序列指標(biāo)。
我們可以直接從用戶界面使用查詢,也可以使用命令通過命令行界面進(jìn)行查詢。Prometheus
curl
Prometheus 用戶界面
通過 CLI 查詢
curl "http://54.186.154.78:30000/api/v1/query?query=$(echo 'up' | jq -s -R -r @uri)" | jq .
此外,當(dāng)您將 prometheus 作為數(shù)據(jù)源添加到 Grafana 時(shí),您可以使用 PromQL 查詢和創(chuàng)建 Grafana 儀表板,如下所示。
結(jié)論
本文解釋了 Prometheus 架構(gòu)的主要組件,并將提供 Prometheus 配置的基本概述,您可以使用該配置執(zhí)行更多操作。文章來源:http://www.zghlxwxcb.cn/news/detail-820529.html
每個(gè)組織的要求都不同,Prometheus 在不同環(huán)境中的實(shí)現(xiàn)也各不相同,例如 VM 和 Kubernetes。如果您了解基礎(chǔ)知識(shí)和關(guān)鍵配置,則可以在任何平臺(tái)上輕松實(shí)現(xiàn)它。文章來源地址http://www.zghlxwxcb.cn/news/detail-820529.html
到了這里,關(guān)于Prometheus 架構(gòu)全面解析的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!