Prometheus 是一個開源的系統監控和警報工具包,最初由 SoundCloud 開發,并于2012年開源。它以其強大的多維數據模型、靈活的查詢語言(PromQL)和高效的存儲引擎而聞名。Prometheus 主要用于監控和警報,適用于各種規模的系統,從單機到大規模的分布式系統。
Prometheus 的核心功能包括:
Prometheus 的安裝非常簡單,可以通過以下幾種方式進行:
二進制文件安裝:
wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz
tar -xzf prometheus-2.30.3.linux-amd64.tar.gz
cd prometheus-2.30.3.linux-amd64
./prometheus --config.file=prometheus.yml
Docker 安裝:
docker run -d -p 9090:9090 --name prometheus prom/prometheus
Kubernetes 安裝:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/prometheus
Prometheus 的配置文件是一個 YAML 文件,通常命名為 prometheus.yml。配置文件定義了 Prometheus 的行為,包括監控目標、抓取間隔、告警規則等。
一個簡單的配置文件示例如下:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
Prometheus 的數據模型基于時間序列(time series),每個時間序列由指標名稱(metric name)和一組標簽(labels)唯一標識。時間序列的數據點由時間戳和值組成。
例如,一個時間序列可以表示為:
http_requests_total{method="GET", status="200"} 1027 @1434055562.123
http_requests_totalmethod="GET", status="200"10271434055562.123Prometheus 支持四種主要的指標類型:
PromQL 是 Prometheus 的查詢語言,支持豐富的查詢和聚合操作。以下是一些基本的查詢示例:
查詢某個指標的值:
http_requests_total
查詢帶有特定標簽的指標:
http_requests_total{method="GET"}
查詢某個時間范圍內的指標:
http_requests_total[5m]
PromQL 支持多種聚合操作和函數,如 sum、avg、rate 等。
計算某個指標的總和:
sum(http_requests_total)
計算某個指標的速率:
rate(http_requests_total[5m])
計算某個指標的平均值:
avg(http_requests_total)
PromQL 支持對時間范圍進行查詢,如過去5分鐘、1小時等。
查詢過去5分鐘的請求總數:
sum(http_requests_total[5m])
查詢過去1小時的請求速率:
rate(http_requests_total[1h])
Prometheus 支持通過靜態配置定義監控目標。靜態配置適用于監控目標較少且不經常變化的場景。
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['192.168.1.100:9100', '192.168.1.101:9100']
Prometheus 支持多種動態服務發現機制,如 Kubernetes、Consul、DNS 等。動態發現適用于監控目標較多且經常變化的場景。
scrape_configs:
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
relabel_configs:
- source_labels: [__meta_kubernetes_node_name]
target_label: node
Prometheus 支持通過告警規則定義告警條件。告警規則通常定義在 rules.yml 文件中。
groups:
- name: example
rules:
- alert: HighRequestLatency
expr: job:request_latency_seconds:mean5m{job="myjob"} > 0.5
for: 10m
labels:
severity: page
annotations:
summary: "High request latency"
description: "Request latency is above 0.5 seconds for more than 10 minutes."
Alertmanager 是 Prometheus 的告警管理組件,負責處理告警通知。Alertmanager 的配置文件通常命名為 alertmanager.yml。
global:
resolve_timeout: 5m
route:
receiver: 'email-notifications'
receivers:
- name: 'email-notifications'
email_configs:
- to: 'admin@example.com'
Grafana 是一個開源的可視化工具,支持與 Prometheus 集成。通過 Grafana,可以創建豐富的儀表盤,實時監控系統狀態。
安裝 Grafana:
docker run -d -p 3000:3000 --name grafana grafana/grafana
配置 Prometheus 數據源:
Configuration -> Data Sources。http://localhost:9090)。創建儀表盤:
Dashboards -> New Dashboard。Prometheus 自帶一個簡單的 Web UI,可以通過瀏覽器訪問 http://localhost:9090。在 Web UI 中,可以執行 PromQL 查詢、查看告警規則、監控目標等。
Prometheus 支持將數據存儲到遠程存儲系統中,如 Thanos、Cortex 等。遠程存儲可以解決 Prometheus 本地存儲的容量和持久性問題。
remote_write:
- url: "http://thanos:10908/api/v1/receive"
Prometheus 支持聯邦集群(Federation),允許將多個 Prometheus 實例的數據聚合到一個中心 Prometheus 實例中。
scrape_configs:
- job_name: 'federate'
honor_labels: true
metrics_path: '/federate'
params:
'match[]':
- '{job="prometheus"}'
static_configs:
- targets:
- 'prometheus1:9090'
- 'prometheus2:9090'
Prometheus 支持多種服務發現機制,如 Kubernetes、Consul、DNS 等。服務發現可以動態地發現和監控目標。
scrape_configs:
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_name]
target_label: pod
Prometheus 數據丟失:
告警誤報:
查詢性能差:
增加存儲容量:
調整告警閾值:
優化查詢:
Prometheus 是一個功能強大且靈活的系統監控和警報工具,適用于各種規模的系統。通過合理的配置和使用,可以有效地監控系統的運行狀態,及時發現和解決問題。本文介紹了 Prometheus 的基本概念、安裝配置、數據模型、查詢語言、監控目標、告警與通知、可視化與儀表盤、高級功能、最佳實踐以及常見問題與解決方案。希望本文能幫助讀者更好地理解和使用 Prometheus。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。