promethues 教程

簡介:

prometheus 是一套開源的監(jiān)控、報(bào)警、時(shí)間序列數(shù)據(jù)庫的組合

  • 主要組件:
    a. prometheus server
    主要負(fù)責(zé)數(shù)據(jù)采集和存儲(chǔ),提供promQL查詢語言的支持
    b. client Libraris/SDK
    各個(gè)的客戶端庫和sdk
    c. push Gateway
    用于支持臨時(shí)任務(wù)的推送網(wǎng)關(guān),各個(gè)客戶端可以主動(dòng)向push gateway 推送監(jiān)控指標(biāo)數(shù)據(jù),prometheus會(huì)到push gateway上拉取
    d. alertmanager
    告警功能
    e. exporters
    用來監(jiān)控,haproxy,StatsD,Graphite等特殊的監(jiān)控目標(biāo),并向Prometheus提供標(biāo)準(zhǔn)的監(jiān)控樣本數(shù)據(jù)
    f. 各種其他支持工具:可視化儀表盤grafana


    image.png

安裝

docker pull prom/node-exporter
docker pull prom/prometheus
docker pull grafana/grafana


docker run -d -p 9100:9100 \
  -v "/proc:/host/proc:ro" \
  -v "/sys:/host/sys:ro" \
  -v "/:/rootfs:ro" \
  prom/node-exporter

docker run  -d \
  -p 9101:9090 \
  -v /Users/albert.liu/opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml  \
  prom/prometheus --name=prometheus_dev


docker run -d \
  -p 3000:3000 \
  --name=grafana \
  -v /Users/albert.liu/opt/grafana-storage:/var/lib/grafana \
  grafana/grafana
啟動(dòng)Prometheus : 127.0.0.1:9101/graph // 默認(rèn)是9000,我本地9000端口沖突修改了

grafana: 127.0.0.1:3000
node_exporter: 127.0.0.1:9100/metrics // 數(shù)據(jù)收集

配置

prometheus.yml 
 # 搜刮配置
scrape_configs:
  - job_name: 'prometheus'
    # 覆蓋全局默認(rèn)值,每15秒從該作業(yè)中刮取一次目標(biāo)
    scrape_interval: 15s
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'alertmanager'
    scrape_interval: 15s
    static_configs:
    - targets: ['alertmanager:9093']
alert.yml

數(shù)據(jù)指標(biāo)

summary 類型,摘要

# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.1207e-05
go_gc_duration_seconds{quantile="0.25"} 0.000153083
go_gc_duration_seconds{quantile="0.5"} 0.000244541
go_gc_duration_seconds{quantile="0.75"} 0.000709667
go_gc_duration_seconds{quantile="1"} 0.006791916
go_gc_duration_seconds_sum 0.077696372
image.png

exporter:

提供監(jiān)控樣本數(shù)據(jù)的程序,exporter的一個(gè)實(shí)例稱為target.prometheus 通過輪詢方式定期從這些target中獲取樣本數(shù)據(jù)。

  • exporter 類型
    1. 直接采集型:內(nèi)置了相應(yīng)的程序,如kubernetes
    2. 間接采集型: 需要我們使用Prometheus提供的client library 編寫采集程序,如 node exporter
  • 規(guī)范:
    1. ‘#’ 開始通常為注釋
    2. 采集后的數(shù)據(jù),Prometheus會(huì)把他添加labels:instance="django", job="django_app" 和timestamp


      *

node-exporter

    1. 監(jiān)控linux 服務(wù)器
  • 指標(biāo):
    cpu: node_cpu_seconds_total; node_load1
    內(nèi)存(node_memory_MemTotal_bytes: 內(nèi)存總大小)
    磁盤:node_disk_
    文件系統(tǒng):node_filesystem_
    網(wǎng)絡(luò)采集:node_network_
    *3 觸發(fā)器設(shè)置:
    配置:alert.yml


    image.png
  • 檢查配置:
    docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml

nginx_exporter

  1. 配置NGINX
  • nginx 開啟stub_status
  • 監(jiān)控nginx 需要with-http_stub_status_module
    檢查是否安裝了
    docker exec -it nginx nginx -V 2>&1 | grep -o with-http_stub_status_module
  • 配置:
    
    server {
    
        location /stub_status{
           stub_status on;
           accecc_log off;
           allow 0.0.0.0/0;
           deny all;
        }
    }
    
    

檢查:curl http://127.0.0.1/stub_status

  1. 安裝nginx_exporter
  2. 配置Prometheus
        - job_name: 'nginx_exporter'
         static_configs:
            - targets: ['192.168.11.11:9113']
              instance: 'test 服務(wù)'
    
image.png
  1. 添加觸發(fā)器:alert.yml


    image.png

添加監(jiān)控的流程

image.png

image.png

pushgateway

image.png
  • docker 鏡像:prom/pushgateway
    添加Prometheus 配置文件
