Prometheus
- bilibili-prometheus:
- 教材:
- github:
- docs:
- api文档:
- node_exporter:
- rate函数算法:
prometheus存储的是时序数据,即按相同时序(相同名称和标签),以时间维度存储连续的数据的集合。
exporter
使用docker部署prometheus和grafana
docker run --name prometheus -d -p 127.0.0.1:9090:9090 prom/prometheus
docker run -d --name=grafana -p 3000:3000 grafana/grafana
备注
grafana配置prometheus数据源时,IP地址输入prometheus容器被分配的IP地址,而不是 http://localhost:9090
。
(localhost是访问了grafana容器本地web地址了)
IP地址可通过 docker inspect container
获取。
端口号是配prometheus容器内部端口号,而不是暴露给外部的端口号, 例如 -p 9091:9090
, grafana应该配9090端口号。
配置文件
这个配置文件的维护非常麻烦,特别是监控项非常多的情况下,很容易出现配置错误,而在Kubernetes上部署Prometheus, 可以不用去维护这个配置文件,而是通 过一个叫ServiceMonitor的资源来自动发现监控目标并动态生成配置。
rate和irate
在使用rate和increase时,难免会遇到”长尾问题”, 也就是某个指标一直处于一个数值,这个增长率即为0,此时会出现误判。 而irate计算是是瞬时增长率,即irate是通过区间向量中最后两个样本数据来计算区间向量的增长率的,这种方式可以适当避免一些指标的”长尾效应”。
但是如果需要分析长期趋势,或者在配置告警规则时,irate过高的灵敏度会造成干扰,针对长期趋势分析和告警规则,更推荐使用rate函数。
Prometheus集群监控方法有哪些?
https://www.zhihu.com/question/590835999
关键字: k8s, Prometheus Operator
ansible部署prometheus
踩坑经验:
prometheus_binary_url指定了下载路径,默认是github, 而prometheus安装包超过100M,所以基本上会下载失败
解决办法是提前下载好安装包, 放到prometheus_local_cache_path变量的位置(默认是/tmp/prometheus-linux-amd64/3.0.1)
以及手工设置 __common_binary_checksums
变量(ansible.builtin.get_url当dest存在以及checksum校验一致时跳过执行)
下载prometheus的ansible代码: https://github.com/prometheus-community/ansible/blob/0.23.0/roles/_common/tasks/install.yml#L64
---
- hosts: localhost
roles:
- role: prometheus.prometheus.prometheus
vars:
# 修改默认变量
prometheus_version: 3.0.1
prometheus_targets:
node:
- targets:
- localhost:9100
# 手工从github下载安装包, 没必要执行校验了(因为又要访问一次github)
# https://github.com/prometheus-community/ansible/blob/0.23.0/roles/_common/tasks/install.yml#L73
prometheus_checksums_url: ""
# 手工设置checksums的内容避免访问github
__common_binary_checksums: {
"prometheus-3.0.1.linux-386.tar.gz": "sha256:c7254094c03f259e5138f0b5bfe51249b242b04c002bb906ec79394323c8f6bb",
"prometheus-3.0.1.linux-amd64.tar.gz": "sha256:43f6f228ef59e0c2f6994e489c5c76c6671553eaa99ded0aea1cd31366222916",
"prometheus-3.0.1.linux-arm64.tar.gz": "sha256:58e8d4f3ab633528fa784740409c529f4a434f8a0e3cf4d2f56e75ce2db69aa8",
"prometheus-3.0.1.linux-armv5.tar.gz": "sha256:517ffe6d74c1d875d6ff70df87640f26b1ac9f92e2f7fba80932cfa8eb6d7b8f",
"prometheus-3.0.1.linux-armv6.tar.gz": "sha256:8bce8e51d782087a2f99389bedff49035d25b7eb3690631e37ab362eb5952e7b",
"prometheus-3.0.1.linux-armv7.tar.gz": "sha256:7291dd21d2255e06c030b979eb0bd965ebc69f7cfa96c7c5b2b674ba5dcf63d5",
"prometheus-3.0.1.linux-mips.tar.gz": "sha256:f78520b93e8a4186649a75a3df0e340c348c82b7fb9711c88bb2c23e78ea761c",
"prometheus-3.0.1.linux-mips64.tar.gz": "sha256:91efe9741bf780970ffc0ba6b871b169cf2f131adf4357e60f73faf203bf1d9a",
"prometheus-3.0.1.linux-mips64le.tar.gz": "sha256:af8df9f2cdd93cda2261a4dd44ccfa4bc84c3f3ce886e91d527c0151dd0948bc",
"prometheus-3.0.1.linux-mipsle.tar.gz": "sha256:222666f762f02fdd5182d8a39590168f5208fd530aa8bf5573920406ace0cb74",
"prometheus-3.0.1.linux-ppc64.tar.gz": "sha256:43ed4755aad1977f9119eaa3ccf124d693ee24d56a5e1c70797a703107e0c1a2",
"prometheus-3.0.1.linux-ppc64le.tar.gz": "sha256:16fe327b691d43e9b2e7ae5a7047db8a2b0bb651b3bf5665113b184fa29e4d06",
"prometheus-3.0.1.linux-riscv64.tar.gz": "sha256:3fda6fc891ac12b323c6a4cec72d40bbe6e2d0f9f6864c4beb2213f03a5d2063",
"prometheus-3.0.1.linux-s390x.tar.gz": "sha256:e5422cad325d6cc83f4fee22159a2ae30582f1332243365e29c1fd34c4500798",
"prometheus-3.0.1.windows-386.tar.gz": "sha256:3ff36757354e46a4fdb81d4248b0d090dcba2275e589fb898dcd6dc7d29f2c41",
"prometheus-3.0.1.windows-386.zip": "sha256:c8401e8eef512e9727bb65b9bb8830e3d4a4e343df9bd7fa5e29c062e9ff2af4",
"prometheus-3.0.1.windows-amd64.tar.gz": "sha256:5f7441790c0f2702a89c0840b860e402c4a0a697f2f06df3a62a03ec044530b6",
"prometheus-3.0.1.windows-amd64.zip": "sha256:6edc3f9f2f1d6e7e9066d4ef33aa0c425aa0a078517ad341631a720b382b20eb",
"prometheus-3.0.1.windows-arm64.tar.gz": "sha256:39c6ddd9ce537c590ad8f6e8afc74e0b9a834805558b41567135eb7736380063",
"prometheus-3.0.1.windows-arm64.zip": "sha256:0a60c6982eeae55bcf0be3cf565db5098635925b9962b9da81fc0f5bc20aa8b9",
"prometheus-web-ui-3.0.1.tar.gz": "sha256:30fec1ee0551427b295cb2cd98f1fcb770d829bb1090357d84f2586ccdab07d3"
}
# 默认的tmp会自动清理掉文件, 应该持久化缓存
prometheus_local_cache_path: "/opt/prometheus-{{ ansible_system | lower }}-{{ _prometheus_go_ansible_arch }}/{{ prometheus_version }}"
- role: prometheus.prometheus.node_exporter
# https://prometheus-community.github.io/ansible/branch/main/node_exporter_role.html
vars:
node_exporter_version: 1.8.2
node_exporter_checksums_url: ""
__common_binary_checksums: {
"node_exporter-1.8.2.linux-amd64.tar.gz": "sha256:6809dd0b3ec45fd6e992c19071d6b5253aed3ead7bf0686885a51d85c6643c66",
}
node_exporter_local_cache_path: "/opt/node_exporter-{{ ansible_system | lower }}-{{ _node_exporter_go_ansible_arch }}/{{ node_exporter_version }}"
tags:
- node_exporter