安装
源码安装
想顺道以后看看源码, 选择了源码安装, 从github下载,按go项目正常流程编译.go版本现在要求>=1.18
git clone https://github.com/prometheus/prometheus.git
cd prometheus
GO111MODULE=on go install github.com/prometheus/prometheus/cmd/...
prometheus --config.file=your_config.yml
运行时遇到问题问题
Error opening React index.html: open web/ui/static/react/index.html: no such file or directory
参考这篇文章解决:Prometheus 2.30.0版本UI界面打不开Error opening React Index.html
但是启动时会卡住,不能启动完成.
官方文档对缺前端文件使用有说明:
However, when using
go installto build Prometheus, Prometheus will expect to be able to read its web assets from local filesystem directories underweb/ui/staticandweb/ui/templates. In order for these assets to be found, you will have to run Prometheus from the root of the cloned repository. Note also that these directories do not include the React UI unless it has been built explicitly usingmake assetsormake build.An example of the above configuration file can be found here.
You can also build using
make build, which will compile in the web assets so that Prometheus can be run from anywhere:make build ./prometheus --config.file=your_config.yml
翻译下就是有两个办法:
- 在克隆回来的项目根目录运行项目, 但是还是缺少React UI
- 用make方式安装
实操用make方法安装执行成功.
docker安装
git clone https://github.com/nacos-group/nacos-docker
这个是安装nacos的仓库, docker-compose里包括了Prometheus和grafana,拿过来直接用了
启动:
docker-compose -f example/standalone-derby.yaml up
成功
使用Prometheus
以下以docker方式安装说明.
最小化配置文件nacos-docker/example/prometheus/prometheus-standalone.yaml
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'nacos'
metrics_path: '/nacos/actuator/prometheus'
static_configs:
- targets: ['nacos:8848']
- job_name: 'node'
static_configs:
- targets: ['host.docker.internal:9100']
配置文件默认接入Prometheus自己和nacos,最后的node是接入node exporter,监控mac m1笔记本
示例配置文件中包含三个配置块:
global,rule_files和scrape_configs。
global配置块控制 Prometheus server 的全局配置。目前有两个参数。scrape_interval参数控制 Prometheus 多久采集一次 targets。您可以为单个 targets 重新配置此参数。在示例中,全局配置为 15 秒采集一次。evaluation_interval参数控制 Prometheus 多久评估一次 rules。Prometheus 使用 rules 来创建新的时间序列并生成报警。
rule_files配置块指定我们希望 Prometheus server 加载规则的文件位置。目前,我们没有任何规则。
scrape_configs配置块控制 Prometheus 监控哪些资源。由于 Prometheus 将有关自身的数据暴露为 HTTP 端点,因此它可以采集并监控其自身的运行状况。在默认配置中,只有一个名为prometheus的作业,它会采集 Prometheus 服务暴露的时间序列数据。该作业包含一个单独的静态配置的 target,为localhost,9090端口。Prometheus 希望数据 指标可以通过 target 的/metrics路径可以访问。所以这个默认作业通过http://localhost:9090/metrics进行指标数据采集。
使用表达式浏览器
让我们尝试查看 Prometheus 收集的有关自身的一些数据。要使用 Prometheus 的内置表达式浏览器,请导航至
http://localhost:9090/graph,然后在 "Graph" 选项卡中选择 "Console" 视图。正如您可以从
http://localhost:9090/metrics获取的数据一样,Prometheus 暴露一个称为promhttp_metric_handler_requests_total(Prometheus 已处理/metrics的请求总数)的指标数据。将其输入到表达式控制台中:promhttp_metric_handler_requests_total
它应该返回多个不同的时间序列(以及每个时间序列的最新值),所有时间序列的 metric 名称均为
promhttp_metric_handler_requests_total,但具有不同的标签。这些标签指定了不同的请求状态。如果我们只对 HTTP 状态码为
200的请求感兴趣,则可以使用此查询来检索该信息:promhttp_metric_handler_requests_total{code="200"}
要计算返回的时间序列数,您可以查询
count(promhttp_metric_handler_requests_total)
有关表达语言的更多信息,请参见表达语言文档
使用grafana
http://localhost:3000/?orgId=1
登陆默认密码admin:admin
接入数据源
在首页可以接入数据源Prometheus,也可以通过Configuration -> datasource添加.注意url和access方式,保存
切换到dashboards默认带了Prometheus模版,直接import
在dashboard首页就可以看到了.
引入node exporter
安装
下载不同系统版本 node exporter,测试机时mac m1新片的cpu,但是奇怪的是node_exporter-1.5.0.darwin-arm64.tar.gz启动失败, node_exporter-1.5.0.darwin-amd64.tar.gz能够运行
tips:
报权限不够的话,通过find右键打开, 可以授权运行
运行
curl http://localhost:9100/metrics
配置Prometheus
修改配置文件nacos-docker/example/prometheus/prometheus-standalone.yaml,添加Node Exporter并重启.
- job_name: 'node'
static_configs:
- targets: ['host.docker.internal:9100']
因为是docker安装的Prometheus,docker需要能访问安装到宿主机的Node Exporter,host填"host.docker.internal"
grafana dashboard
Prometheus下通过node_*方式就可以看到各项指标了. grafana需要配置下新的dashboard.
导入模版
从grafana页面选择所需的dashboard,打开所需dashboard页面, 复制链接或id,填入,点击Load,加载到grafana,就可以看到新dashboard了
总结
Prometheus和granafa的安装使用就基本结束了.
通过看文档,使用Prometheus和接入Exporter, 对Prometheus的使用场景有了比较深刻的认识.Prometheus更多的是一个统计和监控的工具,更多关注的是各种指标的拉取和上报.
之后还需要看下alertManager的配置使用,看看为了实现报警,需要上报什么样的指标, 以及可以实现什么种类的报警.
参考
使用Prometheus和Grafana做服务指标监控可视化(实践篇)