scrape_config片段主要参数
一个scrape_config 片段指定一组目标和参数, 目标就是实例,指定采集的端点, 参数描述如何采集这些实例,主要参数如下
scrape_interval: 抓取间隔,默认继承global值。
scrape_timeout: 抓取超时时间,默认继承global值。
metric_path: 抓取路径, 默认是/metrics
scheme: 指定采集使用的协议,http或者https。
params: 指定url参数。
basic_auth: 指定认证信息。
*_sd_configs: 指定服务发现配置
static_configs: 静态指定服 ob。
relabel_config: relabel设置。
static_configs样例
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
static_configs:
- targets:
- "192.168.100.9:20001"
- "192.168.100.10:20001"
[root@prometheus ~]# systemctl restart prometheus.service
file_sd_configs样例
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/usr/local/prometheus/conf/node*.yml"
[root@prometheus ~]# vim /usr/local/prometheus/conf/node-dis.yml
- targets:
- "192.168.100.9:20001"
labels:
hostname: node1
- targets:
- "192.168.100.10:20001"
labels:
hostname: node2
[root@prometheus ~]# systemctl restart prometheus.service
通过file_fd_files配置后我们可以在不重启prometheus的前提下, 修改对应的采集文件(node_dis.yml), 在特定的时间内(refresh_interval),prometheus会完成配置信息的载入工作。
consul_sd_file样例
由于consul的配置需要有consul的服务提供,这里简单部署下consul的服务。
[root@prometheus ~]# wget https://releases.hashicorp.com/consul/1.6.1/consul_1.6.1_linux_amd64.zip
[root@prometheus ~]# yum -y install unzip
[root@prometheus ~]# unzip consul_1.6.1_linux_amd64.zip
[root@prometheus ~]# cp consul /usr/local/bin/
[root@prometheus ~]# mkdir /etc/consul.d
[root@prometheus ~]# vim /etc/consul.d/prometheus-node.json
{
"addresses": {
"http": "0.0.0.0",
"https": "0.0.0.0"
},
"service" : [{
"name": "prometheus-node",
"tags": ["prometheus","node"],
"port": 20001
}]
}
# 指定配置文件运行
[root@prometheus ~]# consul agent -dev -config-dir=/etc/consul.d
打开web管理界面 192.169.43.254:8500,查看相应的服务信息。
上面我们可以看到有2个service , 其中prometheus-node是我们定义的service。
和prometheus集成样例:
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
consul_sd_configs:
- server: localhost:8500
service:
- prometheus-node
[root@prometheus ~]# systemctl restart prometheus.service
我们通过api接口给该service添加一个节点, 看看是否可以同步过来。
[root@prometheus ~]# vim node1.json
{
"id":"38AA4D56-1DBA-4A11-EA63-B7DF9AE6A5CD", ##id随便写
"Node": "node1",
"Address": "192.168.100.9",
"Service":
{
"Port": 20001,
"ID": "prometheus-node", ##id随便写
"Service": "prometheus-node" ##跟consul中的service保持一致
}
}
[root@prometheus ~]# vim node2.json
{
"id":"39324D56-D82F-768E-56A5-038614189C7D",
"Node": "node2",
"Address": "192.168.100.10",
"Service":
{
"Port": 20001,
"ID": "prometheus-node",
"Service": "prometheus-node"
}
}
上边id的来源:
[root@node1 ~]# cat /sys/class/dmi/id/product_uuid
[root@node2 ~]# cat /sys/class/dmi/id/product_uuid
[root@prometheus ~]# curl -XPUT [email protected] 127.0.0.1:8500/v1/catalog/register
[root@prometheus ~]# curl -XPUT [email protected] 127.0.0.1:8500/v1/catalog/register
在consul和prometheus中查看
可以发现通过向consul中特定服务加入节点, 就可以在prometheus动态变动采集的实例地址。
Prometheus relabel配置
重新标记是一个功能强大的工具,可以在目标的标签集被抓取之前重写它,每个采集配置可以配置多个重写标签设置,并按照配置的顺序来应用于每个目标的标签集。
目标重新标签之后,以__开头的标签将从标签集中删除的。
如果使用只需要临时的存储临时标签值的,可以使用_tmp作为前缀标识。relabel的action类型
replace: 对标签和标签值进行替换。
keep: 采集满足条件的实例。
drop: 采集不满足条件的实例。
labeldrop: 采集没有此标签的实例。
labelkeep: 不采集没有此标签的实例 。
在测试前,同步下配置文件如下。
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/usr/local/prometheus/conf/node*.yml"
[root@prometheus ~]# mkdir -p /usr/local/prometheus/conf
[root@prometheus ~]# vim /usr/local/prometheus/conf/node-dis.yml
- targets:
- "192.168.100.9:20001"
labels:
__hostname__: node1
__businees_line__: "line_a"
__region_id__: "cn-beijing"
__availability_zone__: "a"
- targets:
- "192.168.100.10:20001"
labels:
__hostname__: node2
__businees_line__: "line_b"
__region_id__: "cn-beijing"
__availability_zone__: "b"
##注意__是两个'_'_'
[root@prometheus ~]# chown -R prometheus:prometheus /usr/local/prometheus/conf/
[root@prometheus ~]# systemctl restart prometheus.service
因为我们的label都是以__开头的,目标重新标签之后,以__开头的标签将从标签集中删除的。
将labels中的__hostname__替换为node_name
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
relabel_configs:
- source_labels:
- "__hostname__"
regex: "(.*)"
target_label: "nodename"
action: replace
replacement: "$1"
[root@prometheus ~]# systemctl restart prometheus.service
说下上面的配置: source_labels指定我们我们需要处理的源标签, target_labels指定了我们要replace后的标签名字, action指定relabel动作,这里使用replace替换动作。 regex去匹配源标签(__hostname__)的值,"(.*)"代表__hostname__这个标签是什么值都匹配的,然后replacement指定的替换后的标签(target_label)对应的数值。采用正则引用方式获取的。
keep
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/usr/local/prometheus/conf/node*.yml"
relabel_configs:
- source_labels:
- "__hostname__"
regex: "node1"
action: keep
action为keep,只要source_labels的值匹配regex(node00)的实例才能会被采集。 其他的实例不会被采集。
dorp
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/usr/local/prometheus/conf/node*.yml"
relabel_configs:
- source_labels:
- "__hostname__"
regex: "node1"
action: drop
action为drop,其实和keep是相似的, 不过是相反的, 只要source_labels的值匹配regex(node00)的实例不会被采集。 其他的实例会被采集。
replace
我们的基础信息里面有__region_id__和__availability_zone__,但是我想融合2个字段在一起,可以通过replace来实现。
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
static_configs:
- targets: ['localhost:9090']
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/usr/local/prometheus/conf/node*.yml"
relabel_configs:
- source_labels:
- "__region_id__"
- "__availability_zone__"
separator: "-"
regex: "(.*)"
target_label: "region_zone"
action: replace
replacement: "$1"
[root@prometheus ~]# systemctl restart prometheus
labelkeep
不采集没有此标签的实例
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/usr/local/prometheus/conf/node*.yml"
relabel_configs:
- regex: name
action: labelkeep
[root@prometheus ~]# systemctl restart prometheus
action为keep,只要source_labels的值匹配regex(node00)的实例才能会被采集。 其他的实例不会被采集。
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/usr/local/prometheus/conf/node*.yml"
relabel_configs:
- regex: name
action: labeldrop
给target添加标签
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
static_configs:
- targets: ["localhost:9090"]
labels:
name: node1
age: 110