prometheus数据持久化 docker部署
创始人
2025-06-01 21:10:15
0

https://segmentfault.com/a/1190000015710814

prometheus修改配置不需要停掉,只要修改yml之后用docker restart重启

prometheus存储方式

prometheus提供了本地存储,即tsdb时序数据库。
本地存储的优势就是运维简单,缺点就是无法海量的metrics持久化和数据存在丢失的风险,我们在实际使用过程中,出现过几次wal文件损坏,无法再写入的问题。
prometheus没有自己实现集群存储,而是提供了远程读写的接口,让用户自己选择合适的时序数据库来实现prometheus的扩展性。
prometheus通过下面两种方式来实现与其他的远端存储系统对接
Prometheus 按照标准的格式将metrics写到远端存储
prometheus 按照标准格式从远端的url来读取metrics
参考:https://prometheus.io/docs/prometheus/latest/storage/

持久化选型

AppOptics: write
Chronix: write
Cortex: read and write *
CrateDB: read and write *
Elasticsearch: write
Gnocchi: write
Graphite: write
InfluxDB: read and write *
OpenTSDB: write
PostgreSQL/TimescaleDB: read and write *
SignalFx: write
clickhouse: read and write *

选择TimescaleDB,配合Promscale

InfluxDB

概念

influxDB中的名词 传统数据库中的概念
database 数据库
measurement 数据库中的表
points 表里面的一行数据

命令

  1. 进入命令行
docker exec -it myinfluxdb influx
influx -precision rfc3339
  1. 显示数据库
show databases
  1. 使用指定数据库
use prometheus
  1. 显示所有表
SHOW MEASUREMENTS
  1. 查询
select * from test limit 2 offset 2

Promscale + PostgreSQL/TimescaleDB

Promscale:https://github.com/timescale/promscale

部署

docker-compose部署 Promscale、TimescaleDB、Prometheus

https://github.com/timescale/promscale/blob/master/docs/docker.md

  1. /opt/dcprometheus/prometheus.yml
# my global config
global:scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.# scrape_timeout is set to the global default (10s).# Alertmanager configuration
alerting:alertmanagers:- static_configs:- targets:# - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:# - "first_rules.yml"# - "second_rules.yml"# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:# The job name is added as a label `job=` to any timeseries scraped from this config.- job_name: 'prometheus'# metrics_path defaults to '/metrics'# scheme defaults to 'http'.## MUSTstatic_configs:- targets: ['192.168.200.137:9090']- job_name: 'kong'static_configs:- targets: ['192.168.200.137:8091']remote_write:- url: "http://192.168.200.137:9201/write"write_relabel_configs:- action: keepsource_labels: [__name__]regex: kong_http_con_status|kong_con_bandwidth|kong_http_log_status
remote_read:- url: "http://192.168.200.137:9201/read"read_recent: true
  1. docker-compose.yml
version: '3'services:db:image: timescaledev/timescaledb-ha:pg12-latestcontainer_name: sigma-dc-tsdbnetworks:- prometheus-netports:- 5433:5432/tcpenvironment:POSTGRES_PASSWORD: passwordPOSTGRES_USER: postgresprometheus:image: prom/prometheus:v2.13.1container_name: dc-prometheusnetworks:- prometheus-netports:- "9090:9090"volumes:- /opt/dcprometheus/prometheus.yml:/etc/prometheus/prometheus.yml- /etc/localtime:/etc/localtimerestart: alwayspromscale:image: timescale/promscale:0.2.0container_name: dc-promscalenetworks:- prometheus-netports:- 9201:9201/tcpbuild:context: .restart: on-failuredepends_on:- db- prometheusenvironment:PROMSCALE_DB_CONNECT_RETRIES: 10PROMSCALE_WEB_TELEMETRY_PATH: /metrics-textPROMSCALE_DB_URI: postgres://postgres:password@db:5432/postgres?sslmode=allownetworks:prometheus-net:driver: bridge
  1. docker-compose up -d 启动
    注意:可能会出现promscale重启的现象。因为depend on只限制了启动时间先后,并非在依赖容器ready后再创建新容器。
    参考:https://segmentfault.com/a/1190000021504344
    https://docs.docker.com/compose/startup-order/

prometheus.yml文件对远程读写的配置

https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write

  1. read_recent: true
# Remote read configuration (TSDB).
remote_read:- url: "http://ts-xxxxxxxxxxxx.hitsdb.rds.aliyuncs.com:3242/api/prom_read"read_recent: true

