Prometheus 实战
  • 前言
  • 修订记录
  • 如何贡献
  • Prometheus 简介
    • Prometheus 是什么
    • 为什么选择 Prometheus
  • Prometheus 安装
    • 二进制包安装
    • Docker 安装
  • 基础概念
    • 数据模型
    • 指标类型
    • 作业与实例
  • PromQL
    • PromQL 基本使用
    • 与 SQL 对比
  • 数据可视化
    • Web Console
    • Grafana
    • Promlens
  • Prometheus 配置
    • 全局配置
    • 告警配置
    • 规则配置
    • 数据拉取配置
    • 远程可写存储
    • 远程可读存储
    • 服务发现
    • 配置样例
  • 服务发现
    • 静态服务发现
    • 文件服务发现
    • HTTP服务发现
    • Consul服务发现
    • moby服务发现
    • kubernetes服务发现
  • Exporter
    • 文本格式
    • Sample Exporter
    • Node Exporter 安装使用
    • Node Exporter 常用查询
    • 其他 Exporter 介绍
  • Pushgateway
    • Pushgateway 是什么
    • 如何使用 Pushgateway
  • 数据存储
    • Local Store
    • Remote Store
  • 告警/记录规则
    • 如何配置
    • 触发逻辑
  • Alertmanager
    • Alertmanager 是什么
    • 配置详情
    • 通过 Email 接收告警
    • 通过企业微信接收告警
    • 通过 Slack 接收告警
    • 通过 Webhook 接收告警
    • 其他告警接收方案
  • Prometheus 工具
    • Promtool 介绍和使用
    • Client SDK
  • Prometheus 性能调优
    • Metrics 仪表盘
    • 启动参数优化
    • 日志查询
  • Prometheus 与容器
    • Docker
    • Kubernetes
  • 高可用方案探讨
    • Prometheus Server 的高可靠
    • AlertManager 的高可靠
  • 实战练习
    • NodeExporter
    • 配置告警规则
    • Grafana 集成
    • Alertmanager 告警
  • 常见问题收录
    • 如何热加载新配置
    • 如何通过认证后拉取数据
Powered by GitBook
On this page

Was this helpful?

  1. 常见问题收录

如何热加载新配置

Previous常见问题收录Next如何通过认证后拉取数据

Last updated 4 years ago

Was this helpful?

当 Prometheus 有配置文件修改,我们可以采用 Prometheus 提供的热更新方法实现在不停服务的情况下实现配置文件的重新加载。

热更新加载方法有两种:

  1. kill -HUP pid

  2. curl -X POST

当你采用以上任一方式执行 reload 成功的时候,将在 promtheus log 中看到如下信息:

如果因为配置信息填写不正确导致更新失败,将看到类似信息:

ERRO[0161] Error reloading config: couldn't load configuration (-config.file=prometheus.yml): unknown fields in scrape_config: job_nae  source=main.go:146

提示:

  1. 我个人更倾向于采用 curl -X POST 的方式,因为每次 reload 过后, pid 会改变,使用 kill 方式需要找到当前进程号。

  2. 从 2.0 开始,hot reload 功能是默认关闭的,如需开启,需要在启动 Prometheus 的时候,添加 --web.enable-lifecycle 参数。

下面我们再来探讨下这两种方式内部实现原理。

第一种:通过 kill 命令的 HUP (hang up) 参数实现:

首先 Prometheus 在 cmd/promethteus/main.go 中实现了对进程系统调用监听,如果收到 syscall.SIGHUP 信号,将执行 reloadConfig 函数。

代码类似:

hup := make(chan os.Signal)
signal.Notify(hup, syscall.SIGHUP)
go func() {
  for {
    select {
    case <-hup:
      if err := reloadConfig(cfg.configFile, reloadables...); err != nil {
        log.Errorf("Error reloading config: %s", err)
      }
    }
  }
}()

第二种:通过 web 模块的 /-/reload 请求实现:

  1. 首先 Prometheus 在 web(web/web.go) 模块中注册了一个 POST 的 http 请求 /-/reload, 它的 handler 是 web.reload 函数,该函数主要向 web.reloadCh chan 里面发送一个 error。

  2. 在 Prometheus 的 cmd/promethteus/main.go 中有个单独的 goroutine 来监听 web.reloadCh,当接受到新值的时候会执行 reloadConfig 函数。

代码类似:

hupReady := make(chan bool)

go func() {
    <-hupReady
    for {
        select {
        case rc := <-webHandler.Reload():
            if err := reloadConfig(cfg.configFile, reloadables...); err != nil {
                log.Errorf("Error reloading config: %s", err)
                rc <- err
            } else {
                rc <- nil
            }
        }
    }
}()

Prometheus 内部提供了成熟的 hot reload 方案,这大大方便配置文件的修改和重新加载,在 Prometheus 生态中,很多 Exporter 也采用类似约定的实现方式。

http://IP/-/reload
hotreload.png