目前我正在使用 Prometheus 并取得了良好的结果,我面临的困难是,如果服务重新启动,我的整个旧数据将丢失。有什么办法可以将 Prometheus 数据永久存储在 mysql 或 PostgreSQL 等数据库中吗?
您无法将 Prometheus 数据直接写入关系数据库(或任何与此相关的数据库)。你有两个选择:
信息可以在 Prometheus 文档中找到。
https://blog.timescale.com/prometheus-ha-postgresql-8de68d19b6f5
MySQL 和 PostgreSQL 等传统数据库并未针对 Prometheus 收集的时间序列数据进行优化。有更好的解决方案,它们需要更少的存储空间,并且插入和选择的工作速度更快。
Prometheus 支持远程存储。启用后,它将所有新数据存储在本地存储和远程存储中。远程存储数据库有多种选择,需要进行各种权衡。我建议尝试VictoriaMetrics。它原生支持 Prometheus 的查询语言 PromQL,因此可以轻松用作 Grafana 中的 Prometheus 数据源。
InfluxDB 将是另一种选择:
https://www.influxdata.com/blog/influxdb-now-supports-prometheus-remote-read-write-natively/
只需在 Prometheus 配置中配置remote_write和remote_read,就可以开始了:
remote_write:
- url: 'http://{YOUR_INFLUX-DB}:{YOUR_INFLUX-DB_PORT}/api/v1/prom/write?db=metrics'
remote_read:
- url: 'http://{YOUR_INFLUX-DB}:{YOUR_INFLUX-DB_PORT}/api/v1/prom/read?db=metrics'
吊带也可以帮助解决这个问题:
https://blog.slingdata.io/export-data-from-prometheus-into-any-database
这是一个配置示例:
# replication.yaml
source: prometheus
target: postgres
defaults:
object: prometheus.{stream_name}
mode: full-refresh
streams:
gc_duration_by_job:
sql: |
sum(go_gc_duration_seconds)
by (job, instance, quantile)
# {"start": "now-2M", "end": "now-1d", "step": "1d"}
# incremental load, last 2 days of data, hourly
go_memstats_alloc_bytes_total:
sql: |
sum(go_memstats_alloc_bytes_total)
by (job, instance, quantile)
# {"start": "now-2d"}
primary_key: [timestamp, job, instance, quantile]
update_key: timestamp
mode: incremental