1. 程式人生 > >Prometheus的介紹與實踐

Prometheus的介紹與實踐

一、Prometheus簡介

Prometheus是一套開源的系統監控報警框架。Prometheus作為新一代的雲原生監控系統,相比傳統監控監控系統(Nagios或者Zabbix)擁有如下優點。
易管理性
Prometheus: Prometheus核心部分只有一個單獨的二進位制檔案,可直接在本地工作,不依賴於分散式儲存。
Nagios: 需要有專業的人員進行安裝,配置和管理,並且過程很複雜。
業務資料相關性
Prometheus:監控服務的執行狀態,基於Prometheus豐富的Client庫,使用者可以輕鬆的在應用程式中新增對Prometheus的支援,從而讓使用者可以獲取服務和應用內部真正的執行狀態。
Nagios:大部分的監控能力都是圍繞系統的一些邊緣性的問題,主要針對系統服務和資源的狀態以及應用程式的可用性。
另外Prometheus還存在以下優點


高效:單一Prometheus可以處理數以百萬的監控指標;每秒處理數十萬的資料點。
易於伸縮:通過使用功能分割槽(sharing)+聯邦叢集(federation)可以對Prometheus進行擴充套件,形成一個邏輯叢集;Prometheus提供多種語言的客戶端SDK,這些SDK可以快速讓應用程式納入到Prometheus的監控當中。
良好的視覺化:Prometheus除了自帶有Prometheus UI,Prometheus還提供了一個獨立的基於Ruby On Rails的Dashboard解決方案Promdash。另外最新的Grafana視覺化工具也提供了完整的Proetheus支援,基於Prometheus提供的API還可以實現自己的監控視覺化UI。

二、Prometheus框架的組成和工作流

