Prometheus監控告警體系


 

 一. 四種監控方式

   1)  Logging:應用程序,開發中間件,web系統等所輸出的一些結構化/非結構化的日志,例如應用程序通過一個滾動的文件輸出debug或error信息,並通過日志收集系統存儲到Elasticsearch中。這是一種最常見的一種監控方式。

  2)Tracing:比如分布式調用鏈CAT(Central Application Tracking),就是Tracing。Tracing有開始和結束。

  3)Metrics:Metrics和Logging有點類似,Logging屬於離散(不連續的)事件,Logging一半記錄的是文本信息。而Metrics記錄的是數值,可以聚合,是一段時間內某個度量(計數器或者直方圖)的原子或者是元數據,可用於查看某段時間某個參數的指標及其趨勢。例如接收的HTTP數量可以被建模為計數器,每次的HTTP請求即是我們的度量元數據,可以進行簡單的加法聚合,當持續了一段時間我們又可以建模為直方圖。

  4)HelathCheck:用於系統健康檢查,用於定期檢查某個站點服務器是否存活。這種監控方式也是非常重要,常用於集群中。

    

   Prometheus監控則覆蓋HelathCheck和Metrics這兩種方式。Prometheus專注於衡量系統,隨着時間的推移可能會越來越多地追蹤,從而成為Tracing的指標。

       這四種監控方式比較如下圖:

      

 

            圖片來源:https://peter.bourgon.org/go-for-industrial-programming/

  

   幾種監控方式適用場景

    

    Metrics監控分層 

         

 

 

    Metrics通用監控架構模式

       

 

 

二. MDD理念

   MDD(Metrics Driven Development)度量驅動開發。

 

二. 什么是Prometheus?

  Prometheus(普羅米修斯)是由德國SoundCloud公司開發的開源監控報警系統和時序列數據庫(TSDB)。Prometheus使用Go語言開發,是Google BorgMon監控系統的開源版本。2016年由Google發起Linux基金會旗下的原生雲基金會(Cloud Native Computing Foundation), 將Prometheus納入其下第二大開源項目。

   Prometheus目前在開源社區相當活躍。Prometheus和Heapster(Heapster是K8S的一個子項目,用於獲取集群的性能數據。)相比功能更完善、更全面。Prometheus性能也足夠支撐上萬台規模的集群。

   Prometheus官網地址:https://prometheus.io/

   Prometheus官網文檔地址:https://prometheus.io/docs/introduction/overview/

   Prometheus github地址:https://github.com/prometheus/prometheus

 

  Prometheus特點:

    1)多維度(標簽),拉模式(Pull-based)。

    2)白盒(可以深入應用內部來抓取指標數據),黑盒(把應用當成黑盒來監控)監控都支持,。DevOps友好。

 

三. Prometheus架構

  

   Prometheus基本工作原理

    Prometheus的基本原理是通過HTTP協議周期性抓取被監控組件的狀態,任意組件只要提供對應的HTTP接口就可以接入監控。不需要任何SDK或者其他的集成過程。這樣做非常適合做虛擬化環境監控系統,比如VM、Docker、Kubernetes等。輸出被監控組件信息的HTTP接口被叫做exporter 。目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux系統信息(包括磁盤、內存、CPU、網絡等等)。

 

Prometheus與其他監控系統對比

  Prometheus對比Zabbix

    1.  Zabbix使用的是 C 和 PHP, Prometheus 使用 Golang, 整體而言 Prometheus 運行速度更快一點。

    2. Zabbix 屬於傳統主機監控主要用於物理主機、交換機、網絡等監控Prometheus 不僅適用主機監控還適用於 Cloud、SaaS、Openstack、Container 監控。

    3. Zabbix 在傳統主機監控方面有更豐富的插件。

Prometheus vs. Zabbix

作者:KaliArch
鏈接:https://www.imooc.com/article/details/id/292361
來源:慕課網
本文首次發布於慕課網 ,轉載請注明出處,謝謝合作

 

Prometheus環境搭建

  Prometheus下載官網地址:https://prometheus.io/download/

  windows版https://download.csdn.net/download/qq_38688267/12683264

  Linux版:    https://download.csdn.net/download/qq_38688267/12728855

    

      我是mac系統,下載的是這個版本。不過mac下用brew安裝更為方便。

  1. brew安裝prometheus

