prometheus介绍与安装

305 阅读2分钟

这是我参与8月更文挑战的第8天,活动详情查看:8月更文挑战

1.prometheus

1.1什么是prometheus?

     Prometheus 是由 SoundCloud 开源监控告警解决方案,从 2012 年开始编写代码,再到 2015 年 github 上开源以来,已经吸引了 31.5k+ 关注,以及很多大公司的使用;2016 年 Prometheus 成为继 k8s 后,第二名 CNCF(Cloud Native Computing Foundation) 成员。

1.2主要功能

  • 多维 数据模型(时序由 metric 名字和 k/v 的 labels 构成)。
  • 灵活的查询语句(PromQL)。
  • 无依赖存储,支持 local 和 remote 不同模型。
  • 采用 http 协议,使用 pull 模式,拉取数据,简单易懂。
  • 监控目标,可以采用服务发现或静态配置的方式。
  • 支持多种统计数据模型,图形化友好。

1.3磁盘容量估算

  • 平均而言,普罗米修斯每个样本仅使用大约1-2个字节
  • 1GB = 1073741824字节 ≈ 536870912条样本数据(每个样本2个字节)

1.4基础架构

image-prometheus.png

下载后解压 prometheus.io/download/

mac: prometheus-2.29.0-rc.0.darwin-amd64.tar.gz

tar xvfz prometheus-.tar.gz cd prometheus-

xx_exporter 提供了监控各应用端
mysqld_exporter 监控mysql
node_exporter 监控linux机器
consul_exporter 基于consul注册中心的监控

修改配置文件prometheus.yml job_name: 作业名称 scrape_interval: 间隔 targets: 监控的目标机器

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# 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'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

启动prometheus

二进制数据保存在./data文件下

Start Prometheus. # By default, Prometheus stores its database in ./data (flag --storage.tsdb.path).

./prometheus --config.file=prometheus.yml

可以给监控job指定labels

让我们将所有三个端点分组到一个名为node. 我们将想象前两个端点是生产目标,而第三个端点代表金丝雀实例。为了在 Prometheus 中对此进行建模,我们可以向单个作业添加多组端点,为每组目标添加额外的标签。在本例中,我们将group="production"标签添加到第一组目标,同时添加group="canary"到第二组。

scrape_configs:
  - job_name:       'node'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:8080', 'localhost:8081']
        labels:
          group: 'production'

      - targets: ['localhost:8082']
        labels:
          group: 'canary'

prometheus 再带了一些简单的视图

访问 http://localhost:9090/graph

image-pro2.png