- job_name: 'pushgateway'
   honor_labels: true // 不會(huì)覆蓋Prometheus的指標(biāo)名
   static_configs:
     - targets: ['192.168.199.218:9091']
       labels:
           instance: pushgateway
  1. 推送數(shù)據(jù)給pushgateway
    echo 'some_metric 3.14' | curl --data-binary @- http://192.168.199.218:9091/metrics/job/some_job

    // 刪除某個(gè)組下面所有數(shù)據(jù)
    curl -X DELETE http://192.168.199.218:9091/metrics/job/some_job/instance/some_instance
    // 刪除
    curl -X DELETE http://192.168.199.218:9091/metrics/job/some_job
    a. python 腳本

 pip install prometheus_client

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway

collectorRegistry = CollectorRegistry()
g = Gauge('job_python', 'last python job', registry=collectorRegistry)
g.set_to_current_time()
push_to_gateway('http://192.168.199.218:9091', job='batchA', registry=collectorRegistry)

PromQL 語言

  • 數(shù)據(jù)類型

瞬時(shí)向量(instant vector):
區(qū)間向量
標(biāo)量
字符串


image.png
  • 瞬時(shí)向量過濾器

標(biāo)簽過濾
node_cpu_seconds_total{instance='test服務(wù)器'}


image.png
  • 操作符

1.1 二元運(yùn)算符
+、-、*、/、%、^
瞬時(shí)向量和標(biāo)量計(jì)算
瞬時(shí)向量和瞬時(shí)向量運(yùn)算
1.2 關(guān)系運(yùn)算符
==
!= 不等于

、<、>=、<=
1 > bool 0 # 結(jié)果為1 0
1 > 0 # 結(jié)果為true false

1.3 集合運(yùn)算符
and/or unless: 排除后面的

  1. 聚合操作

sum(求和)
min(),max()
avg(平均值)
stddev(標(biāo)準(zhǔn)差)
bstdvar(標(biāo)準(zhǔn)差異)
count(計(jì)數(shù))
count_values(對value 進(jìn)行計(jì)數(shù))
bottomk(樣本值最小的k個(gè)元素)
topk(樣本值最大的k個(gè)元素, 從大到小排序)
quantile(分布統(tǒng)計(jì))
without: 排除標(biāo)簽名稱
by :保留標(biāo)簽名稱,類sql似group by

// 統(tǒng)計(jì)cpu 和,排除了標(biāo)簽為cpu,job,mode
sum(node_cpu_seconds_total) without (cpu,job,mode)


// 統(tǒng)計(jì)cpu 和,包含標(biāo)簽為cpu,job,mode
sum(node_cpu_seconds_total) by (cpu,job,mode)
  • 基于時(shí)間聚合

max_over_time(range-vector): 區(qū)間向量內(nèi)每個(gè)指標(biāo)的最大值
avg_over_time(range-vector):
min_over_time(range-vector):
min_over_time(node_timex_sync_status[1m]) 1 分鐘內(nèi),最小的值

  • 向量匹配

具有相同標(biāo)簽的會(huì)進(jìn)行運(yùn)算。
使用group_left 指定左側(cè)操作數(shù)組中可以有多個(gè)匹配樣本
on()
ignoring(mode): 忽略 mode 標(biāo)簽 ,group_right 有原則

image.png

image.png
  • 內(nèi)置函數(shù)
    abs() 絕對值
    sqrp() 求平方根
    round(5.6)值四舍五入

time();時(shí)間,秒

increase(node_load1[2m]) 增長量
rate(node_load1[2m]) 可以直接計(jì)算區(qū)間向量v在時(shí)間窗口平均增長速率
irate(): 瞬時(shí)增長率,避免長尾問題。更靈敏

預(yù)測Gauge 指標(biāo)變化趨勢函數(shù)
predict_linear() 未來趨勢

up 標(biāo)簽函數(shù)
label_replace() 替換
label_join() 加入

概況

Server:
時(shí)序數(shù)據(jù)庫:TSDB
數(shù)據(jù)抓取(Scraping): 定期從目標(biāo)端點(diǎn)(Exporter)拉取

Exporters:
Node Exporter
Blockbox Exporter
jobs exporters:項(xiàng)目采集端點(diǎn)
Service Discovery:
服務(wù)發(fā)現(xiàn), 自動(dòng)發(fā)現(xiàn)監(jiān)控目標(biāo)
Alertmanager
告警系統(tǒng)
Pushgateway:
其他節(jié)點(diǎn)push 數(shù)據(jù)到Prometheus
PromQL
查詢語言
Grafana
可視化工具

參考資料

https://www.bilibili.com/video/BV17v4y1H76R/?p=6&spm_id_from=pageDriver&vd_source=5f155e9816129fc687a3807f7a9b1701
https://gitee.com/linge365/docker-prometheus/blob/master/prometheus/prometheus.yml

https://www.bilibili.com/video/BV1K1421q7AE?spm_id_from=333.788.player.switch&vd_source=5f155e9816129fc687a3807f7a9b1701&p=11

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容