天天看點

CNCF實踐之prmetheusprometheus 簡介實踐

prometheus 簡介

什麼是prometheus

prometheus 是一個開源的系統,用于監控和提示套件。 prometheus 于2016加入CNCF,作為第二個寄宿的項目,在kerbernetes 之後

特點

  1. 一個有着被測量名字和鍵值對辨別的時間序列資料的多重次元的資料模型
  2. Promql, 一個可以提供這種次元特性的靈活的查詢語言
  3. 不依賴于分布式存儲
  4. 通過http來拉取模型來收集時間序列
  5. 通過服務發現和靜态配置來發現目标
  6. 支援多節點圖形化和儀表監控

組成部分

  1. prometheus server 用于收集和存儲序列化資料
  2. 用戶端庫用于測量應用代碼
  3. 推送網關用于支援短生命周期的任務
  4. alertmanager 處理提示

架構

CNCF實踐之prmetheusprometheus 簡介實踐

prometheus 從被加了檢測儀器的任務中收集測量資料,直接或間接的通過 用于 短期生命中期的任務push gateway。 它會存儲所有提取的特征,根據規則運作代碼來聚合和記錄新的時間序列從已有的資料或者産生提示

優點

記錄任何純數字的時間序列

可靠,可以快速的診斷問題,每個 prometheus server 是獨立的,不依賴于網絡存儲或其他遠端服務。

缺點

如果你需要100%準确性,prometheus 可能做不到,例如對于每個請求的計費,因為收集的資料可能不會很詳細和完整。

實踐

下載下傳和運作

下載下傳相應系統的prometheus

tar xvfz prometheus-*.tar.gz
cd prometheus-*
           

解壓并進入目錄

配置prometheus 并監控prometheus本身

prometheus 從監控的目标中通過抓取測量http 端點來收集資料。因為prometheus也會以同樣的方式向外暴露自己的資料,是以它可以抓取和監控自己的健康。

配置prometheus.yml

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

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

指令介紹

global 全局配置,作為特定配置沒有情況下的預設配置,特定的配置可以覆寫全局配置,在單個任務下

scrape_interval 抓取時間間隔

external_labels 在與外界系統通信的時候将制定的label賦給時間序列或提示

scrape_configs 抓取配置僅僅包含抓取一個終端的資訊

job_name 指定名字

static_configs 靜态配置發現目标

啟動prometheus

./prometheus --config.file=prometheus.yml
           

這時候通路,即可看見監視界面

localhost:9090

使用表達式浏覽器

進入 (http://localhost:9090/graph) 選擇console,輸入指令

prometheus_target_interval_length_seconds
           

這會傳回不同的時間序列,所有的都有 prometheus_target_interval_length_seconds,但是有不同的label,這些标簽指派不同的延遲百分比

您也可以隻檢視百分比為0.99 的延遲

prometheus_target_interval_length_seconds{quantile="0.99"}
           

使用圖形界面

http://localhost:9090/graph

以下指令會畫出資料塊每秒的産生速率

rate(prometheus_tsdb_head_chunks_created_total[1m])
           
CNCF實踐之prmetheusprometheus 簡介實踐

監控其他目标

下載下傳golang用戶端,分别啟動3個程序,在8080.8081,8082 端口,可以表明prometheus的叢集監控能力

# Fetch the client library code and compile example.
git clone https://github.com/prometheus/client_golang.git
cd client_golang/examples/random
go get -d
go build

# Start 3 example targets in separate terminals:
./random -listen-address=:8080
./random -listen-address=:8081
./random -listen-address=:8082
           

可以在浏覽器通路

http://localhost:8080/metrics

http://localhost:8081/metrics

http://localhost:8082/metrics

檢視服務是否啟動

配置prometheus 監控啟動的服務

在原來的prometheus.yml配置檔案裡加上如下指令

scrape_configs:
  - job_name:       'example-random'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:8080', 'localhost:8081']
        labels:
          group: 'production'

      - targets: ['localhost:8082']
        labels:
          group: 'canary'
           

上面指令加上了分組,比如8080,8081為一組,8082為一組,通過target指定監控目标

我們可以通過以下指令檢視是否成功監控了服務

rpc_durations_seconds
           
CNCF實踐之prmetheusprometheus 簡介實踐

為聚合抓取資料成為新的時間序列配置規則

聚合成千上萬的的時間序列的查詢會比較慢,為了讓這種情況變得更有效,prometheus 允許您通過配置記錄規則為新的時間序列預先記錄表達式。

假設我們想記錄每個服務執行個體在一個5分鐘的視窗内,平均每個每個執行個體的RPC每秒中的次數,我們可以這樣寫(保留 job 和service 次元)

avg(rate(rpc_durations_seconds_count[5m])) by (job, service)
           

繼續閱讀