一、Prometheus是什么
Prometheus是什么
Prometheus(普羅米修斯)是一個最初在SoundCloud上構建的監控系統。自2012年成為社區開源項目,擁有非常活躍的開發人員和用戶社區。為強調開源及獨立維護,Prometheus於2016年加入雲原生雲計算基金會(CNCF),成為繼Kubernetes之后的第二個托管項目。
https://prometheus.io https://github.com/prometheus
作為新一代的監控框架,Prometheus 具有以下特點:
多維數據模型:由度量名稱和鍵值對標識的時間序列數據
- PromSQL:一種靈活的查詢語言,可以利用多維數據完成復雜的查詢
- 不依賴分布式存儲,單個服務器節點可直接工作
- 基於HTTP的pull方式采集時間序列數據
- 推送時間序列數據通過PushGateway組件支持
- 通過服務發現或靜態配置發現目標
- 多種圖形模式及儀表盤支持
Prometheus適用於以機器為中心的監控以及高度動態面向服務架構的監控。
二、Prometheus的組成及架構
2.1 Prometheus 由多個組件組成,但是其中許多組件是可選的:
- Prometheus Server:用於收集指標和存儲時間序列數據,並提供查詢接口
- client Library:客戶端庫(例如Go,Python,Java等),為需要監控的服務產生相應的/metrics並暴露給Prometheus Server。目前已經有很多的軟件原生就支持Prometheus,提供/metrics,可以直接使用。對於像操作系統已經不提供/metrics,可以使用exporter,或者自己開發exporter來提供/metrics服務。
- push gateway:主要用於臨時性的 jobs。由於這類 jobs 存在時間較短,可能在 Prometheus 來 pull 之前就消失了。對此Jobs定時將指標push到pushgateway,再由Prometheus Server從Pushgateway上pull。這種方式主要用於服務層面的 metrics
- exporter:用於暴露已有的第三方服務的 metrics 給 Prometheus。
- alertmanager:從 Prometheus server 端接收到 alerts 后,會進行去除重復數據,分組,並路由到對收的接受方式,發出報警。常見的接收方式有:電子郵件,pagerduty,OpsGenie, webhook 等。
- Web UI:Prometheus內置一個簡單的Web控制台,可以查詢指標,查看配置信息或者Service Discovery等,實際工作中,查看指標或者創建儀表盤通常使用Grafana,Prometheus作為Grafana的數據源;
2.2 數據模型
Prometheus將所有數據存儲為時間序列;具有相同度量名稱以及標簽屬於同一個指標。每個時間序列都由度量標准名稱和一組鍵值對(也成為標簽)唯一標識。
時間序列格式:
<metric name>{<label name>=<label value>, ...}
示例:api_http_requests_total{method="POST", handler="/messages"}
2.3 指標類型
Counter:遞增的計數器
Gauge:可以任意變化的數值
Histogram:對一段時間范圍內數據進行采樣,並對所有數值求和與統計數量
Summary:與Histogram類似
2.4 任務和實例
就Prometheus而言,pull拉取采樣點的端點服務稱之為instance,通常對應一個過程(實例)。具有相同目的的instance,例如,為可伸縮性或可靠性而復制的流程稱為作業,構成了一個job。
scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.5.237:9090'] - job_name: 'node' static_configs: - targets: ['192.168.5.10:9090']
三、部署Prometheus服務
2.1 最新二進制包下載地址:https://prometheus.io/download/
wget https://github.com/prometheus/prometheus/releases/download/v2.18.0-rc.1/prometheus-2.18.0-rc.1.linux-amd64.tar.gz tar xf prometheus-2.18.0-rc.1.linux-amd64.tar.gz -C /usr/local/ ln -s /usr/local/prometheus-2.18.0-rc.1.linux-amd64 /usr/local/prometheus
mkdir -P /usr/local/prometheus/data
2.2 使用systemd管理prometheus服務
# vim /usr/lib/systemd/system/prometheus.service [Unit] Description=https://prometheus.io [Service] Restart=on-failure ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data --web.enable-lifecycle [Install] WantedBy=multi-user.target
2.3 啟動服務
# systemctl daemon-reload
# systemctl start prometheus.service
2.4 訪問服務9090端口
http://192.168.5.55:9090/graph
2.5 默認的metrics接口地址
http://192.168.5.55:9090/metrics