blackbox_exporter: prometheus 黑盒探测程序
blackbox_exporter是一种 prometheus 实现的黑盒探测程序,允许通过 HTTP、HTTPS、DNS、TCP、ICMP 和 gRPC 对端点进行黑盒探测。
介绍
Blackbox Exporter 是 Prometheus 生态中的一个重要工具,主要用于 黑盒监控(Blackbox Monitoring),即从外部视角探测目标服务的可用性和性能。以下是其核心作用和应用场景的详细介绍:
核心功能
- 主动探测监控:通过主动向目标发送请求(如 HTTP、TCP、ICMP 等),模拟用户或客户端的访问行为,检测服务是否正常响应
- 多协议支持:
- HTTP/HTTPS:检查 Web 服务的状态码、响应时间、SSL 证书过期时间等
- TCP:验证端口是否开放、服务是否可连接(如数据库、Redis)
- ICMP(Ping):检测主机网络连通性
- DNS:查询 DNS 解析是否正常
- gRPC:监控 gRPC 服务的健康状态
- 灵活的指标导出:将探测结果(如响应时间、成功/失败状态)转换为 Prometheus 可抓取的 metrics
典型应用场景
- 网站/API 可用性监控:
- 定时检测关键接口是否返回
200 OK - 验证登录、支付等核心业务流程是否正常
- 定时检测关键接口是否返回
- 网络质量监控:
- 通过 ICMP 检测跨机房、跨地域的网络延迟和丢包率
- 证书过期监控:
- 提前预警 HTTPS 证书过期时间(通过
probe_ssl_earliest_cert_expiry指标)
- 提前预警 HTTPS 证书过期时间(通过
- 多节点探测:
- 从不同地理位置(如云厂商的多个区域)探测服务,模拟真实用户访问体验
优势特点
- 外部视角:不同于白盒监控(如 Node Exporter),它从用户角度发现问题(如 DNS 污染、CDN 故障)
- 配置简单:通过 YAML 文件定义探测模块(如超时时间、校验规则)
- 与 Prometheus 无缝集成:直接作为 Prometheus 的抓取目标,配合 Alertmanager 实现告警
部署
- 配置
config/blackbox.yml,参考
该配置文件格式:
# 探针类型:http、 tcp、 dns、 icmp.
prober: <prober_string>
# 超时时间
[ timeout: <duration> ]
# 探针的详细配置,最多只能配置其中的一个
[ http: <http_probe> ]
[ tcp: <tcp_probe> ]
[ dns: <dns_probe> ]
[ icmp: <icmp_probe> ]Docker 部署
运行容器:
docker run -d \
--name blackbox_exporter \
-p 9115:9115 \
-v $(pwd)/blackbox.yml:/etc/blackbox_exporter/config.yml \
prom/blackbox-exporter:master --config.file=/etc/blackbox_exporter/config.yml或
docker run --rm \
-p 9115/tcp \
--name blackbox_exporter \
-v ./config:/config \
quay.io/prometheus/blackbox-exporter:v0.26.0 --config.file=/config/blackbox.yml二进制部署 (Linux)
-
下载:从 GitHub Releases 下载最新版。
-
解压与运行:
bashtar xvf blackbox_exporter-*.linux-amd64.tar.gz cd blackbox_exporter-*.linux-amd64 # 直接运行 ./blackbox_exporter --config.file=blackbox.yml -
配置 Systemd 服务(推荐生产环境使用): 创建
/etc/systemd/system/blackbox_exporter.service: ini[Unit] Description=Blackbox Exporter After=network.target [Service] User=root ExecStart=/usr/local/bin/blackbox_exporter --config.file=/etc/blackbox_exporter/blackbox.yml Restart=always [Install] WantedBy=multi-user.target然后执行
systemctl enable --now blackbox_exporter。
prometheus 配置
与 Prometheus 集成,实现对不同 Target 的黑盒探测
scrape_configs:
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [http_2xx] # Look for a HTTP 200 response.
static_configs:
- targets:
- http://prometheus.io # Target to probe with http.
- https://prometheus.io # Target to probe with https.
- http://example.com:8080 # Target to probe with http on port 8080.
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115 # The blackbox exporter's real hostname:port.
- job_name: 'blackbox_exporter' # collect blackbox exporter's operational metrics.
static_configs:
- targets: ['127.0.0.1:9115']示例
http_post_2xx:
prober: http
timeout: 5s
http:
method: POST
headers:
Content-Type: application/json
body: '{}'
http_basic_auth_example:
prober: http
timeout: 5s
http:
method: POST
headers:
Host: "login.example.com"
basic_auth:
username: "username"
password: "mysecret"