Blackbox Exporter 教程

1. 核心概念

Blackbox Exporter 是 Prometheus 生态系统中的一个组件,用于黑盒监控,主要用于监控网络服务的可用性和性能。

1.1 基本概念

  • 黑盒监控:通过外部请求来检测服务的可用性和响应时间,而不是通过内部指标。
  • 探测类型:支持 HTTP、HTTPS、DNS、TCP、ICMP 等多种协议的探测。
  • 指标收集:收集探测结果的指标,如响应时间、状态码、可用性等。
  • 配置灵活性:支持通过配置文件定义不同的探测目标和参数。

1.2 工作原理

  1. 接收请求:接收 Prometheus 的 scrape 请求,包含目标和模块参数。
  2. 执行探测:根据模块配置执行相应的探测操作(如 HTTP 请求、ICMP ping 等)。
  3. 收集指标:收集探测过程中的各种指标,如响应时间、状态码等。
  4. 返回指标:将收集到的指标以 Prometheus 格式返回给 Prometheus。

2. 安装配置

2.1 二进制安装

  1. 下载二进制文件

从 GitHub Releases 页面下载适合您系统的二进制文件:

# 下载最新版本
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz

# 解压
 tar xvfz blackbox_exporter-0.25.0.linux-amd64.tar.gz

# 进入目录
 cd blackbox_exporter-0.25.0.linux-amd64

# 运行
 ./blackbox_exporter

2.2 Docker 安装

docker run -d \n  --name blackbox_exporter \n  -p 9115:9115 \n  -v "$(pwd)/blackbox.yml:/etc/blackbox_exporter/config.yml" \n  prom/blackbox-exporter:v0.25.0

2.3 配置文件

创建 blackbox.yml 配置文件:

modules:
  http_2xx:
    prober: http
    timeout: 5s
    http:
      valid_status_codes: [200,301,302]
      method: GET
      no_follow_redirects: false
      tls_config:
        insecure_skip_verify: false
  http_post_2xx:
    prober: http
    timeout: 5s
    http:
      method: POST
      valid_status_codes: [200]
  icmp:
    prober: icmp
    timeout: 5s
    icmp:
      preferred_ip_protocol: "ip4"
  tcp_connect:
    prober: tcp
    timeout: 5s

2.4 Prometheus 配置

在 Prometheus 配置文件中添加 Blackbox Exporter 作为目标:

scrape_configs:
  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_2xx]  # 默认为 HTTP 2xx 探测
    static_configs:
      - targets:
        - https://example.com  # 要监控的目标
        - http://localhost:8080
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: localhost:9115  # Blackbox Exporter 的地址

3. 基本使用

3.1 访问 Web 界面

启动 Blackbox Exporter 后,可以通过 http://localhost:9115 访问其 Web 界面。

3.2 手动执行探测

通过 Web 界面可以手动执行探测,测试目标的可用性:

  1. 在 Web 界面的 "Probe Target" 输入框中输入目标 URL。
  2. 选择要使用的模块(如 http_2xx)。
  3. 点击 "Probe" 按钮执行探测。
  4. 查看探测结果和返回的指标。

3.3 常用模块

  • http_2xx:探测 HTTP/HTTPS 服务是否返回 2xx 状态码。
  • icmp:使用 ICMP ping 探测目标主机是否可达。
  • tcp_connect:探测 TCP 端口是否可连接。
  • dns:探测 DNS 解析是否正常。

4. 高级功能

4.1 自定义模块

可以根据需要自定义探测模块,例如:

modules:
  http_custom:
    prober: http
    timeout: 10s
    http:
      valid_status_codes: [200,401]
      method: GET
      headers:
        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
      tls_config:
        insecure_skip_verify: false
      follow_redirects: true
      preferred_ip_protocol: "ip4"

4.2 高级 HTTP 探测

  • 自定义请求头:添加自定义的 HTTP 请求头。
  • POST 请求:发送 POST 请求并携带数据。
  • 认证:支持基本认证、Bearer Token 等认证方式。
  • TLS 配置:自定义 TLS 配置,如跳过证书验证。

4.3 指标增强

  • 响应时间分布:通过 histogram 类型的指标记录响应时间分布。
  • 详细状态码:记录具体的 HTTP 状态码。
  • 证书信息:对于 HTTPS 探测,记录证书的过期时间等信息。

5. 最佳实践

5.1 配置最佳实践

  • 合理设置超时:根据网络环境和服务特性设置合理的探测超时时间。
  • 使用合适的模块:根据探测目标选择合适的探测模块。
  • 避免过度探测:合理设置探测间隔,避免对目标服务造成过大压力。
  • 使用标签:通过标签区分不同类型的探测目标,便于后续的查询和告警。

5.2 部署最佳实践

  • 独立部署:Blackbox Exporter 通常独立部署,与 Prometheus 分离。
  • 多实例部署:对于大规模监控,可以部署多个 Blackbox Exporter 实例,避免单点故障。
  • 网络可达性:确保 Blackbox Exporter 能够访问所有需要探测的目标。
  • 资源限制:根据探测目标的数量和频率,合理设置 Blackbox Exporter 的资源限制。

