一个计算机技术爱好者与学习者

0%

Prometheus Blackbox exporter

1. 白盒监控和黑盒监控

我们监控主机的资源用量、容器的运行状态、数据库中间件的运行数据。这些都是支持业务和服务的基础设施,通过白盒能够了解其内部的实际运行状态,通过对监控指标的观察能够预判可能出现的问题,从而对潜在的不确定因素进行优化。而从完整的监控逻辑的角度,除了大量的应用白盒监控以外,还应该添加适当的黑盒监控。黑盒监控即以用户的身份测试服务的外部可见性,常见的黑盒监控包括HTTP探针、TCP探针等用于检测站点或者服务的可访问性,以及访问效率等。

黑盒监控相较于白盒监控最大的不同在于黑盒监控是以故障为导向当故障发生时,黑盒监控能快速发现故障,而白盒监控则侧重于主动发现或者预测潜在的问题。一个完善的监控目标是要能够从白盒的角度发现潜在问题,能够在黑盒的角度快速发现已经发生的问题。

Prometheus Blackbox exporter允许通过HTTP,HTTPS,DNS,TCP和ICMP对端点进行黑盒探测。

本文中,我们会安装blackbox exporter,并且使用http探针对一些接口进行探活。

参考文档:

2. 安装配置Blackbox exporter

Blackbox exporter的安装方法有两种:bin安装和docker安装。这里我们选择使用docker安装。

1、创建配置文件 /opt/exporter/blackbox/blackbox.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
modules:
http_2xx:
prober: http
http:
method: GET
preferred_ip_protocol: "ip4"
valid_status_codes: [200,201]
http_post_2xx:
prober: http
http:
method: POST
preferred_ip_protocol: "ip4"
valid_status_codes: [200,201]
tcp:
prober: tcp
ping:
prober: icmp
timeout: 3s
icmp:
preferred_ip_protocol: "ip4"
dns_baidu:
prober: dns
timeout: 5s
dns:
query_name: "baidu.com"
query_type: "A"
preferred_ip_protocol: "ip4"

探测时默认使用ipv6,探测不支持ipv6的站点都会显示失败,因此在配置时改成了ipv4,更多内容参考UNDERSTANDING AND USING THE MULTI-TARGET EXPORTER PATTERN
注意,最后一个配置项 dns_baidu 的作用是探测DNS解析 baidu.com 是否可以正常解析。

2、启动blackbox exporter

1
2
3
4
docker run --name blackbox-exporter -d \
-p 9115:9115 \
-v /opt/exporter/blackbox/blackbox.yml:/config/blackbox.yml \
prom/blackbox-exporter:v0.25.0 --config.file=/config/blackbox.yml

3、测试

1
2
3
4
5
curl "http://127.0.0.1:9115/probe?module=http_2xx&target=baidu.com"
curl "http://127.0.0.1:9115/probe?module=http_post_2xx&target=baidu.com"
curl "http://127.0.0.1:9115/probe?module=tcp&target=baidu.com:80"
curl "http://127.0.0.1:9115/probe?module=ping&target=baidu.com"
curl "http://127.0.0.1:9115/probe?module=dns_baidu&target=180.76.76.76"

从返回的样本中,可以获取站点的DNS解析耗时、站点响应时间、HTTP响应状态码等等和站点访问质量相关的监控指标,从而帮助管理员主动的发现故障和问题。

3. 与Prometheus集成

3.1. 简单配置

修改 prometheus.yml,添加blackbox exporter相关字段。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
scrape_configs: 
- job_name: baidu_http2xx_probe
params:
module:
- http_2xx
target:
- baidu.com
metrics_path: /probe
static_configs:
- targets:
- 127.0.0.1:9115
- job_name: prometheus_http2xx_probe
params:
module:
- http_2xx
target:
- prometheus.io
metrics_path: /probe
static_configs:
- targets:
- 127.0.0.1:9115

这里分别配置了名为 baidu_http2x_probe 和 prometheus_http2xx_probe 的采集任务,并且通过params指定使用的探针(module)以及探测目标(target)。

3.2. 高级配置

以上配置会有一个问题,假如我们有N个目标站点且都需要M种探测方式,那么Prometheus中将包含N * M 个采集任务,从配置管理的角度来说显然是不可接受的。
这里我们利用Prometheus的Relabeling方式对这些配置进行简化,配置方式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
scrape_configs:
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [http_2xx]
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.
- http://192.168.56.102:8080/healthz # Target to probe with http on path /healthz
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.56.102:9115 # The blackbox exporter's real hostname:port.

这里针对每一个探针服务(如http_2xx)定义一个采集任务,并且直接将任务的采集目标定义为我们需要探测的站点。在采集样本数据之前通过relabel_configs对采集任务进行动态设置。

上面的配置,实际上相当于:

1
2
3
curl "http://192.168.56.102:9115/probe?module=http_2xx&target=http://prometheus.io"
curl "http://192.168.56.102:9115/probe?module=http_2xx&target=https://prometheus.io"
curl "http://192.168.56.102:9115/probe?module=http_2xx&target=http://example.com:8080"

这个配置实际上是比较奇怪的:static_configs.targets 一般代表的是用来exporter对外暴露的接口,但是blackbox这里的 static_configs.targets 代表的是blackbox要探测的站点,relabel_configs.replacement 才是blackbox exporter对外暴露的接口。
blackbox-exporter的这种配置方式,与mysql-exporter、redis-exporter是一致的。

重启prometheus,然后进行验证:
(1)访问blackbox-exporter页面,可以看到探测记录的日志。
(2)访问prometheus的target页面,即可看到blackbox-exporter。
(3)访问prometheus的graph页面,可以查看blackbox-exporter指标。

1
{job=~"blackbox",__name__!~"^go.*"}