Prometheus 提供了多种服务发现选项来发现抓取目标,包括 Kubernetes、Consul 和许多其他选项。如果您需要使用当前不支持的服务发现系统,Prometheus 的基于文件的服务发现机制可能最适合您的用例,它允许您在 JSON 文件中列出抓取目标(以及这些目标的元数据)。
在本指南中,我们将
targets.json
文件,指定 Node Exporter 的主机和端口信息targets.json
文件发现 Node Exporter请参阅使用 Node Exporter 监控 Linux 主机指标指南的这一部分。Node Exporter 运行在 9100 端口。要确保 Node Exporter 正在暴露指标
curl http://localhost:9100/metrics
指标输出应类似于此
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
...
与 Node Exporter 一样,Prometheus 是一个单一的静态二进制文件,您可以通过 tarball 安装。下载适用于您平台的最新版本并解压。
wget https://github.com/prometheus/prometheus/releases/download/v*/prometheus-*.*-amd64.tar.gz
tar xvf prometheus-*.*-amd64.tar.gz
cd prometheus-*.*
解压后的目录包含一个 prometheus.yml
配置文件。将其当前内容替换为以下内容:
scrape_configs:
- job_name: 'node'
file_sd_configs:
- files:
- 'targets.json'
此配置指定了一个名为 node
的作业(用于 Node Exporter),它从 targets.json
文件中检索 Node Exporter 实例的主机和端口信息。
现在创建 targets.json
文件,并将以下内容添加到其中:
[
{
"labels": {
"job": "node"
},
"targets": [
"localhost:9100"
]
}
]
此配置指定了一个名为 node
的作业,它有一个目标:localhost:9100
。
现在您可以启动 Prometheus
./prometheus
如果 Prometheus 成功启动,您应该在日志中看到类似以下内容的行:
level=info ts=2018-08-13T20:39:24.905651509Z caller=main.go:500 msg="Server is ready to receive web requests."
Prometheus 启动并运行后,您可以使用 Prometheus 的表达式浏览器探索 node
服务暴露的指标。例如,如果您探索 up{job="node"}
指标,您可以看到 Node Exporter 已被正确发现。
使用 Prometheus 的基于文件的服务发现机制时,Prometheus 实例将监听文件的更改并自动更新抓取目标列表,无需重新启动实例。为了演示这一点,请在端口 9200 上启动第二个 Node Exporter 实例。首先导航到包含 Node Exporter 二进制文件的目录,并在新的终端窗口中运行以下命令:
./node_exporter --web.listen-address=":9200"
现在修改 targets.json
中的配置,添加新 Node Exporter 的条目:
[
{
"targets": [
"localhost:9100"
],
"labels": {
"job": "node"
}
},
{
"targets": [
"localhost:9200"
],
"labels": {
"job": "node"
}
}
]
当您保存更改时,Prometheus 将自动收到新的目标列表的通知。up{job="node"}
指标应显示两个实例,其 instance
标签分别为 localhost:9100
和 localhost:9200
。
在本指南中,您安装并运行了一个 Prometheus Node Exporter,并配置 Prometheus 使用基于文件的服务发现机制来发现并抓取 Node Exporter 的指标。
本文档是开源的。请通过提交问题或拉取请求来帮助改进它。