一、下载 Prometheus
从Prometheus 官网 https://prometheus.io/download/ 下载 Prometheus 安装包 prometheus-2.10.0.linux-amd64.tar.gz
,并解压:
tar xvfz prometheus-*.tar.gz
cd prometheus-*
(注意:启动 Prometheus 之前,需要配置相应的配置文件。)
二、配置 Prometheus 配置文件,使 Prometheus 监控其自身
Prometheus server 除了可以拉取 jobs/exporters 上的 metric,还可以从其他的 Prometheus server(包括自己) 上拉取 metric。虽然在实践中 Prometheus server 收集自身的数据并没有太大用处,但这是一个很好的实例演示,以便我们更好的了解 Prometheus。
添加 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).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'.
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
有关 Prometheus 配置选项的完整规范,可参阅「配置文档」。简单的配置介绍如下:
- 1.Prometheus 是通过【命令行参数】和【配置文件】进行配置的。
其中,【命令行参数】配置不可变的系统参数,如:存储路径、保存在磁盘和内存的数据量等。【配置文件】定义了抓取 jobs 及其 instances 的所有相关内容,以及加载哪些规则文件。 - 2.Prometheus 可在运行时重新加载它的配置。
如果新配置的格式不正确,那么配置不会生效。通过【向 Prometheus 进程发送SIGHUP
】或者【向/-/reload
端点发送 HTTP POST 请求,开启--web.enable-lifecycle
配置参数】 来触发配置 reload。同时任何配置的规则文件也将重新加载。 - 3.执行命令
./prometheus -h
,可查看所有可用的命令行参数。 - 4.通过
--config.file
命令行参数指定加载哪个配置文件。
三、启动 Prometheus
需要使用新的配置文件来启动 Prometheus,需要切换到包含 Prometheus 二进制文件的目录下,并执行命令:
# Start Prometheus.
# By default, Prometheus stores its database in ./data (flag --storage.tsdb.path).
./prometheus --config.file=prometheus.yml
如果是 Windows 系统,那么启动 Prometheus 的命令如下:
start ./prometheus.exe --config.file=prometheus.yml
浏览器访问 Prometheus 主页 http://localhost:9090,启动成功,如下图所示:

Prometheus 的状态信息:http://localhost:9090/status

Prometheus 的监控指标:http://localhost:9090/metrics

四、查看 Prometheus 的监控数据
1)通过 Prometheus 的内置 表达式浏览器
来查看监控数据。
我们以 prometheus_target_interval_length_seconds
指标为例,来进行演示:
(http://localhost:9090/metrics 可获取 Prometheus 监控的所有 metrics。)
- 1.导航到
http://localhost:9090/graph
,选中Console
tab 页。
- 2.在 Expression 输入栏中输入对应的
表达式
,查看指标信息。
prometheus_target_interval_length_seconds
prometheus_target_interval_length_seconds{quantile="0.99"}
count(prometheus_target_interval_length_seconds)
count(prometheus_target_interval_length_seconds{quantile="0.99"})


要了解更多关于 表达式
语法的信息,可参阅「表达式说明文档」。
2)使用「图形界面」来查看监控数据。
- 1.导航到
http://localhost:9090/graph
,选中Graph
tab 页。
- 2.在 Expression 输入栏中输入对应的
表达式
,查看指标信息。
rate(prometheus_tsdb_head_chunks_created_total[1m])

网友评论