Prometheus官方文档 仅支持二进制部署或者 docker 容器部署。任意一种都行,我这里以二进制为例进行部署。
二进制方式部署
在 Prometheus 官网下载对应版本和 CPU 架构的二进制包
$ wget -O /opt/prometheus.tar.gz https://github.com/prometheus/prometheus/releases/download/v2.36.0/prometheus-2.36.0.linux-amd64.tar.gz
$ mkdir /opt/prometheus && cd /opt
$ tar zxf prometheus.tar.gz -C ./prometheus --strip-components 1
$ rm -f prometheus.tar.gz
工具介绍
解压后的包内有 2 个可执行文件 prometheus
和 promtool
$ ls prometheus
console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
promtool
用来检查 prometheus.yml 配置文件的语法,通过 –help 可以查看使用方法
prometheus
用来启动 prometheus 进程的命令
prometheus -h 查看常用参数:
--config.file="prometheus.yml" # 指定配置文件
--web.listen-address="0.0.0.0:9090" # 指定监听地址和端口号
--log.level=info # 日志级别
--alertmanager.timeout=10s # 与告警组件之间的超时时间
--storage.tsdb.path="data/" # 数据目录
--storage.tsdb.retention.time=15d # 数据保存时间,默认15天
启动 Prometheus
# 直接使用该命令即可启动
$ /opt/prometheus/prometheus
使用命令直接启动是前台方式在运行,一旦退出终端,prometheus 进程也将结束。
systemd 管理 Prometheus
$ cat > /usr/lib/systemd/system/prometheus.service << EOF
[Unit]
Descriptioin=prometheus
[Service]
ExecStart=/opt/monitoring/prometheus/prometheus \
--config.file=/opt/monitoring/prometheus/prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
$ systemctl enable --now prometheus
Docker 部署
使用默认配置文件进行部署
docker run -d -p 9090:9090 prom/prometheus
使用挂载自定义配置文件进行部署,配置文件可以从二进制方式的包中获取
## 挂载文件
docker run -d \
-p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
# /path/to/prometheus.yml 为宿主机本地路径
## 挂载目录
docker run -d \
-p 9090:9090 \
-v /path/to/config:/etc/prometheus \
prom/prometheus
docker 运行官方文档中是没有 -d
参数的,使用前台运行的方式,使用 -d
可以将容器放在后台运行。
Prometheus 配置文件
Prometheus 配置文件 prometheus.yml
分为几个大项:
- global:全局配置
- alerting:告警服务配置
- rule_files:指定告警规则文件的位置
- scrape_configs:被监控端配置
- remote_write:远程读取,使用远程数据库时使用
- remote_read:远程写入,使用远程数据库时使用
# prometheus.yml
global:
scrape_interval: 15s # 采集数据时间间隔
evaluation_interval: 15s # 指定评估告警规则的时间间隔
# scrape_timeout: 10s # 采集数据超过10s无响应则为超时(默认10s)
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093 # 指定告警组件(Alertmanager)的ip和端口
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
scrape_configs:
- job_name: "prometheus" # 分组名称
# metrics_path: /metrics # 指定监控服务获取指标的API URI,默认为 /metrics
# scheme: http # 默认的协议为 http
static_configs:
- targets: ["localhost:9090"] # 指定监控服务的URL(ip:port)
以上是 Prometheus 的默认配置文件中的配置
扩展配置
#### 服务发现 ####
consul_sd_configs: # 基于 consul 的服务发现
dns_sd_configs: # 基于 DNS 的服务发现
file_sd_configs: # 基于文件的服务发现
kubernetes_sd_configs: # 基于 k8s 的服务发现
#################
scrape_configs:
- job_name: "prometheus" # 分组名称
# scrape_interval: 15s # 可覆盖全局配置
# scrape_timeout: 10s # 可覆盖全局配置
# honor_labels: true|false # 默认false
metrics_path: /metrics # 指定监控服务获取指标的API URI,默认为 /metrics
# params: # 向采集端获取指标的路径传参
scheme: http # 默认的协议为 http
# tls_config: # 如果 scheme 为 https,则需要指定ssl文件位置
#### 采集端 API 的认证方式 ####
basic_auth: # 如果采集端API需要进行用户密码的认证需要配置此项
username:
password:
password_file:
bearer_token: # token值
bearer_token_file: # 访问采集端API的token文件路径
##############################
# proxy_url: # Prometheus 访问采集端 API 需要通过代理访问时配置
static_configs:
- targets: ["localhost:9090"] # 指定监控服务的URL(ip:port)
- relabel_configs: # 重写标签
- metrics_relabel_configs: # 重写标签
sample_limit:
Prometheus 指标数据模型
通过部署好的 Prometheus Server,可以访问 Promethues 的 http://ip:9090/metrics
,所有使用 Prometheus 监控的应用都必须以这种数据模型保留监控指标。如下所示。
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 3.9666e-05
go_gc_duration_seconds{quantile="0.25"} 5.7221e-05
go_gc_duration_seconds{quantile="0.5"} 9.4388e-05
go_gc_duration_seconds{quantile="0.75"} 0.000157097
go_gc_duration_seconds{quantile="1"} 0.001566062
go_gc_duration_seconds_sum 0.039801346
go_gc_duration_seconds_count 262
...
- Prometheus 将所有数据存储为时间序列;
- 具有相同度量名称以及标签属于同一个指标;如下所示go_gc_duration_seconds 为度量名称,quantile 为标签,每个标签的值不同
go_gc_duration_seconds{quantile="0"} 3.9666e-05
go_gc_duration_seconds{quantile="0.25"} 5.7221e-05
go_gc_duration_seconds{quantile="0.5"} 9.4388e-05
go_gc_duration_seconds{quantile="0.75"} 0.000157097
go_gc_duration_seconds{quantile="1"} 0.001566062
- 每个时间序列都由一个度量名称和一组键值对(标签)唯一标识;以上的每一条都是一个时间序列