Prometheus Blackbox 外部探测

FreeGuideOnline 最新 2026-07-13

什么是 Prometheus Blackbox 外部探测

Blackbox Exporter 是 Prometheus 生态中专门用于黑盒监控的组件。它允许你从外部探测目标服务的可用性、响应时间和功能正确性,无需在被监控目标上安装任何 Agent。

常见场景:

  • 检测网站是否能正常访问(HTTP/HTTPS 探测)
  • 验证域名解析是否正常(DNS 探测)
  • 测试 TCP 端口是否存活(TCP 探测)
  • 检查 TLS 证书是否过期(证书探测)
  • 测量 ICMP 延迟和丢包率(ICMP 探测)

Blackbox Exporter 的核心思想是:主动发起探测 → 收集指标 → 暴露给 Prometheus 拉取


架构与工作流程

flowchart LR
    Prometheus[Prometheus Server] -->|拉取 http://blackbox:9115/probe| Blackbox[Blackbox Exporter]
    Blackbox -->|执行探测| Target[目标服务]
    Blackbox -->|返回指标| Prometheus
    Target -.->|响应/超时| Blackbox
  1. Prometheus 通过 probe 接口向 Blackbox Exporter 发起请求,携带目标地址和模块参数。
  2. Blackbox Exporter 根据配置的模块定义,执行相应的探测(HTTP、TCP、DNS 等)。
  3. 探测完成后,Blackbox 返回一组标准格式的指标。
  4. Prometheus 存储这些指标,后续可用于 Grafana 面板和告警。

快速安装 Blackbox Exporter

二进制安装

GitHub Releases 下载对应平台的二进制文件。

tar -xzf blackbox_exporter-*.linux-amd64.tar.gz
cd blackbox_exporter-*.linux-amd64
./blackbox_exporter --config.file=blackbox.yml

默认监听端口为 9115

Docker 安装

docker run -d -p 9115:9115 --name blackbox_exporter \
  -v /path/to/blackbox.yml:/config/blackbox.yml \
  prom/blackbox-exporter:latest --config.file=/config/blackbox.yml

配置文件详解

配置文件使用 YAML 格式,核心是 modules 定义。

modules:
  http_2xx:                     # 模块名,可自定义
    prober: http                # 探针类型:http, tcp, dns, icmp
    timeout: 5s                 # 探测超时时间
    http:
      valid_status_codes: [200] # 认为成功的 HTTP 状态码(支持范围)
      method: GET
      headers:                  # 自定义请求头(可选)
        Host: www.example.com
      fail_if_ssl: false        # 如果 SSL 证书无效则标记失败
      fail_if_not_ssl: false
      preferred_ip_protocol: ip4 # 优先使用的 IP 协议

  tcp_connect:
    prober: tcp
    timeout: 5s
    tcp:
      query_response:          # 期望的响应正则(可选)
        - send: "PING"
        - expect: "PONG"

  dns_tcp:
    prober: dns
    dns:
      transport_protocol: tcp  # 使用 TCP 进行 DNS 查询
      query_name: "example.com"
      query_type: "A"

  icmp_check:
    prober: icmp
    timeout: 5s
    icmp:
      preferred_ip_protocol: ip4

详细的配置选项可查看 官方文档


常用探针类型详解

HTTP 探针

HTTP 探针是使用最广泛的类型,可检测 Web 服务状态、响应时间、TLS 证书有效期等。

典型配置示例

http_2xx_long:
  prober: http
  timeout: 10s
  http:
    valid_status_codes: [200, 201]  # 允许的成功状态码
    method: GET
    headers:
      X-Custom-Header: "value"
    no_follow_redirects: false       # 是否跟随重定向
    fail_if_ssl: false               # 证书问题是否导致失败
    fail_if_not_ssl: false
    tls_config:
      insecure_skip_verify: false    # 跳过证书验证(生产环境慎用)

暴露的重要指标

  • probe_success:探测成功与否(0 或 1)
  • probe_duration_seconds:探测总耗时
  • probe_http_status_code:HTTP 状态码
  • probe_http_version:HTTP 版本
  • probe_ssl_earliest_cert_expiry:证书最早到期时间(Unix 时间戳)

TCP 探针

用于检测任意 TCP 端口的连通性,还可以发送特定字符串并验证响应(类似简单的 TCP 健康检查)。

配置示例

tcp_connect_status:
  prober: tcp
  timeout: 5s
  tcp:
    ip_protocol_fallback: true
    query_response:
      - send: "STATUS\r\n"
        expect: "OK"

如果不需要数据交互,仅测试端口是否可连接,设置 tcp: {} 即可。


ICMP 探针

ICMP 探针能够测量网络延迟和丢包,但需要 root 权限或适当的 Linux capability(CAP_NET_RAW)。

Docker 启动时注意

docker run --cap-add=NET_RAW ...

或者对于二进制运行方式:

sudo setcap cap_net_raw+ep blackbox_exporter

配置示例

icmp_ping:
  prober: icmp
  timeout: 3s
  icmp:
    preferred_ip_protocol: ip4

暴露的指标中 probe_icmp_duration_seconds 可用于分析延迟分布。


DNS 探针

验证 DNS 解析正确性、记录类型、权威服务器等。

配置示例