brew install prometheus

  安裝完后安裝路徑為:/usr/local/Cellar/prometheus/2.22.0/

  2. 默認配置文件在/usr/local/etc/prometheus.yml,內容如下,可以看見prometheus訪問為localhost:9090

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
    - targets: ["localhost:9090"]

   3. 啟動prometheus命令

prometheus --config.file=/usr/local/etc/prometheus.yml

    當提示如下說明啟動成功就可以在瀏覽器上訪問localhost:9090

     

   訪問后展示如下dashboard界面

  

         prometheus還有metrics頁面,輸入http://localhost:9090/metrics地址,這個頁面展示了相關metrics指標,如下圖,這些指標大多是prometheus自己提供,也是說prometheus自己可以監控自己。

       

    在metrice中出現的指標,在dashboard中是可以搜索的,如我要搜索prometheus_engine相關的指標。

   

   在status菜單可以看到配置信息

  

   4.  prometheus啟動和關閉說明

     如果發現啟動時候端口被占用,使用命令查看

     lsof -i tcp:9090

    然后在根據PID在使用kill -9來終止掉。

    Prometheus 提供了 HTTP 關閉接口,但在使用之前,需要通過 --web.enable-lifecycle 參數開啟 lifecycle 功能,然后你就可以使用 HTTP 請求來關閉程序了,例如:

curl -X POST http://localhost:9090/-/quit

    此時 HTTP 接口會返回:

Requesting termination... Goodbye!

 

prometheus配置說明

  官網配置文檔說明:https://prometheus.io/docs/prometheus/latest/configuration/configuration/

    Prometheus可以在運行時重新加載其配置。如果想要重載Prometheus配置,可以給Prometheus的主進程發送SIGHUP信號或者發送post請求給指定端點 /-/reload(前提條件是在啟動時加上--web.enable-lifecycle啟動參數)

  Prometheus的配置文件是YAML格式,當我們運行prometheus二進制文件時,我們通過參數可指定一個配置文件。

prometheus --config.file=prometheus.yml

   Prometheus的配置文件主要分四塊:

    1) global:配置文件的全局信息配置。比如配置兩次信息抓取間隔,配置抓取超時時間等。

    2)alerting:告警配置。

    3)rule_files:告警規則。

    4)scrape_configs:配置數據源,稱為target,每個target用job_name命名。又分為靜態配置和服務發現。

 

    下面是配置文件說明

global:   # 全局配置
  scrape_interval:     15s # 默認抓取周期,可用單位ms、smhdwy #設置每15s采集數據一次,默認1分鍾
  evaluation_interval: 15s # 估算規則的默認周期 # 每15秒計算一次規則。默認1分鍾
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# 規則文件列表
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# 抓取配置列表 下面static_configs設置要抓取信息等地址,比如這里抓取就是Prometheus自己
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.  #這里表示所使用的網絡協議比如http

    static_configs:
    - targets: ['localhost:9090']

 

 Prometheus環境文件及存儲

  Prometheus包括一個本地磁盤時間序列數據庫,但也可選擇與遠程存儲系統集成。

 

 

 

Grafana使用

  Grafana 是一套開源的分析監視平台,支持 Graphite, InfluxDB, OpenTSDB, Prometheus, Elasticsearch, CloudWatch 等數據源,其 UI 非常漂亮且高度定制化。

  Grafana-7.1下載

  官網https://grafana.com/   

      windows版https://download.csdn.net/download/qq_38688267/12683258   

      Linux版https://download.csdn.net/download/qq_38688267/12728849

    Mac安裝

brew install grafana

安裝好后路徑是

/usr/local/Cellar/grafana/7.2.2

啟動命令

grafana-server --config=/usr/local/etc/grafana/grafana.ini --homepath /usr/local/share/grafana --packaging=brew cfg:default.paths.logs=/usr/local/var/log/grafana cfg:default.paths.data=/usr/local/var/lib/grafana cfg:default.paths.plugins=/usr/local/var/lib/grafana/plugins

 

命令輸入后會看到下面日志信息

啟動完成后,瀏覽http://localhost:3000/進入首頁(注:如需修改默認端口,可修改/usr/local/etc/grafana/grafana.ini 這個文件),默認用戶名和密碼都是admin,使用前必須更改密碼。

 

 登陸成功后就是下面這樣

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM