1. 簡介
Prometheus: (簡稱Prom)是由SoundCloud開發的開源監控報警系統。是大名鼎鼎的CNCF雲原生基金會下的第二大開源項目。具有如下特點:
- 使用Go語言開發
- 内置時序資料庫
- 性能足夠支撐上萬台規模的叢集
- 多元度資料模型,靈活的查詢語言
- 通過HTTP的pull方式采集時序資料
- 可以通過中間網關進行時序列資料推送
- 服務發現或者靜态配置來發現被監控對象
- 支援多種多樣的圖表和界面展示,比如Grafana等
架構圖
工作流程
- Prometheus Server通過http協定周期性的pull被監控對象的metrics,然後儲存在本地的TSDB(時序資料庫)中。
- Prometheus Server判斷監控資料是否觸發告警,如果觸發,将告警發送給Alertmanager。
- Alertmanager處理告警,并通過郵件等方式進行通知。
- Prometheus監控資料也可以通過Grafana等可視化工具進行展示。
2. Prometheus環境搭建
注:本文測試環境為單機環境,IP需要修改為自己對應的位址
涉及到的主要元件(括号内表示搭建環境使用的docker鏡像),如下:
- Prometheus Server( prom/prometheus:v2.7.2)
- 被監控對象exporter(prom/node-exporter,輸出被監控對象資訊的http接口,我們叫做exporter)
- 資料上報網關pushgateway (prom/pushgateway)
- Alertmanager(prom/alertmanager,告警系統)
- Grafana (grafana/grafana, 資料可視化工具)
2.1 安裝Prometheus Server
主要涉及到兩個配置檔案和一個服務:
- prometheus.yml #主配置檔案
- rules.yml #告警規則配置檔案
- prom/prometheus #包含Prometheus Server可執行程式的docker鏡像
2.1.1 主配置檔案prometheus.yml
global:
scrape_interval: 15s #預設采集監控資料時間間隔
external_labels:
monitor: 'my-monitor'
scrape_configs: #監控對象設定
- job_name: prometheus #任務名稱
scrape_interval: 5s #每隔5s擷取一次監控資料
static_configs: #監控對象位址
- targets: ['127.0.0.1:9090'] # 将自己加入到監控對象中
2.1.2 告警規則配置檔案rules.yml
# 此處暫時不設定,後面Alertmanager章節具體介紹
2.1.3 啟動prometheus服務
docker run --name=prometheus -d \ #設定容器啟動名稱
-p 9090:9090 \ #暴露prometheus服務端口
-v 本地絕對路徑/prometheus.yml:/etc/prometheus/prometheus.yml \ #挂載本地prometheus.yaml
-v 本地絕對路徑/rules.yml:/etc/prometheus/rules.yml \ #挂載本地rules.yml
prom/prometheus:v2.7.2 \ #prometheus容器鏡像
--config.file=/etc/prometheus/prometheus.yml \ #設定prometheus服務使用的配置檔案
--web.enable-lifecycle #啟動prometheus遠端熱加載配置檔案,方式:curl -X POST http://localhost:9090/-/reload
打開浏覽器,通路http://127.0.0.1:9090/, 如果顯示如下界面,說明prometheus啟動成功
通路http://127.0.0.1:9090/metrics,可以檢視采集的metrics名額
2.2 通過node exporter提供metrics
2.2.1 啟動node exporter
docker run -d --name=node-exporter -p 9100:9100 prom/node-exporter
2.2.2 修改prometheus.yml檔案
将監控對象加入到監控任務中,如下
global:
scrape_interval: 15s
external_labels:
monitor: 'my-monitor'
scrape_configs:
- job_name: prometheus
scrape_interval: 5s
static_configs:
- targets: ['127.0.0.1:9090']
- targets: ['127.0.0.1:9100'] # 配置node-exporter,用于監控主機cpu、記憶體使用和磁盤等
labels:
group: 'client-node-exporter'
2.2.3 檢視監控資訊
首先,重新加載prometheus配置檔案,使用如下方式
curl -X POST http://localhost:9090/-/reload #啟動prometheus的時候,必須帶web.enable-lifecycle參數
打開浏覽器,通路http://127.0.0.1:9090/targets
可以看到,剛才配置的node-exporter已經成功加入到prometheus的targets中。點選該targert(紅線連結),可以看到主機cpu監控資料已經可以正常采集:
2.2 安裝pushgateway
Prometheus采集資料是用的pull方式,prometheus配置檔案設定的5秒就是采集資料的頻率。但是有些資料并不适合采用這樣的方式,對這樣的資料可以使用Push Gateway服務。PushGateway比較适合臨時作業和批處理作業,由于這些作業是short-lived的,如果采用pull的模式,可能在prometheus采集之前,作業已經執行結束。pushgateway相當于一個暫存器,這些臨時作業将metrics資料緩存到pushgateway中,然後等待Prometheus來pull資料。
2.2.1 啟動pushgateway
pushgateway是一個單獨的服務,啟動如下:
docker run -d -p 9091:9091 --name pushgateway prom/pushgateway
通路浏覽器http://localhost:9091/#, 可以看到pushgateway已經成功啟動
2.2.2 pushgateway推送資料
pushgateway支援多種語言的sdk推送資料,下面以shell為例:
- 推送一個名額
echo "exam_metric 100" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/exam
- 一次推送多個名額
cat <<EOF | curl --data-binary @- http://127.0.0.1:9091/metrics/job/exam/instance/test
# 語數外考試成績
chinese 120
math 150
english 140
EOF
打開浏覽器,通路http://localhost:9091/#, 可以看到我們剛才已經成功推送資料到pushgateway。
2.2.3 将pushgateway配置到prometheus中
修改prometheus.yml檔案,配置pushgateway如下:
global:
scrape_interval: 15s
external_labels:
monitor: 'my-monitor'
scrape_configs:
- job_name: prometheus
scrape_interval: 5s
static_configs:
- targets: ['127.0.0.1:9090']
- targets: ['127.0.0.1:9100']
labels:
group: 'client-node-exporter'
- targets: ['127.0.0.1:9091'] # 配置pushgateway位址
labels:
group: 'pushgateway'
打開浏覽器,通路prometheus,通過搜尋框,我們搜尋從pushgateway擷取的資料,如下:
可以看到成功擷取pushgateway資料
chinese 120
2.3 告警管理Alertmanager
Prometheus的告警機制由兩部分組成:
- Prometheus根據告警規則,将告警發送給Alertmanager
- Alertmanager管理告警,包括silencing,inhibition和aggregation;并通過郵件等方式進行外部通知
2.3.1 配置和啟動Alertmanager
本地建立Alertmanager配置檔案alertmanager.yml,内容如下:
global:
resolve_timeout: 5m
route:
group_by: ['exam'] #與prometheus配置檔案rules.yml中配置規則名對應
group_wait: 10s #報警等待時間
group_interval: 10s #報警間隔時間
repeat_interval: 1m #重複報警間隔時間
receiver: 'web.hook' #告警處理方式,我們這裡通過web.hook方式,也可以配置成郵件等方式
receivers:
- name: 'web.hook'
webhook_configs:
- url: 'http://127.0.0.1:8080/exam/test' #告警web.hook位址,告警資訊會post到該位址,需要編寫服務接收該告警資料
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning' #目标告警狀态
equal: ['alertname', 'dev', 'instance']
啟動Alertmanager服務:
docker run -d -p 9093:9093 --name alertmanager \
-v 本地絕對路徑/alertmanager.yml:/etc/alertmanager/alertmanager.yml \ #挂載本地alertmanager配置檔案
prom/alertmanager
打開浏覽器,通路http://127.0.0.1:9093, 顯示如下:
設定prometheus告警規則
編輯rules.yml配置檔案,添加如下資訊:
groups:
- name: exam
rules:
- alert: exam告警測試
expr: chinese > 100 #國文成績大于100告警,注:我們在pushgateway推送資料的時候,設定的是120,是以會觸發告警
for: 1m
labels:
status: warning
annotations:
summary: "{{$labels.instance}}:國文成績優秀! nb了!!!"
description: "{{$labels.instance}}: 國文成績優秀! nbnbnb!!!"
編輯prometheus.yml配置檔案,添加alertmanager配置和rules.yml檔案,如下:
global:
scrape_interval: 15s
external_labels:
monitor: 'my-monitor'
rule_files:
- /etc/prometheus/rules.yml #告警規則檔案路徑,注:此檔案在啟動prometheus容器時,作為volumes挂載到了/etc/prometheus下
scrape_configs:
- job_name: prometheus
scrape_interval: 5s
static_configs:
- targets: ['127.0.0.1:9090']
- targets: ['127.0.0.1:9100']
labels:
group: 'client-node-exporter'
alerting: #告警管理器設定
alertmanagers:
- static_configs:
- targets: ['127.0.0.1:9093'] #配置Alertmanager位址,告警資訊會發送給alertmanager進一步處理
重新加載prometheus配置檔案
curl -X POST http://localhost:9090/-/reload
, 然後打開浏覽器,通路http://127.0.0.1:9093/#/alerts,然後點選圖中的Info,會彈出具體的告警提示資訊,info上面也會提示有1個alert資訊。
注:打開頁面可能需要等一會才有alert資訊推送
告警通知處理
由于我們在alertmanager.yml中配置的告警receiver時
web.hook
方式,推送位址為
http://127.0.0.1:8080/exam/test
, 下面我們模拟個web.hook服務,來接收告警資訊。
我們使用golang代碼來編寫web.hook,檔案名為main.go, 代碼如下:
package main
import (
"fmt"
"io/ioutil"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.POST("/exam/test", func(c *gin.Context) {
res, _ := ioutil.ReadAll(c.Request.Body)
fmt.Println(string(res)) //這裡我們隻簡單列印告警資訊
c.JSON(200, gin.H{
"message": "alert message",
})
})
r.Run(":8080")
}
啟動web服務, 等待接收Alertmanager發送過來的告警資訊。控制台會輸出類似如下資訊:
$ go run main.go
[GIN] 2020/01/20 - 14:01:05 | 200 | 228.075µs | 127.0.0.1 | POST /exam/test
{"receiver":"web\\.hook","status":"firing","alerts":[{"status":"firing","labels":{"alertname":"exam告警測試","exported_instance":"test","exported_job":"exam","group":"pushgateway","instance":"127.0.0.1:9091","job":"prometheus","monitor":"my-monitor","status":"warning"},"annotations":{"description":"127.0.0.1:9091: 國文成績優秀! nbnbnb!!!","summary":"127.0.0.1:9091:國文成績優秀! nb了!!!"},"startsAt":"2020-01-20T05:46:40.339966325Z","endsAt":"0001-01-01T00:00:00Z","generatorURL":"http://caf2be9d9d5d:9090/graph?g0.expr=chinese+%3E+100\u0026g0.tab=1","fingerprint":"00936ad48d797b28"}],"groupLabels":{},"commonLabels":{"alertname":"exam告警測試","exported_instance":"test","exported_job":"exam","group":"pushgateway","instance":"127.0.0.1:9091","job":"prometheus","monitor":"my-monitor","status":"warning"},"commonAnnotations":{"description":"127.0.0.1:9091: 國文成績優秀! nbnbnb!!!","summary":"127.0.0.1:9091:國文成績優秀! nb了!!!"},"externalURL":"http://288d9ac3b9d7:9093","version":"4","groupKey":"{}:{}"}
[GIN] 2020/01/20 - 14:02:05 | 200 | 190.27µs | 192.168.71.92 | POST /exam/test
作者:warm3snow
出處:http://www.cnblogs.com/informatics/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須在文章頁面給出原文連接配接,否則保留追究法律責任的權利。