一、Prometheus主配置文件
# cat prometheus.yml # my global config global: # 全局配置 scrape_interval: 15s # 設置抓取數據時間為15秒,默認為1分鍾抓取一次 evaluation_interval: 15s # 估算規則的時間為15秒,默認為1分鍾 # scrape_timeout is set to the global default (10s). 抓取超時時間默認為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: # 抓取配置,prometheus的數據采集通過此片段配置 # 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']
二、使用scrape_configs定義采集目標
配置一系列的目標,以及如何抓取它們的參數。一般情況下,每個scrape_config對應單個Job。目標可以在scrape_config中靜態的配置,也可以使用某種服務發現機制動態發現。
# 任務名稱,自動作為抓取到的指標的一個標簽 job_name: <job_name> # 抓取周期 [ scrape_interval: <duration> | default = <global_config.scrape_interval> ] # 每次抓取的超時 [ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ] # 從目標抓取指標的URL路徑 [ metrics_path: <path> | default = /metrics ] # 當添加標簽發現指標已經有同名標簽時,是否保留原有標簽不覆蓋 [ honor_labels: <boolean> | default = false ] # 抓取協議 [ scheme: <scheme> | default = http ] # HTTP請求參數 params: [ <string>: [<string>, ...] ] # 身份驗證信息 basic_auth: [ username: <string> ] [ password: <secret> ] [ password_file: <string> ]
# Authorization請求頭取值 [ bearer_token: <secret> ]
# 從文件讀取Authorization請求頭 [ bearer_token_file: /path/to/bearer/token/file ] # TLS配置 tls_config: [ <tls_config> ] # 代理配置 [ proxy_url: <string> ] # DNS服務發現配置 dns_sd_configs: [ - <dns_sd_config> ... ]
# 文件服務發現配置 file_sd_configs: [ - <file_sd_config> ... ]
# K8S服務發現配置 kubernetes_sd_configs: [ - <kubernetes_sd_config> ... ] # 此Job的靜態配置的目標列表 static_configs: [ - <static_config> ... ] # 目標重打標簽配置 relabel_configs: [ - <relabel_config> ... ]
# 指標重打標簽配置 metric_relabel_configs: [ - <relabel_config> ... ] # 每次抓取允許的最大樣本數量,如果在指標重打標簽后,樣本數量仍然超過限制,則整個抓取認為失敗 # 0表示不限制 [ sample_limit: <int> | default = 0
三、基於文件的服務發現
Prometheus也提供了服務發現功能,可以從consul,dns,kubernetes,file等等多種來源發現新的目標。其中最簡單的是從文件發現服務。
支持服務發現的來源:
azure_sd_configs
consul_sd_configs
dns_sd_configs
ec2_sd_configs
openstack_sd_configs
file_sd_configs
gce_sd_configs
kubernetes_sd_configs
marathon_sd_configs
nerve_sd_configs
serverset_sd_configs
triton_sd_configs
配置基於文件發現的服務:
# vim prometheus.yml scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: "node" file_sd_configs: - refresh_interval: 30s files: - "/usr/local/prometheus/sd_config/node*.yml" # mkdir /usr/local/prometheus/sd_config
五、relabel_configs 允許在采集之前對任何目標及其標簽進行修改
重新標簽的意義:重命名標簽、刪除標簽、過濾標簽