prometheus入门
Prometheus(普罗米修斯) 是一种 新型监控告警工具,Kubernetes的流行带动了Prometheus的应用。
全文参考自 prometheus学习笔记(1)-mac单机版环境搭建
Mac上安装Prometheus
brew install prometheus
安装路径在 /usr/local/Cellar/prometheus/2.20.1,
配置文件在 /usr/local/etc/prometheus.yml:
1 | global: |
切换到安装目录的bin文件夹,
cd /usr/local/Cellar/prometheus/2.20.1/bin
执行:
./prometheus –config.file=/usr/local/etc/prometheus.yml
此时可访问localhost:9090
,有如下页面:
在Status中可以看到若干元信息.
安装grafana
brew install grafana
安装路径在 /usr/local/Cellar/grafana/7.1.5,
配置文件在 /usr/local/etc/grafana/grafana.ini,
可在此修改默认的端口等信息启动:
grafana-server –config=/usr/local/etc/grafana/grafana.ini –homepath /usr/local/share/grafana –packaging=brew cfg:default.paths.logs=/usr/local/var/log/grafana cfg:default.paths.data=/usr/local/var/lib/grafana cfg:default.paths.plugins=/usr/local/var/lib/grafana/plugins
访问localhost:3000
,有如下页面:
Grafana 是一个单纯的前端图表展示工具, 必须为其添加数据源,才能读取到数据进而进行展示, 参考下图:
Grafana 支持非常多的数据源.
选择Prometheus数据源, 指定URL地址, 然后保存即可
安装pushgateway
在 Prometheus官网搜索pushgateway
, 下载 pushgateway-1.2.0.darwin-amd64.tar.gz
本地解压,运行
./pushgateway
此时, 在localhost:9091/
页面Status这个Tab页,可见:
即 客户端(不管是通过代码, 还是直接终端敲命令行) 将数据push到网关(pushgateway), 然后Prometheus从网关pull数据
修改Prometheus的配置文件
vim /usr/local/etc/prometheus.yml
增加如下几行,重启Prometheus,以让新的配置文件生效.
1 | - job_name: "push-metrics" |
(Prometheus提供了多种语言的sdk, 最简单的方式是通过shell)
推送一个指标:
1 | echo "cui_metric 100" | curl --data-binary @- http://localhost:9091/metrics/job/dashen_blog |
推送多个指标:1
2
3
4
5cat <<EOF | curl --data-binary @- http://localhost:9091/metrics/job/dashen_blog
blog_visit_total{blogid="12345",domain="dashen.tech",clientip="10.0.1.1"} 20
blog_visit_total{blogid="34567",domain="dashen.tech",clientip="10.0.1.2"} 30
blog_visit_total{blogid="56789",domain="dashen.tech",clientip="10.0.1.3"} 40
EOF
此时http://localhost:9091上, 已能看到刚才push的数据:
http://localhost:9090, Prometheus里也能感知刚添加的数据
Prometheus本质上是一个时序数据库. curl命令可以多执行几次, 相当于持续向时序数据库中写入数据.
配置Grafana
1.新建Dashboard
可在设置中进行相关修改
2.添加图表
参考&进阶:
原文作者: fliter
原文链接:
http://www.dashen.tech/2020/09/14/prometheus入门/版权声明: 转载请注明出处