Prometheus配置文件
相關內容原文地址 :
博客園:漠漠顏:Prometheus配置文件
1、全局配置文件簡介
1.1 默認配置文件
# 全局配置
global:
# 默認抓取週期,可用單位ms、smhdwy #設置每15s採集數據一次,默認1分鐘
[ scrape_interval: <duration> | default = 1m ]
# 默認抓取超時
[ scrape_timeout: <duration> | default = 10s ]
# 估算規則的默認週期 # 每15秒計算一次規則。默認1分鐘
[ evaluation_interval: <duration> | default = 1m ]
# 和外部系統(例如AlertManager)通信時爲時間序列或者警情(Alert)強制添加的標籤列表
external_labels:
[ <labelname>: <labelvalue> ... ]
# 規則文件列表
rule_files:
[ - <filepath_glob> ... ]
# 抓取配置列表
scrape_configs:
[ - <scrape_config> ... ]
# Alertmanager相關配置
alerting:
alert_relabel_configs:
[ - <relabel_config> ... ]
alertmanagers:
[ - <alertmanager_config> ... ]
# 遠程讀寫特性相關的配置
remote_write:
[ - <remote_write> ... ]
remote_read:
[ - <remote_read> ... ]
- global: 此片段指定的是prometheus的全局配置, 比如採集間隔,抓取超時時間等。
- rule_files: 此片段指定報警規則文件, prometheus根據這些規則信息,會推送報警信息到alertmanager中。
- scrape_configs: 此片段指定抓取配置,prometheus的數據採集通過此片段配置。
- alerting: 此片段指定報警配置, 這裏主要是指定prometheus將報警規則推送到指定的alertmanager實例地址。
- remote_write: 指定後端的存儲的寫入api地址。
- remote_read: 指定後端的存儲的讀取api地址。
1.2 global片段主要參數
# How frequently to scrape targets by default.
[ scrape_interval: <duration> | default = 1m ] # 抓取間隔
# How long until a scrape request times out.
[ scrape_timeout: <duration> | default = 10s ] # 抓取超時時間
# How frequently to evaluate rules.
[ evaluation_interval: <duration> | default = 1m ] # 評估規則間隔
# The labels to add to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels: # 外部一些標籤設置
[ <labelname>: <labelvalue> ... ]
1.3 scrapy_config片段主要參數
一個scrape_config 片段指定一組目標和參數, 目標就是實例,指定採集的端點, 參數描述如何採集這些實例, 主要參數如下
- scrape_interval: 抓取間隔,默認繼承global值。
- scrape_timeout: 抓取超時時間,默認繼承global值。
- metric_path: 抓取路徑, 默認是/metrics
- scheme: 指定採集使用的協議,http或者https。
- params: 指定url參數。
- basic_auth: 指定認證信息。
- *_sd_configs: 指定服務發現配置
- static_configs: 靜態指定服務job。
- relabel_config: relabel設置。
1.4 static_configs樣例
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']
- job_name: "node"
static_configs:
- targets:
- "192.168.100.10:20001"
- "192.168.100.11:20001
- "192.168.100.12:20001"
1.5 file_sd_configs樣例
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']
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/usr/local/prometheus/prometheus/conf/node*.yml"
# 獨立文件配置如下 cat conf/node-dis.conf
- targets:
- "192.168.100.10:20001"
- "192.168.100.11:20001"
- "192.168.100.12:20001"
或者可以這樣配置 [root@node00 conf]# cat node-dis.yml
- targets:
- "192.168.100.10:20001"
labels:
hostname: node00
- targets:
- "192.168.100.11:20001"
labels:
hostname: node01
- targets:
- "192.168.100.12:20001"
labels:
hostname: node02
通過file_fd_files 配置後我們可以在不重啓prometheus的前提下, 修改對應的採集文件(node_dis.yml), 在特定的時間內(refresh_interval),prometheus會完成配置信息的載入工作。
1.6 consul_sd_file樣例
由於consul的配置需要有consul的服務提供, 這裏簡單部署下consul的服務。
# 進入下載目錄
[root@node00 prometheus]# cd /usr/src/
# 下載
[root@node00 src]# wget https://releases.hashicorp.com/consul/1.6.1/consul_1.6.1_linux_amd64.zip
# 解壓
[root@node00 src]# unzip consul_1.6.1_linux_amd64.zip
Archive: consul_1.6.1_linux_amd64.zip
inflating: consul
# 查看
[root@node00 src]# ls
consul consul_1.6.1_linux_amd64.zip debug kernels node_exporter-0.18.1.linux-amd64.tar.gz prometheus-2.12.0.linux-amd64.tar.gz
# 查看文件類型
[root@node00 src]# file consul
consul: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
# 防止到系統bin目錄
[root@node00 src]# mv consul /usr/local/bin/
# 確保環境變量包含
[root@node00 src]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# 運行測試
[root@node00 consul.d]# consul agent -dev
# 測試獲取成員
[root@node00 ~]# consul members
# 創建配置目錄
[root@node00 ~]#mkdir /etc/consul.d
[root@node00 consul.d]# cat prometheus-node.json
{
"addresses": {
"http": "0.0.0.0",
"https": "0.0.0.0"
},
"services": [{
"name": "prometheus-node",
"tags": ["prometheus","node"],
"port": 20001
}]
}
# 指定配置文件運行
consul agent -dev -config-dir=/etc/consul.d
打開web管理界面 192.169.100.10:8500,查看相應的服務信息。
上面我們可以看到有2個service , 其中prometheus-node是我們定義的service。
和prometheus集成樣例:
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']
- job_name: "node"
consul_sd_configs:
- server: localhost:8500
services:
- prometheus-node
# tags:
# - prometheus
# - node
#- refresh_interval: 1m
- job_name: 'node_exporter'
consul_sd_configs:
- server: 'consul_ip:8500'
services: ['node_exporter']
# 匹配service關鍵字
- job_name: 'service'
consul_sd_configs:
- server: 'consul_ip:8500'
services: []
relabel_configs:
- source_labels: [__meta_consul_tags]
regex: .*service.*
action: keep
在prometheus的target界面上我們看到服務註冊發現的結果。
通過api接口給該service添加一個節點, 看看是否可以同步過來。
[root@node00 ~]# curl -XPUT [email protected] 127.0.0.1:8500/v1/catalog/register
true
[root@node00 ~]# cat node01.json
{
"id":"0cc931ea-9a3a-a6ff-3ef5-e0c99371d77d",
"Node": "node01",
"Address": "192.168.100.11",
"Service":
{
"Port": 20001,
"ID": "prometheus-node",
"Service": "prometheus-node"
}
}
在consul和prometheus中查看:
可以發現通過向consul中特定服務加入節點, 就可以在prometheus動態變動採集的實例地址。