5.3 告警最佳实践

  • 基于可用性:设置基于可用性的告警,当服务不可用时及时通知。
  • 基于响应时间:设置基于响应时间的告警,当服务响应缓慢时及时通知。
  • 基于状态码:设置基于 HTTP 状态码的告警,当服务返回错误状态码时及时通知。
  • 合理设置阈值:根据服务的特性和历史数据,设置合理的告警阈值。

6. 实用应用案例

6.1 网站可用性监控

场景:监控公司网站的可用性和响应时间。

配置

modules:
  website:
    prober: http
    timeout: 10s
    http:
      valid_status_codes: [200,301,302]
      method: GET
      follow_redirects: true
      tls_config:
        insecure_skip_verify: false

scrape_configs:
  - job_name: 'website_monitoring'
    metrics_path: /probe
    params:
      module: [website]
    static_configs:
      - targets:
        - https://www.example.com
        - https://api.example.com
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: blackbox-exporter:9115

告警规则

alerts:
  - alert: WebsiteDown
    expr: probe_success{job="website_monitoring"} == 0
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "Website {{ $labels.instance }} is down"
description: "Website {{ $labels.instance }} has been down for more than 5 minutes."

  - alert: WebsiteSlow
    expr: probe_duration_seconds{job="website_monitoring"} > 2
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "Website {{ $labels.instance }} is slow"
description: "Website {{ $labels.instance }} response time is more than 2 seconds for more than 5 minutes."

6.2 网络设备监控

场景:监控网络设备的可达性。

配置

modules:
  icmp:
    prober: icmp
    timeout: 5s
    icmp:
      preferred_ip_protocol: "ip4"

scrape_configs:
  - job_name: 'network_devices'
    metrics_path: /probe
    params:
      module: [icmp]
    static_configs:
      - targets:
        - 192.168.1.1  # 路由器
        - 192.168.1.2  # 交换机
        - 192.168.1.3  # 防火墙
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: blackbox-exporter:9115

告警规则

alerts:
  - alert: NetworkDeviceDown
    expr: probe_success{job="network_devices"} == 0
    for: 2m
    labels:
      severity: critical
    annotations:
      summary: "Network device {{ $labels.instance }} is down"
description: "Network device {{ $labels.instance }} has been down for more than 2 minutes."

6.3 API 服务监控

场景:监控 RESTful API 服务的可用性和响应时间。

配置

modules:
  api_probe:
    prober: http
    timeout: 10s
    http:
      valid_status_codes: [200,401]
      method: GET
      headers:
        Authorization: Bearer <token>
      follow_redirects: false

scrape_configs:
  - job_name: 'api_monitoring'
    metrics_path: /probe
    params:
      module: [api_probe]
    static_configs:
      - targets:
        - https://api.example.com/users
        - https://api.example.com/products
        - https://api.example.com/orders
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: blackbox-exporter:9115

告警规则

alerts:
  - alert: ApiDown
    expr: probe_success{job="api_monitoring"} == 0
    for: 3m
    labels:
      severity: critical
    annotations:
      summary: "API {{ $labels.instance }} is down"
description: "API {{ $labels.instance }} has been down for more than 3 minutes."

  - alert: ApiSlow
    expr: probe_duration_seconds{job="api_monitoring"} > 1
    for: 3m
    labels:
      severity: warning
    annotations:
      summary: "API {{ $labels.instance }} is slow"
description: "API {{ $labels.instance }} response time is more than 1 second for more than 3 minutes."

7. 总结

Blackbox Exporter 是 Prometheus 生态系统中一个强大的黑盒监控工具,通过模拟外部请求来监控网络服务的可用性和性能。它支持多种协议的探测,配置灵活,易于集成到 Prometheus 监控系统中。

通过合理配置和使用 Blackbox Exporter,可以实现对网站、API 服务、网络设备等各种网络服务的全面监控,及时发现和解决问题,提高服务的可靠性和可用性。

7.1 核心优势

  • 多协议支持:支持 HTTP、HTTPS、DNS、TCP、ICMP 等多种协议的探测。
  • 配置灵活:通过配置文件可以定义不同的探测模块和参数,适应各种监控场景。
  • 易于集成:与 Prometheus 无缝集成,使用 Prometheus 的查询语言和告警系统。
  • 轻量级:部署简单,资源消耗低,适合大规模监控。

7.2 应用前景

随着微服务架构的普及和云原生技术的发展,对网络服务的监控需求越来越高。Blackbox Exporter 作为一种黑盒监控工具,在以下场景中有着广泛的应用前景:

  • 云服务监控:监控云服务的可用性和性能。
  • 微服务监控:监控微服务之间的通信和依赖。
  • 边缘设备监控:监控边缘设备的在线状态。
  • 全球性能监控:通过部署在不同地区的 Blackbox Exporter 实例,监控全球范围内的服务性能。

通过不断探索和实践 Blackbox Exporter 的功能和最佳实践,可以构建更加完善和可靠的监控系统,为业务的稳定运行提供有力保障。

« 上一篇 Node Exporter 中文教程 下一篇 » Alertmanager 教程