性能|入门Prometheus

175 阅读3分钟

Prometheus 是一个开源的监控和警报系统,用于收集、存储和查询应用程序和系统的指标数据。它采用了一种特定的架构,如下所示:

image.png

Prometheus Server(Prometheus 服务器):Prometheus Server 是整个系统的核心组件。它负责从各种目标(例如应用程序、服务、操作系统)中采集指标数据,并存储在本地的时间序列数据库中。Prometheus Server 还负责数据的查询和分析,以支持监控数据的可视化和警报生成。

Exporters(导出器):Exporters 是用于从不同的应用程序、服务和系统中采集指标数据的组件。它们充当桥梁,将各种格式的指标数据转换为 Prometheus 可以理解的格式。Prometheus 提供了一些常用的 Exporters,同时也支持用户自定义的 Exporters。

Push Gateway(推送网关):Push Gateway 是一种中间组件,用于接收来自短暂作业(如批处理任务、临时任务)的指标数据。这些作业通常无法被 Prometheus Server 直接抓取,因此它们可以将指标数据推送到 Push Gateway,然后由 Prometheus Server 从 Push Gateway 中获取数据。

Alertmanager(警报管理器):Alertmanager 负责接收来自 Prometheus Server 的警报规则生成的警报通知,并根据配置的规则进行分组、去重、静默和发送通知。它支持多种通知渠道,如电子邮件、Slack、PagerDuty 等。

Grafana:虽然 Grafana 不是 Prometheus 的一部分,但它是一种常用的监控数据可视化工具,与 Prometheus 集成紧密。Grafana 可以连接到 Prometheus Server,从中查询和展示监控数据,并创建仪表板和报告。

知道了 Prometheus 架构后,我们可以在本地启动,具体操作如下

下载和安装

Prometheus安装包地址

选择操作系统和处理器架构,比如 mac i5 处理器选择 darwin操作系统 amd64 处理器架构,如下图所示:

image.png

如果是使用 docker ,可以不用考虑操作系统和处理器架构

tar zvxf prometheus-2.45.0.darwin-amd64.tar.gz
cd prometheus-2.45.0.darwin-amd64

配置抓取目标

prometheus.yml 用于定义 Prometheus 的抓取目标(即要监控的应用程序)和其他配置选项。编辑该文件,将您要监控的目标添加到 scrape_configs 部分,样例如下所示:

# 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
# Prometheus 的警报功能,配置警报规则并定义警报接收者。当指标数据满足特定条件时,Prometheus 将触发警报,并将通知发送给配置的接收者。
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"]
  # 采集node exporter监控数据
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']   

启动 promethehus

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

执行结果如下所示:

image.png

在浏览器中打开 http://localhost:9090,即可访问 Prometheus 的 Web 用户界面。在该界面上,您可以执行查询、查看指标数据和配置警报规则等。可以使用 PromQL 查询语言编写查询,以检索和可视化应用程序的指标数据。您可以创建仪表板、图形和警报规则,以满足您的监控需求,如下所示:

image.png