上面的配置中,’read_recent: true’ 表示近期数据也要读远程存储。
因为Prometheus近期数据无论如何都是要读本地存储的,如果开启这个标志位,Prometheus会把本地和远程的数据进行Merge。开启这个标志位,可以方便验证读取远程TSDB是否正常工作。
如果正式在生产环境,可以根据实际情况将’read_recent: true’去掉,可提升Prometheus的查询性能。
参考:https://help.aliyun.com/document_detail/114508.html

  1. write_relabel_configs
    注意,大部分以label为粒度筛选
    参考:https://studygolang.com/articles/13522?fr=sidebar
    https://blog.csdn.net/liangkiller/article/details/105758857

target重启导致的数据不连续问题

  1. target重启时间,prometheus应该显示0 或 原数据?
  2. target重启完成之后,数据应该与重启之前相同

相关问题:

  1. Kubernetes节点更换后的时间序列混乱
    https://github.com/prometheus/prometheus/issues/7944

postgresql操作

http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql.html
https://www.runoob.com/postgresql/postgresql-create-table.html

  1. 容器进入
psql
  1. 查看全部数据库
\l
  1. 进入某个数据库
\c postgres
  1. 查看数据库的全部表
\d

相关内容

热门资讯

哪吒汽车,注定掉队 哪吒汽车,... 定焦One(dingjiaoone)原创特约作者 | 胡锟编辑 | 魏佳从登顶新势力销量冠军,到几乎...
牛市早报|端午假期预计全社会跨... 【市场数据】截至5月30日收盘,上证综指跌0.47%,报3347.49点;科创50指数跌0.94%,...
黄酒真的雄起了? 黄酒真的雄起... 斑马消费 杨伟2025年A股酒水板块“冰火两重天”,白酒承压,啤酒失速,黄酒却异军突起!Wind 5...
一台不到600元,魅族新机大火... 对于整个手机市场来说,华米OV等国产手机的市场大战已经日渐稳定,各家手机企业都处于平淡化的状态,就在...
港股稳定币概念暴涨!刘煜辉:人... 6月2日,在港股市场上,数字货币概念股集体拉升,连连数字盘中一度上涨80%,移卡一度涨近50%,欧科...
又现百万罚单 消金合作机构管理... 北京商报讯(记者 岳品瑜 董晗萱)消费金融机构的一张新罚单,又指向合作业务管理。6月2日,北京商报记...
国际金价重返高位 炒金是否卷土... 6月2日,国际金价重返3300美元/盎司高位,截至北京商报记者发稿,金价涨幅超过2%,盘中突破336...
股价翻倍基金霸屏 创新药否极泰... 证券时报记者 裴利瑞今年以来,中国创新药行业正经历了一场前所未有的价值重估,而且在近期呈现加速趋势。...
5000亿人民币新型融资政策工... 内容提要:中国计划推出5000亿元新型政策性金融工具,重点投向新基建与消费领域,以对冲出口压力。但企...
美国5月ISM制造业PMI连续... 6月2日周一,ISM公布的数据显示,美国5月ISM制造业活动连续三个月萎缩,在关税上调的背景下,进口...
国内油价或现年内第四涨 加满一...   中新经纬6月3日电 (万可义)国内成品油新一轮调价窗口将于6月3日24时开启。综合机构观点,国内...
道指三连阳!美股6月开门红,黄... *三大股指上扬,纳指涨近0.7%;*中长期美债收益率走高,基准10年期美债报4.61%;*受特朗普言...
结构性行情或延续,券商建议6月... 经历5月冲高回落后,A股6月行情即将拉开帷幕。展望后市表现,当前机构多数持相对谨慎态度,认为市场短期...
马斯克卸任DOGE后旗下公司迎... 马斯克重返其商业帝国、远离政治后,其旗下公司迅速开启一系列融资,包括xAI正在启动一项3亿美元的股份...
媒体称美国秘密提案允许低浓缩铀... 媒体报道,美国对伊核谈判政策出现180度转变,美国政府上周六向伊朗提出一份新的核协议提案,其中允许伊...
一笔漂亮的退出:93亿卖始祖鸟... (图片由豆包AI生成) 消费赛道又一明星公司被减持了。5月29日,据彭博社消息,始祖鸟母公司亚玛芬体...
速腾聚创一季度毛利同比增七成 ...   速腾聚创第二代灵巧手Papert 2.0。  5月30日,速腾聚创发布2025年第一季度财报。据...
“消费+科技”双轮驱动,港股市... 港股IPO市场正经历显著回暖,优质资产供给逐步改善,市场流动性增强,吸引了大量资金关注。Wind资讯...
从一面之恩到千亿帝国CEO,安... 近日,吉利汽车管理层大调整引发行业聚焦。在吉利一季度财报发布的当天,吉利控股集团宣布重大人事调整:极...
A股公司密集更名折射四大现象 ... 据不完全统计,2025年以来,超60家A股公司完成或启动名称变更工作,其中超30家公司涉及证券简称调...