一、目标
将配置文件中的各主机的地址分割成多个文件,以减轻prometheus.yml的大小。
二、说明
当我们把所有主机节点的信息都往prometheus服务器的主配置文件prometheus.yml里写的时候,就会造成文件过大、不便管理、甚至你最后都几乎无法去编辑主配置文件了。那么我们就可以将各node节点信息写在分配置文件中,就好比nginx的各站点配置文件一样。
本次prometheus的安装目录是/usr/local/prometheus/
三、直接看一个实际分割例子
1、看prometheus主服务器的主配置文件
vim /usr/local/prometheus/prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
##---下面这几行就起到了配置分割的作用,
##---●file_sd_configs是固定分割配置文件的写法
##---●xTargets/*.json,指的是在当前主配置文件prometheus.yml平级的目录里有个文件夹叫xTargets,且该文件夹下面的所有.json的文件都是target node的列表
##---●refresh_interval: 60s ,指的是刷新间隔60s
- job_name: 'xfile_ds'
file_sd_configs:
- files:
- xTargets/*.json
refresh_interval: 60s
2、新建分割后的配置文件夹目录
mkdir /usr/local/prometheus/xTargets/
3、创建分割的配置文件
vim /usr/local/prometheus/xTargets/xxf.json
[
{
"targets": [
"10.0.0.90:9100"
],
"labels": {
"project_name": "测试项目1",
"env_name": "开发环境1",
"soft_name": "测试应用1",
"template_name": "测试模板名1",
"template_type": "主机模板1",
"group": "linux"
}
},
{
"targets": [
"10.0.0.91:9100"
],
"labels": {
"project_name": "测试项目2",
"env_name": "开发环境2",
"soft_name": "测试应用node1",
"template_name": "测试模板名2",
"template_type": "主机模板2",
"group": "windows"
}
}
]
4、再创建分割的配置文件
vim /usr/local/prometheus/xTargets/xxf2.json
[
{
"targets": [
"10.0.0.92:9100"
],
"labels": {
"project_name": "项目2",
"env_name": "开发环境2",
"soft_name": "测试应用node2",
"template_name": "测试模板名2",
"template_type": "模板222222",
"group": "linux"
}
}
]
5、给配置文件和文件夹改成prometheus权限
chown -R prometheus:prometheus /usr/local/prometheus/xTargets
四、看试验效果
经测试,在prometheus.yml配置文件改动后,需要重启prometheus外,以后再添加node节点进来就不需要再重启prometheus服务了。你可以随意往分割好的文件夹里添加配置文件json,或者往json文件里继续添加键值对就行了,非常方便。啰嗦,你可以随便往被分割的配置文件夹里任意多个配置文件.json
-------------------------ok------------2021年12月5日21:52:21--------------------------
|