promethues 教程

简介:

prometheus 是一套开源的监控、报警、时间序列数据库的组合

  • 主要组件:
    a. prometheus server
    主要负责数据采集和存储,提供promQL查询语言的支持
    b. client Libraris/SDK
    各个的客户端库和sdk
    c. push Gateway
    用于支持临时任务的推送网关,各个客户端可以主动向push gateway 推送监控指标数据,prometheus会到push gateway上拉取
    d. alertmanager
    告警功能
    e. exporters
    用来监控,haproxy,StatsD,Graphite等特殊的监控目标,并向Prometheus提供标准的监控样本数据
    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
启动Prometheus : 127.0.0.1:9101/graph // 默认是9000,我本地9000端口冲突修改了

grafana: 127.0.0.1:3000
node_exporter: 127.0.0.1:9100/metrics // 数据收集

配置

prometheus.yml 
 # 搜刮配置
scrape_configs:
  - job_name: 'prometheus'
    # 覆盖全局默认值,每15秒从该作业中刮取一次目标
    scrape_interval: 15s
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'alertmanager'
    scrape_interval: 15s
    static_configs:
    - targets: ['alertmanager:9093']
alert.yml

数据指标

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:

提供监控样本数据的程序,exporter的一个实例称为target.prometheus 通过轮询方式定期从这些target中获取样本数据。

  • exporter 类型
    1. 直接采集型:内置了相应的程序,如kubernetes
    2. 间接采集型: 需要我们使用Prometheus提供的client library 编写采集程序,如 node exporter
  • 规范:
    1. ‘#’ 开始通常为注释
    2. 采集后的数据,Prometheus会把他添加labels:instance="django", job="django_app" 和timestamp


      *

node-exporter

    1. 监控linux 服务器
  • 指标:
    cpu: node_cpu_seconds_total; node_load1
    内存(node_memory_MemTotal_bytes: 内存总大小)
    磁盘:node_disk_
    文件系统:node_filesystem_
    网络采集:node_network_
    *3 触发器设置:
    配置:alert.yml


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

nginx_exporter

  1. 配置NGINX
  • nginx 开启stub_status
  • 监控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 服务'
    
image.png
  1. 添加触发器:alert.yml


    image.png

添加监控的流程

image.png

image.png

pushgateway

image.png
  • docker 镜像:prom/pushgateway
    添加Prometheus 配置文件
- job_name: 'pushgateway'
   honor_labels: true // 不会覆盖Prometheus的指标名
   static_configs:
     - targets: ['192.168.199.218:9091']
       labels:
           instance: pushgateway
  1. 推送数据给pushgateway
    echo 'some_metric 3.14' | curl --data-binary @- http://192.168.199.218:9091/metrics/job/some_job

    // 删除某个组下面所有数据
    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 语言

  • 数据类型

瞬时向量(instant vector):
区间向量
标量
字符串


image.png
  • 瞬时向量过滤器

标签过滤
node_cpu_seconds_total{instance='test服务器'}


image.png
  • 操作符

1.1 二元运算符
+、-、*、/、%、^
瞬时向量和标量计算
瞬时向量和瞬时向量运算
1.2 关系运算符
==
!= 不等于

、<、>=、<=
1 > bool 0 # 结果为1 0
1 > 0 # 结果为true false

1.3 集合运算符
and/or unless: 排除后面的

  1. 聚合操作

sum(求和)
min(),max()
avg(平均值)
stddev(标准差)
bstdvar(标准差异)
count(计数)
count_values(对value 进行计数)
bottomk(样本值最小的k个元素)
topk(样本值最大的k个元素, 从大到小排序)
quantile(分布统计)
without: 排除标签名称
by :保留标签名称,类sql似group by

// 统计cpu 和,排除了标签为cpu,job,mode
sum(node_cpu_seconds_total) without (cpu,job,mode)


// 统计cpu 和,包含标签为cpu,job,mode
sum(node_cpu_seconds_total) by (cpu,job,mode)
  • 基于时间聚合

max_over_time(range-vector): 区间向量内每个指标的最大值
avg_over_time(range-vector):
min_over_time(range-vector):
min_over_time(node_timex_sync_status[1m]) 1 分钟内,最小的值

  • 向量匹配

具有相同标签的会进行运算。
使用group_left 指定左侧操作数组中可以有多个匹配样本
on()
ignoring(mode): 忽略 mode 标签 ,group_right 有原则

image.png

image.png
  • 内置函数
    abs() 绝对值
    sqrp() 求平方根
    round(5.6)值四舍五入

time();时间,秒

increase(node_load1[2m]) 增长量
rate(node_load1[2m]) 可以直接计算区间向量v在时间窗口平均增长速率
irate(): 瞬时增长率,避免长尾问题。更灵敏

预测Gauge 指标变化趋势函数
predict_linear() 未来趋势

up 标签函数
label_replace() 替换
label_join() 加入

概况

Server:
时序数据库:TSDB
数据抓取(Scraping): 定期从目标端点(Exporter)拉取

Exporters:
Node Exporter
Blockbox Exporter
jobs exporters:项目采集端点
Service Discovery:
服务发现, 自动发现监控目标
Alertmanager
告警系统
Pushgateway:
其他节点push 数据到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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 226,913评论 6 527
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,710评论 3 412
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 174,561评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,278评论 1 306
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,080评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,604评论 1 320
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,698评论 3 434
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,847评论 0 285
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,361评论 1 329
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,330评论 3 353
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,487评论 1 365
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,044评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,738评论 3 342
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,134评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,378评论 1 281
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,053评论 3 385
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,471评论 2 370

推荐阅读更多精彩内容