为什么要使用远端存储

Prometheus 默认使用本地存储,也就是 tsdb 时序数据库来存储数据。

本地存储有两个关键参数:

  • storage.tsdb.path : 数据保存路径
  • storage.tsdb.retention : 数据保留时间

本地存储操作简单,且 prometheus 的数据压缩能力很强,可以很大地节省磁盘空间。但是依然会受到单节点存储的限制,无法持久化海量的metrics数据。
为了解决这个问题,prometheus 提供了远程读写的接口,让用户自己选择合适的远端存储。
本文讲解的就是将数据存储到 elasticsearch。

部署 prometheusbeat

Prometheus 按照标准的格式将 metrics 写到远端存储 elasticsearch,需要依靠 prometheusbeat。当 prometheus 收集到数据时,会发送数据到 prometheusbeat,由它来转存到 elasticsearch。

本文使用 docker-compose 来部署 prometheusbeat

拉取镜像

docker pull infonova/prometheusbeat

编写 prometheusbeat.yml

此处 prometheusbeat.yml 放在 /data/docker-compose/beat 目录中。

在此文件中设置 prometheusbeat 接收服务的地址,以及 elasticsearch 的地址。

prometheusbeat:
  # prometheusbeat 服务的端口。默认为8080
  listen: ":8080"
  # Context path. Defaults to /prometheus
  context: "/prometheus"
output.elasticsearch:
  # elasticsearch 的地址
  hosts: ["127.0.0.1:9200"]

编写 docker-compose.yml

version: '3'
services:
  beat:
    image: infonova/prometheusbeat
    ports:
      - 8080:8080
    volumes:
      - /data/docker-compose/beat/prometheusbeat.yml:/prometheusbeat.yml
      - /etc/localtime:/etc/localtime

启动 prometheusbeat

docker-compose up -d --build beat

修改 prometheus.yml

prometheus 通过 remote_write 来实现远端存储,所以在 prometheus.yml 配置文件中增加 remote_write 参数。

详细配置

remote_write:
  - url: "http://10.68.4.66:9300/prometheus"
    write_relabel_configs:
    - source_labels: [__name__]
      action: keep
      regex: container_cpu_usage_seconds_total|container_memory_rss

    - source_labels: [namespace]
      action: keep
      regex: training
      
    - source_labels: [container]
      action: drop
      regex: POD

配置说明

url:prometheusbeat 的服务地址
write_relabel_configs:这个很有用,用于过滤数据,可以设置多个过滤条件,同时满足这些条件的数据才会被存到远端存储
  - source_labels:根据哪个字段进行过滤
  - regex:值过滤的规则
  - action:keep(保留)/drop(丢弃)

所以,上面配置文件中的配置意思就是:prometheus 收到数据后,远程发送给 prometheusbeat,并且只发送 name 为 container_cpu_usage_seconds_total 或者 container_memory_rss,并且 labels.namespace 为 training,且 labels.container 不等于 'POD' 的数据!

修改完之后,启动 prometheus。之后当收集到数据时,就能成功保存到 elasticsearch 了,首次保存时会自动创建索引:prometheusbeat-7.3.1,数据都存在此索引中。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