prometheus 中文文档
v2.17
v2.17
  • Prometheus 中文文档
  • introduction
    • 概述
    • 初识 Prometheus
    • 与替代品比较
    • 常见问题
    • 路线图
    • 相关资源
    • 相关术语
  • concepts
    • 数据模型
    • 数据指标类型
    • 作业和实例
  • prometheus
    • 快速开始
    • 安装
    • 配置
      • 配置
      • 定义记录规则
      • 告警规则
      • 模板示例
      • 模板参考
      • 规则的单元测试
    • 查询
      • Prometheus 查询
      • 运算符
      • 函数
      • 查询示例
      • HTTP API
    • 存储
    • 联合
    • 管理 API
    • Prometheus 2.0 迁移指南
    • API 稳定性保证
  • visualization
    • 表达式浏览器
    • Grafana 对 Prometheus 的支持
    • 控制台模板
  • operating
    • 安全模型
    • 集成
  • instrumenting
    • 客户端库
    • 编写客户端库
    • 推送数据指标
    • 数据导出及相关集成
    • 编写数据导出器
    • 公开的格式
  • alerting
    • 告警概述
    • Alertmanager
    • 配置
    • 发送告警
    • 通知模板参考
    • 通知模板示例
    • 管理 API
  • practices
    • 指标和标签命名
    • 控制台和仪表盘
    • 工具
    • Histogram and Summary
    • 告警
    • 记录规则
    • 什么时候使用 Pushgateway
    • 远程写调试
  • guides
    • 使用 cAdvisor 监控 docker 容器数据指标
    • 使用基于文件的服务发现来发现数据采集目标
    • 实现一个 Go 应用
    • 使用 Node Exporter 监控 Linux 主机指标
    • 使用基本身份验证保护 Prometheus API 和 UI 端点
    • 理解并使用 multi-target exporters 模式
    • 使用 TLS 加密 Prometheus API 和 UI 端点
    • 使用 Prometheus 查询日志
由 GitBook 提供支持
在本页
  • 安装并运行 Node Exporter
  • 安装配置并运行 Prometheus
  • 公开发现服务的数据指标
  • 动态更改目标列表
  • 小结

这有帮助吗?

  1. guides

使用基于文件的服务发现来发现数据采集目标

上一页使用 cAdvisor 监控 docker 容器数据指标下一页实现一个 Go 应用

最后更新于5年前

这有帮助吗?

Prometheus 提供了多种用于发现抓取目标的, 包括 , 等。如果您使用系统当前不支持服务发现,则 Prometheus 的机制可能会最好地满足您的用例,您可以在 JSON 文件中列出采集目标(以及有关这些目标的元数据)。

在本指南中,我们将:

  • 在本地安装并运行 Prometheus

  • 创建 targets.json 文件,为 Node Exporter 指定主机和端口信息

  • 安装并运行 Prometheus实例,该实例配置为使用 targets.json 文件发现 Node Exporter

安装并运行 Node Exporter

请参阅的部分。

节点导出器在 9100 端口上运行。为确保节点导出器公开指标:

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
...

安装配置并运行 Prometheus

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 实例的主机和端口信息。

This configuration specifies that there is a job called node (for the Node Exporter) that retrieves host and port information for Node Exporter instances from a targets.json file.

创建 targets.json 文件并将此内容添加到其中:

[
  {
    "labels": {
      "job": "node"
    },
    "targets": [
      "localhost:9100"
    ]
  }
]

NOTE: 在本指南中,为了简洁起见,我们将手动使用 JSON 服务发现配置。 但是,一般而言,我们建议您改用某种 JSON 生成程序或工具。

此配置指定一个带有 localhost:9100 目标的名为 node 的作业

现在你可以启动 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实例将侦听对该文件的更改并自动更新抓取目标列表,而无需重新启动实例。 为了演示这一点,请在端口 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 Node Exporter,并配置了 Prometheus 使用基于文件的服务发现从 Node Exporter 中发现和采集指标。

像 Node Exporter 一样,Prometheus 是一个静态二进制文件,可以通过 tar 包安装。 并解压它:

随着 Prometheus 的启动和运行,您可以使用 Prometheus 检索 node 公开的指标。例如,如果您查询 [ 指标,则可以看到 Node Exporter 恰好被发现。

保存更改后,Prometheus 将自动收到新目标列表的通知。 数据指标将显示instance 标签分别为 localhost:9100 和 localhost:9200 的两个实例,

下载适合您平台的最新版
表达式浏览器
up{job="node"}
up{job="node"}
服务发现选项
Node Exporter
使用 Node Exporter 监控 Linux 主机
安装
Kubernetes
Consul
基于文件的服务发现