Prometheus(三)配置

简介

Prometheus通过命令行和配置文件共同进行配置。一般用命令行配置来配置不变更的属性,如数据存储位置、数据存储时间、存储大小等,具体的命令行参数可通过 ./prometheus -h查看。配置文件主要用来对数据抓取信息、规则信息进行配置。

Prometheus可以在运行时对配置文件的内容进行重新reload,从而实现配置信息的动态更新,如果配置文件格式有问题,Prometheus会继续采用旧的配置项。

重新加载配置项有如下两种方式

  1. 通过向Prometheus进程发送HUP信号

    $ ps -elf |grep -e PID -e prometheus
    
    $ kill -s HUP ${pid} #上一步骤找出来的pid
    
    

    如果Prometheus用systemctl进行控制启动,服务文件中有ExecReload=/bin/kill -s HUP $MAINPID配置项,可以采用如下命令重新加载配置

    $ systemctl reload prometheus.service
    
    
  2. 通过Prometheus暴露的rest接口,需要在启动时指定--web.enable-lifecycle参数

    $ curl -XPOST http://192.168.0.107:9090/-/reload
    
    
    • http://192.168.0.107:9090是Prometheus服务器地址

配置文件

在启动Prometheus服务时,通过--config.file参数来设置读取的配置文件。Prometheus配置文件用yaml格式编辑。配置格式如下

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> ... ]

  # File to which PromQL queries are logged.
  # Reloading the configuration will reopen the file.
  [ query_log_file: <string> ]

# Rule files specifies a list of globs. Rules and alerts are read from
# all matching files.
rule_files:
  [ - <filepath_glob> ... ]

# A list of scrape configurations.
scrape_configs:
  [ - <scrape_config> ... ]

# Alerting specifies settings related to the Alertmanager.
alerting:
  alert_relabel_configs:
    [ - <relabel_config> ... ]
  alertmanagers:
    [ - <alertmanager_config> ... ]

# Settings related to the remote write feature.
remote_write:
  [ - <remote_write> ... ]

# Settings related to the remote read feature.
remote_read:
  [ - <remote_read> ... ]
  
  • global 全局性配置,如Prometheus拉取指标数据的间隔,Prometheus根据rule文件进行计算的周期等
  • rule_files 配置rule文件的路径位置
  • scrape_configs Prometheus拉取指标信息配置项
  • alerting 报警配置信息
  • remote_write 将指标数据写入到外部时间序列数据库(influxdb等)对应的配置信息
  • remote_read 从外部时间序列库读取数据的配置信息

此处主要说明一下啊scrape_configs,其他配置的具体信息可参考configuration

在scrape_configs下可以配置多个scrape_config,每一个scrape_config可以配置一系列的监控对象、以及从这些对象拉取监控数据的参数,例如

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'node'
    static_configs:
    - targets: ['localhost:9100','node2:9100']
  - job_name: 'spring-boot'
    metrics_path: /actuator/prometheus
    static_configs:
    - targets: ['192.168.0.108:8080']
	

其中监控对象可以通过static_configs静态配置一个监控列表,也可以采用动态服务发现技术来自动发现被监控的对象,如kubernetes_sd_configs、dns_sd_configs、file_sd_configs

file_sd_configs是最通用的一种服务发现方式,可以很方便帮助我们实现自定义的服务发现功能,其核心思想是通过disk watches持续观察此属性指定的文件列表,当任一文件中的targets列表发生变更后,立即更新Prometheus的拉取指标数据对象,从变更后的targets列表中获取指标数据。例如Docker Swarm、Packet等都是通过这个机制来进行自动发现监控对象的。

完整配置例子 example file

posted @ 2020-03-20 16:44  高枫_henu  Views(889)  Comments(0Edit  收藏  举报