003、安装

安装

采用二进制包安装,直接解压就可以使用。

github地址: https://github.com/prometheus/prometheus/releases

比如如下版本: prometheus-2.22.2.linux-amd64.tar.gz

# 进入安装包所在目录
cd /opt/
# 解压缩
tar -xvzf prometheus-2.22.2.linux-amd64.tar.gz
# 文件夹改个名字
mv prometheus-2.22.2.linux-amd64 prometheus
# 查看目录
cd prometheus

drwxr-xr-x 2 wjf wjf     4096 Nov 16 22:49 console_libraries
drwxr-xr-x 2 wjf wjf     4096 Nov 16 22:49 consoles
drwxrwxr-x 4 wjf wjf     4096 Nov 19 10:48 data
-rw-r--r-- 1 wjf wjf    11357 Nov 16 22:49 LICENSE
-rw-r--r-- 1 wjf wjf     3420 Nov 16 22:49 NOTICE
-rwxr-xr-x 1 wjf wjf 87591039 Nov 16 20:45 prometheus
-rw-r--r-- 1 wjf wjf      926 Nov 16 22:49 prometheus.yml
-rwxr-xr-x 1 wjf wjf 77678859 Nov 16 20:47 promtool

# 启动,不需要参数设置的话,直接执行主程序即可启动
./prometheus

目录文件说明:

prometheus: prometheus server可运行主程序。

prometheus.yml: 默认提供的配置文件模板。

promtool: 提供的一个prometheus工具箱,提供非交互式的查询,check配置文件等。可以–help查看。

data目录: 默认的TSDB数据文件的存放目录。

console_libraries/consoles目录: 数据可视化接口目录,基本不用。

# 开机自启动配置(systemctl)
# 进入systemd文件目录
cd /usr/lib/systemd/system

# 编写prometheus systemd文件
cat prometheus.service 
[Unit]
Description=prometheus
After=network.target 

[Service]
User=prometheus
Group=prometheus
WorkingDirectory=/opt/prometheus/
ExecStart=/opt/prometheus/prometheus
[Install]
WantedBy=multi-user.target

# 启动
systemctl restart prometheus 
# 查看状态
systemctl status prometheus

# 开机自启配置
systemctl enable prometheus

配置

prometheus的配置有两部分,配置文件和启动参数。

启动参数:

以通过 promethus --help命令查看,以下只列举几个常用的。

./prometheus --help
usage: prometheus [<flags>]
 
The Prometheus monitoring server
 
Flags:
  -h, --help                     Show context-sensitive help (also try --help-long and --help-man).
      --version                  Show application version.
      --config.file="prometheus.yml"         指定prometheus的配置文件                        
      --web.listen-address="0.0.0.0:9090"    监听地址和端口                   
      --web.enable-admin-api                 启用管理API,其中删除metric就会用到这个
      --storage.tsdb.path="data/"            指定TSDB存储的目录                  
      --storage.tsdb.retention.time=STORAGE.TSDB.RETENTION.TIME    TSDB保存时长,默认15天
                                 How long to retain samples in storage. When this flag is set it overrides "storage.tsdb.retention". If neither this flag nor "storage.tsdb.retention" nor
                                 "storage.tsdb.retention.size" is set, the retention time defaults to 15d. Units Supported: y, w, d, h, m, s, ms.
      --storage.tsdb.retention.size=STORAGE.TSDB.RETENTION.SIZE     TSDB最大大小
                                 [EXPERIMENTAL] Maximum number of bytes that can be stored for blocks. A unit is required, supported units: B, KB, MB, GB, TB, PB, EB. Ex: "512MB". This flag
                                 is experimental and can be changed in future releases.

配置文件

prometheus的配置文件使用YAML标记语言。

首先对YAML有个大致的介绍:

1、大小写敏感
2、使用缩进表示层级关系,只允许使用空格缩进,缩进的空格数不重要,只要相同层级的元素左对齐即可。
3、’#'表示注释
4、对象键值对使用冒号结构表示 key: value
5、以 - 开头的行表示构成一个数组

prometheus的默认配置文件模板如下:

# 全局配置
global:
  scrape_interval:     15s #抓取间隔设置为15s,默认1分钟
  evaluation_interval: 15s #评估rule的间隔为15s,默认1分钟
  # scrape_timeout is set to the global default (10s).
 
# Alertmanager配置
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
 
# rule配置
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
 
# 抓取配置
scrape_configs:
  # job为配置的基本单位
  - job_name: 'prometheus'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ['localhost:9090']
 
# 下面是增加了remote storage的配置
remote_write:
  - url: "http://xxxx:8086/api/v1/prom/write?db=prometheus"
 
# remote_read:
#   - url: "http://xxxx:8086/api/v1/prom/read?db=prometheus"

global

scrape_interval: 抓取间隔,前面基础部分说了,prometheus是主动拉取exporter的数据,这个主动拉取的动作就是scrape。需要注意,一般从exporter抓取的数据时没有时间的,因此抓取时间就成为了数据时间,查询最新数据,其实是查询当前时间 - 抓取间隔时间范围内的最新一条数据。
evaluation_interval: 评估规则的时间间隔
scrape_timeout: 抓取超时时间。

alerting 和 rule_files
暂时不用,不做研究

remote_write/remote_read
此部分是prometheus支持外部存储的接口,比如数据存放到influxdb、es等。可以不使用。

scrape_configs

配置样例如下:

- job_name: 'test_job'
    scrape_interval: 10s
    honor_labels: true
    metrics_path: '/metrics'
    static_configs:
      - targets:
        - '127.0.0.1:9090'

基本概念

Job:一组抓取单元(target)的组合。

Target: 一个具体的抓取目标端,一般是:格式。target支持静态配置和自动发现。

对于JOB的属性设置参数有

  • scrape_interval: 抓取间隔,默认继承global值。

  • scrape_timeout: 抓取超时时间,默认继承global值。

  • metric_path: 抓取路径, 默认是/metrics

  • scheme: 指定采集使用的协议,http或者https。

  • params: 指定url参数。

  • basic_auth: 指定认证信息。

  • relabel_config: relabel设置。

  • honor_labels:服务端拉取过来的数据也会存在标签,配置文件中也会有标签,这样就可能发生冲突

    true就是以抓取数据中的标签为准

    false就会重新命名抓取数据中的标签为“exported”形式,然后添加配置文件中的标签

  • *_sd_configs: 动态发现的方式配置target。

  • static_configs: 静态配置target。

对于TARGET属性设置有

  • 对于静态配置和自动发现,支持的属性不同。

  • 通用的配置属性为labels,是一个字典,可以为目标端metric增加多个label。

TARGET的自动发现

自动发现是指prometheus主动追踪目标端配置的变化,并自动更新配置,而不需要重启等人工介入。

*_sd_configs 此处就是服务发现工具,不同的发现类,对应的配置参数可能不一样。

#sd就是service discovery的缩写

简单列举几个:

  • azure_sd_configs:
  • consul_sd_configs:
  • dns_sd_configs:
  • file_sd_configs:

TAGET配置样例

下面就只介绍下static_configs(静态配置) 和 file_sd_configs(基于文件的自动发现)。

static_config

- job_name: "node"
   static_configs:
   - targets:
     - 192.168.100.10:20001
     - 192.168.100.11:20001
     labels:
       type: mysql
       alias: busi

说明:

通过设置labels,会对所有target采集的数据,自动加上所有的labels标签,若标签名发生冲突,参考honor_labels属性配置

file_sd_configs

## 主配置文件prometheus.yml文件内容
shell> cat /opt/prometheus/prometheus.yml
- job_name: 'dbs'
    file_sd_configs:
    - files:
      - mycat.yml
      - mysql.yml

shell> cat /opt/prometheus/mysql.yml
## 对应的mysql.yml或者mycat.yml文件内容,格式类似static_configs
- labels:
    type: mysql
    alias: busi
  targets:
  - 192.168.100.10:20001
  - 192.168.100.11:20001

说明:

基于文件的自动发现,prometheus会自动追踪文件的变化,并自动更新最新的配置内容,而不需要重启。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值