dns_a_record:
  prober: dns
  timeout: 5s
  dns:
    query_name: "www.example.com"
    query_type: "A"
    valid_rcodes:              # 认为成功的响应码
      - NOERROR

可以检查特定 DNS 服务器:在 Prometheus 抓取时通过参数 target 指定 DNS 服务器地址。


与 Prometheus 集成

静态配置文件方式

prometheus.yml 中添加 scrape 配置:

scrape_configs:
  - job_name: 'blackbox-http'
    metrics_path: /probe
    params:
      module: [http_2xx]              # 使用的模块名
    static_configs:
      - targets:
          - https://www.example.com
          - http://192.168.1.10:8080/health
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target   # 将目标地址传给 Blackbox
      - source_labels: [__param_target]
        target_label: instance         # 保留可读的实例标签
      - target_label: __address__
        replacement: 127.0.0.1:9115    # Blackbox Exporter 地址(可替换为实际地址)

这样 Prometheus 会抓取 http://blackbox:9115/probe?target=xxx&module=http_2xx

基于服务发现的配置

对于动态环境,可以使用 file_sd_configsconsul_sd_configskubernetes_sd_configs 等。

示例:对 Kubernetes 中的 Ingress 进行 HTTP 探测。

- job_name: 'blackbox-ingress-http'
  metrics_path: /probe
  params:
    module: [http_2xx]
  kubernetes_sd_configs:
    - role: ingress
  relabel_configs:
    - source_labels: [__meta_kubernetes_ingress_scheme, __address__, __meta_kubernetes_ingress_path]
      regex: (.+);(.+);(.*)
      replacement: ${1}://${2}${3}
      target_label: __param_target
    - source_labels: [__param_target]
      target_label: instance
    - target_label: __address__
      replacement: blackbox-exporter.monitoring:9115

告警规则设置

下面给出一些实用的告警规则,保存在 rules/blackbox-alerts.yml 中,并在 Prometheus 中引用。

groups:
  - name: blackbox_probe_alerts
    rules:
      - alert: ProbeTargetDown
        expr: probe_success == 0
        for: 2m
        labels:
          severity: critical
        annotations:
          summary: "探测目标 {{ $labels.instance }} 不可达"
          description: "模块 {{ $labels.module }} 探测目标 {{ $labels.instance }} 已经失败超过 2 分钟"

      - alert: ProbeHighLatency
        expr: probe_duration_seconds > 5
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "探测目标 {{ $labels.instance }} 延迟过高"
          description: "模块 {{ $labels.module }} 探测目标 {{ $labels.instance }} 的响应时间超过 5 秒(当前值 {{ $value }}s)"

      - alert: SslCertificateExpiry
        expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 30
        for: 30m
        labels:
          severity: warning
        annotations:
          summary: "SSL 证书即将过期({{ $labels.instance }})"
          description: "目标 {{ $labels.instance }} 的 SSL 证书将在 {{ $value | humanizeDuration }} 后过期"

      - alert: DnsProbeFailure
        expr: probe_success{probe=~"dns.*"} == 0
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "DNS 探测失败 {{ $labels.instance }}"
          description: "DNS 查询 {{ $labels.query_name }} 失败,模块 {{ $labels.module }}"

高级用法与技巧

自定义请求体和响应校验(HTTP POST)

http_post_check:
  prober: http
  http:
    method: POST
    headers:
      Content-Type: application/json
    body: '{"key": "value"}'
    valid_status_codes: [200]
    fail_if_body_not_matches_regexp:
      - "ok"

使用 relabel_configs 动态覆盖模块

如果某些目标需要使用不同模块,可以通过 __param_module 覆盖 params 中的默认模块。

relabel_configs:
  - source_labels: [__param_module_override]
    target_label: __param_module

然后在目标上添加标签 __param_module_override 即可。

多目标探测与黑名单

Blackbox Exporter 支持单个 probe 请求同时探测多个目标(multi-target),不过通常由 Prometheus 逐一抓取更易管理。在 prometheus.yml 中使用 static_configstargets 列表即可并行抓取。


常见问题排查

问题现象 可能原因 解决方法
probe_success 始终为 0 超时时间过短、目标不可达、模块参数错误 检查 timeout 设置,手动 curl Blackbox 的 probe 接口测试
证书过期不告警 模块未启用 fail_if_ssl 或未解析到证书指标 确认配置中 http.fail_if_ssl: true,检查 probe_ssl_earliest_cert_expiry 是否存在
ICMP 探测无权限 非 root 用户运行,无 CAP_NET_RAW 为二进制添加 setcap cap_net_raw+ep 或使用 root 运行(不推荐)
DNS 探测返回成功但解析错误 valid_rcodes 未包含期望值 明确设置 valid_rcodes 列表
负载较高时探测延迟增大 Blackbox Exporter 资源不足或因网络拥塞 独立部署 Blackbox Exporter 实例,增加副本,调整 Prometheus scrape interval

总结

Prometheus Blackbox Exporter 是外部可观测性必不可少的工具。通过灵活配置多种探针类型,可以无侵入地监控 HTTP、DNS、TCP、ICMP 等服务的可用性与性能。结合 Prometheus 和 Alertmanager,能够构建起完善的主动探测告警体系,提前发现用户侧可能遇到的问题。