1.解决的问题
-
之前手动配置了很多个target
-
手动配置维护成本高,还容易出错
2.基于文件的服务发现配置
3.特点
-
基于文件的服务发现提供了一种配置静态目标的更通用的方法
-
并充当了插入自定义服务发现机制的接口。
-
摆脱对特定服务发现源的依赖
-
只要能正确给出 json/yaml文件即可
4.yaml文件类型
- targets:
- 172.20.70.205:9100
- 172.20.70.215:9100
labels:
account: "huaweicloud-01"
region: "ap-south-1"
5.json文件类型
[
{
"targets": [
"172.20.70.205:9100"
],
"labels": {
"account": "huaweicloud-01",
"region": "ap-south-1",
"env": "prod",
"group": "inf",
"project": "monitor",
"stree_gpa": "inf.monitor.prometheus"
}
},
{
"targets": [
"172.20.70.215:9100"
],
"labels": {
"account": "huaweicloud-02",
"region": "ap-south-2",
"env": "prod",
"group": "inf",
"project": "middleware",
"stree_gpa": "inf.middleware.kafka"
}
}
]
-
targets 是一组实例地址的列表
-
labels 是这组实例的标签,应用到列表中所有实例
-
如果想每个实例不同的标签,可以将targets列表保留一个实例即可
-
标签可以自定义
-
那么prometheus在采集对应target时就会将对应标签打入其metrics中
-
为后续我们按照标签过滤提供方便
6.配置举例
-
files 代表 文件路径 支持通配符
-
refresh_interval 代表 文件刷新间隔
- job_name: 'node_exporter'
scrape_interval: 30s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
honor_timestamps: false
file_sd_configs:
- files:
- /opt/app/prometheus/sd/node_exporter.json #json或者yaml,文件自己创建
refresh_interval: 5m
- job_name: 'blackbox-http'
metrics_path: /probe
# 传入的参数
params:
module: [http_2xx] # Look for a HTTP 200 response.
scrape_interval: 15s
scrape_timeout: 10s
scheme: http
honor_timestamps: false
file_sd_configs:
- files:
- /opt/app/prometheus/sd/blackbox_http.json
refresh_interval: 2m
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 172.20.70.205:9115