prometheus教程
目录
简介
记录 prometheus 的相关使用.
安装和配置
安装
参考官网 Installation | Prometheus
下载解压
1 2 3
# 下载 https://prometheus.io/download/ wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz tar -xzvf prometheus-2.45.0.linux-amd64.tar.gz
Systemd 配置文件
/etc/systemd/system/prometheus.service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
[Unit] Description=prometheus # 启动区间30s内,尝试启动3次 StartLimitIntervalSec=30 StartLimitBurst=3 [Service] # 环境变量 $MY_ENV1 # Environment=MY_ENV1=value1 # Environment="MY_ENV2=value2" # 环境变量文件,文件内容"MY_ENV3=value3" $MY_ENV3 # EnvironmentFile=/path/to/environment/file1 WorkingDirectory=/root/prometheus-2.48.1.linux-amd64 ExecStart=/root/prometheus-2.48.1.linux-amd64/prometheus --config.file=/root/prometheus-2.48.1.linux-amd64/prometheus.yml # 总是间隔30s重启,配合StartLimitIntervalSec实现无限重启 RestartSec=30s Restart=always # 相关资源都发送term后,后发送kill KillMode=mixed # 最大文件打开数不限制 LimitNOFILE=infinity # 子线程数量不限制 TasksMax=infinity [Install] WantedBy=multi-user.target Alias=prometheus.service
启动
systemctl daemon-reload ; systemctl enable prometheus.service --now
验证
curl 127.0.0.1:9090
常用配置
启动配置
存储配置文档 Storage | Prometheus
|
|
主配置文件
官网完整配置查看 Configuration | Prometheus
|
|
高可用采集
- 存储: prometheus 的后端存储使用 mimir, 实际存放在 minio 里. 通过集群的方式保证两者高可用.
- k8s 采集: 使用 shards-and-replicas.md 多实例分片拓展.
- 动态服务发现: 使用现有方案过滤, 例如 consul_sd_config, 通过 label 进行花费, 使多个节点均匀分配指标采集. 也可以使用 file_sd_config 自己编写一个动态服务发现
- 手动管理: 通常来说 prometheus 的性能不弱, 部署一个起码能服务 1000 个以上的微服务, 即使手动部署, 也不会是一件太困难的事情.
指标与查询
查询类型
vector
一个时刻的结果 instant querymatrix
一段时间的结果 range query
数据类型
gauge
当前值counter
计数器 时间选择器只能用在这histogram
直方图summary
摘要
常用函数
rate
配合时间,生成条状图sum by(code) (rate(prometheus_http_requests_total[1m]))
仅添加指定标签sum without(code) (rate(prometheus_http_requests_total[1m]))
去除标签topk(5,xxx) xxx
的前 5bottomk(5,xxx) xxx
的后 5sum (rate(prometheus_http_requests_total[1m] offset 1h ) ) -sum (rate(prometheus_http_requests_total[1m]))
环比增加与减少absent(qweoj)===1
表示指标 qweoj 不存在histogram_quantile(0.99,sum (rate(prometheus_http_requests_total[1m])))
分位置rate(node_cpu_seconds_total{mode="idle"}[10m])*100
cpu 空闲率
查询示例
Pod 内存使用率
取每个容器的最大内存值
/ requests 内存
* 100
|
|