乐闻世界logo
搜索文章和话题

Prometheus 的 Pull 和 Push 模式有什么区别,如何选择?

2月21日 15:37

Prometheus 采用 Pull 模式采集数据,但也支持 Push 模式:

Pull 模式(推荐)

  • Prometheus 主动从目标拉取指标
  • 优点:
    • 架构简单,易于扩展
    • 可通过服务发现自动发现目标
    • 支持目标健康检查
    • 降低目标服务复杂度
  • 适用场景:
    • 长期运行的服务
    • 容器化环境
    • Kubernetes 集群

Push 模式

  • 目标主动推送指标到 Pushgateway
  • 优点:
    • 适合短期任务
    • 不需要目标暴露 HTTP 端口
  • 缺点:
    • 无法监控目标健康状态
    • Pushgateway 成为单点
    • 数据可能过时
  • 适用场景:
    • 批处理任务
    • 临时性任务
    • 短期运行的作业

服务发现机制

  • 静态配置
  • Kubernetes API
  • DNS SRV 记录
  • Consul
  • EC2、Azure 等云平台

配置示例

yaml
scrape_configs: - job_name: 'kubernetes-pods' kubernetes_sd_configs: - role: pod

最佳实践:

  • 优先使用 Pull 模式
  • 短期任务使用 Pushgateway
  • 合理设置采集间隔(通常 15s-1m)
标签:Prometheus