
VictoriaMetrics 中文教程系列文章:
核心摘要
- Prometheus 可以通过
remote_write把采集到的监控数据写入 VictoriaMetrics。 - 配置 remote write 后,Prometheus 仍会写本地 TSDB;本地保留时间由
--storage.tsdb.retention.time控制,未指定时默认 15 天。 - 多套 Prometheus 写入同一个 VictoriaMetrics 时,建议通过
external_labels给数据加区分标签。 - 高负载 Prometheus 可以调整
queue_config,但 remote write 可能增加 Prometheus 内存使用。 - Prometheus remote write 不会把 metric 的 help 信息写入后端存储,因此在 VictoriaMetrics 中看不到 help 信息是正常现象。
Prometheus 简介
Prometheus 是一款开源监控系统和时间序列数据库,由 SoundCloud 开发并开源。Prometheus 以多维数据模型和强大的查询语言 PromQL 著称,可以灵活处理多种类型的监控数据。随着 Kubernetes 流行,Prometheus 也成为云原生监控的事实标准。
但 Prometheus 的存储是单点。生产环境使用时,需要考虑数据的高可用和持久化。这时可以使用 VictoriaMetrics 替代 Prometheus 的存储,让 Prometheus 把数据远程写入 VictoriaMetrics。
在这个架构里,Prometheus 的角色更像采集器:负责采集数据,然后推给 VictoriaMetrics;VictoriaMetrics 负责存储和查询。实际上,高版本 Prometheus 已经支持 agent mode,这说明 Prometheus 对自身在采集链路中的定位也越来越清晰。
配置 Prometheus 远程写入 VictoriaMetrics
修改 Prometheus 配置文件,通常位于 /etc/prometheus/prometheus.yml,把下面配置添加进去:
remote_write:
- url: http://<victoriametrics-addr>:8428/api/v1/write
把 <victoriametrics-addr> 替换为自己的 VictoriaMetrics 地址,然后通过下面命令让 Prometheus 重载配置:
kill -HUP `pidof prometheus`
pidof prometheus 用于查看 Prometheus 进程号,kill -HUP 用于让 Prometheus 重载配置。
配置完成后,Prometheus 会在把数据存储到本地的同时,把数据远程写入 VictoriaMetrics。Prometheus 本地数据存储多久,取决于启动参数 --storage.tsdb.retention.time;如果默认没有指定,保留时间是 15 天。
多套 Prometheus 如何区分数据来源
如果有多套 Prometheus 实例都需要远程写入 VictoriaMetrics,建议在 Prometheus 配置中添加 external_labels:
global:
external_labels:
datacenter: dc-123
这样 Prometheus 在转发数据给远端存储前,会自动给每个监控数据点添加 datacenter=dc-123 标签。在 VictoriaMetrics 中,就可以通过 datacenter 标签区分不同 Prometheus 实例。
这个标签设计很重要。否则多个 Prometheus 写入同一个后端时,查询侧很难判断数据来自哪个机房、集群或采集实例。
高负载 Prometheus 的 remote write 参数
对于高负载 Prometheus 实例,例如每秒处理 20 万以上数据点,建议在 Prometheus 配置中添加如下内容:
remote_write:
- url: http://<victoriametrics-addr>:8428/api/v1/write
queue_config:
max_samples_per_send: 10000
capacity: 20000
max_shards: 30
使用 remote write 机制会让 Prometheus 内存大概增加 25%。如果遇到 Prometheus 占用内存太多的问题,可以调低 max_samples_per_send 和 capacity 的值。要记住,这两个值是有关联的,更多配置参数可以阅读 Prometheus remote write 文档。
如果要启用 remote write,强烈建议把 Prometheus 升级到 v2.12.0 以上,因为更早版本存在较多 remote write 相关 bug,v2.12.0 以上已经修复了很多问题。
remote write 采集的数据为何看不到 help 信息
在 Prometheus 配置中添加 remote write 后,你会发现 VictoriaMetrics 中看不到指标 help 信息。原因是 Prometheus remote write 机制不会把指标 help 信息写入后端存储,只会把 metric 数据写入后端存储。
从 Prometheus remote write 使用的 protobuf 数据格式可以看出来:
func Send(WriteRequest)
message WriteRequest {
repeated TimeSeries timeseries = 1;
// Cortex uses this field to determine the source of the write request.
// We reserve it to avoid any compatibility issues.
reserved 2;
// Prometheus uses this field to send metadata, but this is
// omitted from v1 of the spec as it is experimental.
reserved 3;
}
message TimeSeries {
repeated Label labels = 1;
repeated Sample samples = 2;
}
message Label {
string name = 1;
string value = 2;
}
message Sample {
double value = 1;
int64 timestamp = 2;
}
从上面的数据格式可以看出,remote write 只会把 metric 的时间序列数据写入后端存储,所以在 VictoriaMetrics 中看不到 help 信息。
remote write 的好处
虽然 remote write 不会把 help 信息写入后端存储,但它也有明显好处,典型场景是对网络 ACL 更友好。
比如公司有几个网络特殊区域,这些区域不能接收外部调用,但可以调用外部服务。此时可以在每个特殊区域分别部署 Prometheus 采集监控数据,再通过 remote write 把数据推到中心机房的 VictoriaMetrics,实现监控数据的集中存储和查询。
vmagent 也值得关注
在整个 remote write 过程中,Prometheus 相当于一个 agent:负责数据采集,然后把数据推给后端存储。
VictoriaMetrics 提供了专门用于数据采集的 vmagent 组件。相比 Prometheus,vmagent 更轻量。有兴趣的朋友可以试试。
FAQ
Prometheus 配置 remote write 后还会写本地数据吗?
会。Prometheus 会同时写本地 TSDB 和远端 VictoriaMetrics。本地数据保留多久由 --storage.tsdb.retention.time 控制,未指定时默认 15 天。
多个 Prometheus 写入同一个 VictoriaMetrics 时要注意什么?
建议使用 external_labels 给数据加来源标签,例如机房、集群或实例标识。否则后续查询和排障时很难区分数据来源。
为什么 VictoriaMetrics 里没有 Prometheus help 信息?
因为 remote write 协议写入的是时间序列数据,不会把 help 信息作为后端存储数据写进去。这是协议和数据格式决定的,不是 VictoriaMetrics 展示问题。
总结
通过本文,我们了解了如何配置 Prometheus,使其通过 remote write 把数据远程写入 VictoriaMetrics,从而实现监控数据的集中存储、高可用和持久化。实际落地时,重点关注三件事:写入地址是否正确,external_labels 是否能区分数据来源,高负载场景下 queue_config 是否需要调整。