2.1 Prometheus的框架組成
Prometheus的框架如下圖(參考自Prometheus官網
在這裡插入圖片描述
Prometheus Server:Prometheus Sever是Prometheus元件中的核心部分,負責實現對監控資料的獲取,儲存及查詢。Prometheus Server可以通過靜態配置管理監控目標,也可以配合使用Service Discovery的方式動態管理監控目標,並從這些監控目標中獲取資料。其次Prometheus Sever需要對採集到的資料進行儲存,Prometheus Server本身就是一個實時資料庫,將採集到的監控資料按照時間序列的方式儲存在本地磁碟當中。Prometheus Server對外提供了自定義的PromQL,實現對資料的查詢以及分析。另外Prometheus Server的聯邦叢集能力可以使其從其他的Prometheus Server例項中獲取資料。
Exporters

:Exporter將監控資料採集的端點通過HTTP服務的形式暴露給Prometheus Server,Prometheus Server通過訪問該Exporter提供的Endpoint端點,即可以獲取到需要採集的監控資料。可以將Exporter分為2類:
直接採集:這一類Exporter直接內建了對Prometheus監控的支援,比如cAdvisor,Kubernetes,Etcd,Gokit等,都直接內建了用於向Prometheus暴露監控資料的端點。
間接採集:原有監控目標並不直接支援Prometheus,因此需要通過Prometheus提供的Client Library編寫該監控目標的監控採集程式。例如:Mysql Exporter,JMX Exporter,Consul Exporter等。
AlertManager:在Prometheus Server中支援基於Prom QL建立告警規則,如果滿足Prom QL定義的規則,則會產生一條告警。在AlertManager從 Prometheus server 端接收到 alerts後,會進行去除重複資料,分組,並路由到對收的接受方式,發出報警。常見的接收方式有:電子郵件,pagerduty,webhook 等。
PushGateway:Prometheus資料採集基於Prometheus Server從Exporter pull資料,因此當網路環境不允許Prometheus Server和Exporter進行通訊時,可以使用PushGateway來進行中轉。通過PushGateway將內部網路的監控資料主動Push到Gateway中,Prometheus Server採用針對Exporter同樣的方式,將監控資料從PushGateway pull到Prometheus Server。

2.2 Prometheus的工作流
1.Prometheus server定期從配置好的jobs或者exporters中拉取metrics,或者接收來自Pushgateway傳送過來的metrics,或者從其它的Prometheus server中拉metrics。
2.Prometheus server在本地儲存收集到的metrics,並執行定義好的alerts.rules,記錄新的時間序列或者向Alert manager推送警報。
3.Alertmanager根據配置檔案,對接收到的警報進行處理,發出告警。
4.在圖形介面中,視覺化採集資料。

三、Prometheus的安裝和配置

3.1 Prometheus的安裝
Prometheus的安裝有兩種方式:1.從官網下載Prometheus映象進行安裝;2.從 docker上獲取映象進行安裝。
從官網下載Prometheus映象進行安裝

wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
tar txvf prometheus-2.3.2.linux-amd64.tar.gz
cd prometheus-2.3.2.linux-amd64.tar.gz
./prometheus --config.file=prometheus.yml

從Docker獲取映象進行安裝

docker pull prom/prometheus

3.2 Prometheus的配置
Prometheus執行的命令配置參考如下

docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml \      
                         -v /tmp/first.rules:/etc/prometheus/first.rules \      
                          -v /tmp/prometheus-data:/prometheus-data \      
                           prom/prometheus

備註:Prometheus在執行過程中會出現各種不同的bug,關於bug的問題和解決方案可以參考部落格Prometheus配置過程中出現的bug及解決方案
Prometheus配置檔案的規範和解析
Prometheus的配置檔案採用的是yaml檔案,yaml檔案書寫規範要求如下
1.大小寫敏感
2.使用縮排表表示層級關係
3.縮排時不允許使用tab鍵,只允許使用空格
4.縮排的空格數目不重要,只要相同層級的元素左側對齊即可
Prometheus的配置檔案解析如下:

# Prometheus全域性配置項
global:
  scrape_interval:     15s # 設定抓取資料的週期,預設為1min
  evaluation_interval: 15s # 設定更新rules檔案的週期,預設為1min
  scrape_timeout: 15s # 設定抓取資料的超時時間,預設為10s
  external_labels: # 額外的屬性,會新增到拉取得資料並存到資料庫中
   monitor: 'codelab_monitor'


# Alertmanager配置
alerting:
 alertmanagers:
 - static_configs:
   - targets: ["localhost:9093"] # 設定alertmanager和prometheus互動的介面,即alertmanager監聽的ip地址和埠
     
# rule配置,首次讀取預設載入,之後根據evaluation_interval設定的週期載入
rule_files:
 - "alertmanager_rules.yml"
 - "prometheus_rules.yml"

# scape配置
scrape_configs:
- job_name: 'prometheus' # job_name預設寫入timeseries的labels中,可以用於查詢使用
  scrape_interval: 15s # 抓取週期,預設採用global配置
  static_configs: # 靜態配置
  - targets: ['localdns:9090'] # prometheus所要抓取資料的地址,即instance例項項

- job_name: 'example-random'
  static_configs:
  - targets: ['localhost:8080']

Prometheus模組定義的告警規則如下

groups:
 - name: test-rules
   rules:
   - alert: InstanceDown # 告警名稱
     expr: up == 0 # 告警的判定條件,參考Prometheus高階查詢來設定
     for: 2m # 滿足告警條件持續時間多久後,才會傳送告警
     labels: #標籤項
      team: node
     annotations: # 解析項,詳細解釋告警資訊
      summary: "{{$labels.instance}}: has been down"
      description: "{{$labels.instance}}: job {{$labels.job}} has been down "
      value: {{$value}}

參考:
1.prometheus官網
2.Prometheus操作指南
3.Prometheus 入門與實踐
4.Prometheus 和 Alertmanager實戰配置
5.使用Prometheus+grafana打造高逼格監控平臺