VictoriaMetrics 中文教程(03)如何配置 Prometheus 使其把数据远程写入 VictoriaMetrics

快猫运营团队 2024-10-19 12:05:42

VictoriaMetrics

VictoriaMetrics 中文教程系列文章:

Prometheus 简介

Prometheus 是一款开源的监控系统和时间序列数据库,由 SoundCloud 开发并开源。Prometheus 以多维数据模型和强大的查询语言 PromQL 而著称,可以灵活地处理多种类型的监控数据。Prometheus 随着 Kubernetes 而流行,成为云原生监控的事实标准。

但是,Prometheus 的存储是单点,所以生产环境使用时,需要考虑数据的高可用性和持久化。这时候,就需要使用 VictoriaMetrics 来替代 Prometheus 的存储,使得 Prometheus 的数据可以远程写入 VictoriaMetrics,从而实现数据的高可用和持久化。

此时,Prometheus 的角色就相当于一个采集器,采集了数据之后推给 VictoriaMetrics,VictoriaMetrics 负责存储和查询数据。实际上,高版本的 Prometheus 已经支持 agent mode 模式,对自身是有很好的定位认知的。

配置 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 本地数据存储多久,就看 Prometheus 的启动参数 --storage.tsdb.retention.time 指定的多久。如果默认没有指定,那么默认是 15 天。

如果你有多套 Prometheus 实例,都需要远程写入 VictoriaMetrics,那么我们建议你在 Prometheus 的配置文件中添加如下内容:

global:
  external_labels:
    datacenter: dc-123

这会使得 Prometheus 在转发数据给远端存储之前,自动给每个监控数据点添加 datacenter=dc-123 的标签,这样在 VictoriaMetrics 中就可以通过 datacenter 这个标签来区分不同的 Prometheus 实例啦。

另外,对于高负载的 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_sendcapacity 的值。脑子里要记住,这俩值是有关联的,阅读这里可以获知更多 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 的协议,通过查看 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 信息写入后端存储,但是 remote write 机制也有好处,典型的就是对网络 ACL 更为友好,如果你们公司有几个网络特殊区域,这些区域不能接收外部调用,但是可以调用外部服务,那就可以在每个特殊区域分别部署 Prometheus 采集监控数据,然后通过 remote write 方式推数据到中心机房的 VictoriaMetrics 中,这样就可以实现监控数据的集中存储和查询。

vmagent 也值得关注

在整个 remote write 的过程中,Prometheus 就相当于一个 agent,只是负责数据采集,然后把数据推给后端存储就不管了。而实际上 VictoriaMetrics 提供了 vmagent 组件专门用于做数据采集,vmagent 相比 Prometheus,更加轻量级,有兴趣的朋友可以试试。

总结

通过本文,我们了解了如何配置 Prometheus 使其把数据远程写入 VictoriaMetrics,这样就可以实现 Prometheus 的数据高可用和持久化。同时,我们也了解了如何给 Prometheus 添加标签,以及如何调整 remote write 的配置参数。希望对你有所帮助。

快猫星云 联系方式 快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云
OpenSource
开源版
Flashcat
Flashcat