使用基于文件的服务发现来抓取目标


Prometheus提供各种服务发现选项,用于发现抓取目标,包括KubernetesConsul等等。 如果您需要使用当前不支持的服务发现系统,Prometheus基于文件的服务发现机制可以最好地为您的用例提供服务,该机制允许您列出JSON文件中的scrape目标(以及有关这些目标的元数据))。

在本指南中,我们将:

  • 在本地安装并运行Prometheus Node Exporter
  • 创建一个targets.json文件,指定Node Exporter的主机和端口信息
  • 安装并运行配置为使用targets.json文件发现Node Exporter的Prometheus实例

安装并运行Node Exporter

请参阅使用节点导出器指南监视Linux主机指标的此部分。 节点导出程序在端口9100上运行。要确保节点导出程序正在公开指标:

  1. curl http://localhost:9100/metrics

指标输出应该像这样:

  1. # HELP go_gc_duration_seconds A summary of the GC invocation durations.
  2. # TYPE go_gc_duration_seconds summary
  3. go_gc_duration_seconds{quantile="0"} 0
  4. go_gc_duration_seconds{quantile="0.25"} 0
  5. go_gc_duration_seconds{quantile="0.5"} 0
  6. ...

安装,配置并运行Prometheus

与Node Exporter一样,Prometheus是一个可以通过tarball安装的静态二进制文件。 下载适用于您平台的最新版本并解压缩它:

  1. wget https://github.com/prometheus/prometheus/releases/download/v*/prometheus-*.*-amd64.tar.gz
  2. tar xvf prometheus-*.*-amd64.tar.gz
  3. cd prometheus-*.*

untarred目录包含prometheus.yml配置文件。 用以下内容替换该文件的当前内容:

  1. scrape_configs:
  2. - job_name: 'node'
  3. file_sd_configs:
  4. - files:
  5. - 'targets.json'

此配置指定存在一个名为node(用于节点导出器)的作业,该作业从targets.json文件中检索节点导出器实例的主机和端口信息。

现在创建targets.json文件并将其添加到其中:

  1. [
  2. {
  3. "labels": {
  4. "job": "node"
  5. },
  6. "targets": [
  7. "localhost:9100"
  8. ]
  9. }
  10. ]

此配置指定存在具有一个目标的node作业:localhost:9100

现在你可以启动Prometheus了:

  1. ./prometheus

如果Prometheus已成功启动,您应该在日志中看到如下所示的行:

  1. 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"}指标,则可以看到正在正确发现节点导出器。

动态更改目标列表

使用Prometheus基于文件的服务发现机制时,Prometheus实例将侦听对文件的更改并自动更新scrape目标列表,而无需重新启动实例。 为了演示这一点,请在端口9200上启动第二个Node Exporter实例。首先导航到包含Node Exporter二进制文件的目录,然后在新的终端窗口中运行此命令:

  1. ./node_exporter --web.listen-address=":9200"

现在通过为新的Node Exporter添加一个条目来修改targets.json中的配置:

  1. [
  2. {
  3. "targets": [
  4. "localhost:9100"
  5. ],
  6. "labels": {
  7. "job": "node"
  8. }
  9. },
  10. {
  11. "targets": [
  12. "localhost:9200"
  13. ],
  14. "labels": {
  15. "job": "node"
  16. }
  17. }
  18. ]

保存更改后,Prometheus将自动收到新目标列表的通知。 up{jo ="node"}指标应显示两个instance标签为localhost:9100localhost:9200

总结

在本指南中,您安装并运行了Prometheus节点导出程序并配置了Prometheus,以使用基于文件的服务发现从节点导出程序中发现和搜索指标。