Prometheus 特点

Prometheus 具有以下特点:

1、多维数据模型:由度量名称和键值对标识的时间序列数据
2、PromSQL:一种灵活的查询语言,可以利用多维数据完成复杂的查询
3、不依赖分布式存储,单个服务器节点可直接工作
4、基于HTTP的pull方式采集时间序列数据
5、推送时间序列数据通过PushGateway组件支持
6、通过服务发现或静态配置发现目标
7、多种图形模式及仪表盘支持(grafana)
8、适用于以机器为中心的监控以及高度动态面向服务架构的监控

一:prometheus server 的部署

在 peometheus的官网下载prometheus的源码文件:https://prometheus.io/download/ ,linux 下载之后是一个tar包,直接解压缩,就会出现一个prometheus的二进制文件,这样就安装完成了,无需其他安装部署步骤。

prometheus修改时间间隔 prometheus 动态配置_prometheus修改时间间隔

二:配置中心详解

编辑prometheus的安装目录中的prometheus.yml文件

[root@webserver2 prometheus]# pwd 
/alidata/prometheus/prometheus 
[root@webserver2 prometheus]# ll prometheus.yml 
-rw-r--r-- 1 root root 2872 Oct 16 14:11 prometheus.yml

配置文件的模块介绍:
global:全局配置
alerting:告警配置
rule_files:告警规则
scrape_configs:配置数据源,称为target,每个target用job_name命名
prometheus server 配置文件如下:

[root@webserver2 prometheus]# cat prometheus.yml
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
alerting:
  alertmanagers:
  - static_configs:
    - targets: ['127.0.0.1:9093']
rule_files:
    - "/alidata/prometheus/prometheus/rules/disk_rule.yml"
    - "/alidata/prometheus/prometheus/rules/cpu_rule.yml"
    - "/alidata/prometheus/prometheus/rules/memory_rule.yml"
    - "/alidata/prometheus/prometheus/rules/node_up.yml"
    - "/alidata/prometheus/prometheus/rules/system_load.yml"
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'pushgateway'
    scrape_interval: 30s
    honor_labels: true  #加上此配置exporter节点上传数据中的一些标签将不会被pushgateway节点的相同标签覆盖
    static_configs:
    - targets: ['34.193.83.103:9091']
      labels:
        instance: pushgateway
# php组服务
  - job_name: 'php_node_exporter'
    static_configs:
    - targets: ['172.16.100.126:9098']
      labels:
        name: webserver01
    - targets: ['172.16.100.127:9098']
      labels:
        name: webserver02
#go组服务
  - job_name: 'go_node_exporter'
    static_configs:
    - targets: ['172.16.254.173:9098']
      labels:
        name: go1
    - targets: ['172.16.254.172:9098']
      labels:
        name: go2
#japan_node_exporter
  - job_name: 'japan_node_exporter'
    static_configs:
#node_ecporter
  - job_name: 'node_exporter'
    static_configs:
    - targets: ['127.0.0.1:9100']
      labels:
        name: webserver2
    - targets: ['172.16.149.48:9100']
      labels:
        name: sonar
#admin_node_exporter
  - job_name: 'admin_node_exporter'
    static_configs:
    - targets: ['172.16.100.120:9098']
      labels:
        name: H2

默认配置补充:

#有一些默认值,不配置就内容就选择使用默认值,是可以进行更改的,以下是单个job的默认值 
# 任务名 
job_name: <job_name> 
# 抓取间隔,默认为对应全局配置 
[ scrape_interval: <duration> | default = <global_config.scrape_interval> ] 
# 抓取超时时间,默认为对应全局配置 
[ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ] 
# 协议,默认为 http,可选 https 
[ scheme: <scheme> | default = http ] 
# 抓取地址的路径,默认为 /metrics 
[ metrics_path: <path> | default = /metrics ] 
# 抓取地址的参数 
params: 
  [ <string>: [<string>, ...] ] 
# 是否尊重抓取回来的标签,默认为 false 
[ honor_labels: <boolean> | default = false ] 
# 静态目标配置 
static_configs: 
  [ - <static_config> ... ] 
# 单次抓取的采样值个数限制,默认为 0,表示没有限制 
[ sample_limit: <int> | default = 0 ]

三:启动

服务器启动的默认端口是:9090 可以通过--web.listen-address="0.0.0.0:9090" 进行更改

# 切换到prometheus的安装目录 
[root@webserver2 ~]# cd /alidata/prometheus/prometheus/ 
# 使用nohup 进行后台启动 
[root@webserver2 prometheus]# nohup ./prometheus --config.file=./prometheus.yml --storage.tsdb.retention.time=365d & 
#正常的情况下,可以看到以下日志内容: 
level=info ts=2019-10-18T08:45:45.084Z caller=main.go:654 msg="Starting TSDB ..." 
level=info ts=2019-10-18T08:45:45.084Z caller=web.go:448 component=web msg="Start listening for connections" address=0.0.0.0:9090 
level=info ts=2019-10-18T08:45:45.204Z caller=head.go:509 component=tsdb msg="replaying WAL, this may take awhile" 
level=info ts=2019-10-18T08:45:45.258Z caller=head.go:533 component=tsdb msg="WAL checkpoint loaded"
level=info ts=2019-10-18T08:45:50.020Z caller=main.go:669 fs_type=EXT4_SUPER_MAGIC level=info ts=2019-10-18T08:45:50.020Z caller=main.go:670 msg="TSDB started" 
level=info ts=2019-10-18T08:45:50.020Z caller=main.go:740 msg="Loading configuration file" filename=./prometheus.yml 
level=info ts=2019-10-18T08:45:50.208Z caller=main.go:768 msg="Completed loading of configuration file" filename=./prometheus.yml 
level=info ts=2019-10-18T08:45:50.208Z caller=main.go:623 msg="Server is ready to receive web requests."

启动的常用参数

--storage.tsdb.path="data/" 
#修改本地数据存储的路径,默认存储目录是prometheus的当前目录 
--storage.tsdb.retention.time=15d 
# 修改prometheus的数据保留时间,默认保留15天数据 
--web.listen-address="0.0.0.0:9090" 
# 修改启动prometheus的监听端口,默认为9090 
在prometheus的安装目录中执行 ./prometheus -h 查看所支持的启动参数命令

四:web UI介绍

服务启动完成之后,可通过http://localhost:9090访问Prometheus的UI界面,这个是prometheus的自带的UI界面,可用作数据查询,在当前界面使用promsql的语法进行数据查询,promsql的查询方式请参考文章《Prometheus-PromSQL 语法介绍和使用方法》

prometheus修改时间间隔 prometheus 动态配置_数据_02

PromSQl的查询举例:

查询cpu的使用率

100 - (avg(irate(node_cpu_seconds_total{instance=~"$node",mode="idle"}[5m])) * 100)

prometheus修改时间间隔 prometheus 动态配置_数据_03

查询内存使用率:

100 - (node_memory_MemFree_bytes{instance=~"$node"} + node_memory_Cached_bytes{instance=~"$node"} + node_memory_Buffers_bytes{instance=~"$node"}) / (node_memory_MemTotal_bytes{instance=~"$node"})* 100

prometheus修改时间间隔 prometheus 动态配置_prometheus修改时间间隔_04


常用的界面操作介绍:

通过9090端口进入界面

Status -> targets 进入界面查看被监控节点的运行状态

Status -> rules 进入界面查看报警规则

Status -> configuration 进入界面查看prometheus的配置中心 prometheus.yml 文件中的内容