Prometheus 是一个开源的系统监控和警报工具包。

特点

  • 多维数据模型,时间序列由 metric 名字和 K/V 标签标识
  • 灵活的查询语言(PromQL)
  • 单机模式,不依赖分布式存储
  • 基于 HTTP 采用 pull 方式收集数据
  • 支持 push 数据到中间件(pushgateway)
  • 通过服务发现或静态配置发现目标
  • 多种图表和仪表盘

注意:由于数据采集可能会有丢失,所以 Prometheus 不适用对采集数据要 100% 准确的情形。但如果用于记录时间序列数据,Prometheus 具有很大的查询优势,此外,Prometheus 适用于微服务的体系架构。

组件

Prometheus 生态系统由多个组件构成,其中多是可选的,根据具体情况选择

  • Prometheus server - 收集和存储时间序列数据
  • Client Library: 客户端库,为需要监控的服务生成相应的 metrics 并暴露给 Prometheus server。当 Prometheus server 来 pull 时,直接返回实时状态的 metrics。
  • pushgateway - 对于短暂运行的任务,负责接收和缓存时间序列数据,同时也是一个数据源
  • exporter - 各种专用 exporter,面向硬件、存储、数据库、HTTP 服务等
  • alertmanager - 处理报警
  • webUI 等,其他各种支持的工具

安装

Prometheus 是 Golang 写的,我们可以直接下载二进制文件直接解压执行即可。

wget -c https://github.com/prometheus/prometheus/releases/download/v2.9.2/prometheus-2.9.2.linux-amd64.tar.gz

wget -c  https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz

其中,prometheus-2.9.2.linux-amd64.tar.gz 就是服务端程序压缩包,node_exporter-0.17.0.linux-amd64.tar.gz 是主机监控指标的包。

部署

客户端

node_exporter 有默认的配置,可以直接使用 nohup 运行即可,会监听 9100 的端口

nohup  ./node_exporter &

服务端

prometheus 程序默认是找当前目录下的 prometheus.yml 配置文件,也可以使用–config.file=“prometheus.yml"指定配置文件

nohup ./prometheus   --config.file="prometheus.yml"    &

配置文件

cat prometheus.yml
# my global config
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.
  # scrape_timeout is set to the global default (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:
  # 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']

默认配置文件里面,使用了静态配置的方式,监控 Prometheus 的指标。

添加一个 node

添加以下内容


  + job_name: 'node_export'

    static_configs:

    - targets: ['192.168.100.171:9100']

重载配置

kill -HUP [prometheus_pid]

给 Prometheus 发现重载配置的信号,就会开始收集 node 的指标。

webUI

Prometheus 提供了一个 webUI 可以查询对应的参数的值,,也有简单的图形趋势展示

http://[ip]:9090

但是我们一般会结合 grafana 使用,绘制出比较美观漂亮的界面。