本文介绍如何将自建开源 Prometheus 历史数据迁移到阿里云 Prometheus。
执行条件
数据源:自建开源 Prometheus 实例必须使用 TSDB 存储(默认存储引擎)。
阿里云Prometheus实例:需确保目标阿里云 Prometheus 实例已创建且处于运行状态。
网络连通性:自建 Prometheus 服务器需能访问阿里云 Prometheus 的远程写入(Remote Write)端点。
工具依赖:需安装阿里云提供的 promtool 工具。
迁移步骤
步骤1:创建或获取阿里云 Prometheus 实例
登录阿里云控制台。
登录云监控控制台或者Prometheus控制台,选择已有 Prometheus 实例,或者创建 Prometheus 实例。
创建实例时,选择合适的存储时长,需大于自建 Prometheus 的历史时长。
重要请根据您本地的数据量评估,先扩容 Prometheus 实例背后的 SLS MetricStore Shard。
数据量>1GB 扩大到 4 Shard
数据量>10GB 扩大到 16 Shard
数据量>100GB 扩大到 64 Shard
数据量<1TB 扩大到 256 Shard
数据量较大时,请提交工单处理。
获取实例写入端点信息。
单击目标实例名称,进入详情设置页,获取 Remote Write 公网 URL。如果您的自建服务在阿里云上,也可以获取内网 URL。
关于认证,可以分配写数据子账号并为其分配 AliyunPrometheusMetricWriteAccess 角色,生成 AK/SK 作为账号密码。您也可以在实例设置页面,进行免密设置,开启面向
0.0.0.0
的免密写。
步骤2:使用 promtool migrate
执行迁移
promtool migrate
是阿里云 Prometheus 官方提供的工具,用于将本地 TSDB 数据迁移到阿里云 Prometheus 服务。
准备工作。
备份数据:迁移前备份自建 Prometheus 的
data/
目录(TSDB 存储路径)。安装 promtool。
curl -fsSL "https://o11y-addon-hangzhou-public.oss-cn-hangzhou.aliyuncs.com/share/promtool/install.sh" | bash
查看 promtool 命名帮助。
# 查看支持的所有工具命令 promtool --help # 查看数据迁移参数 promtool migrate --help
执行迁移命令。
promtool migrate \ --db_dir=/path/to/local/prometheus/data \ # 自建 Prometheus 的 TSDB 数据目录 --output=rw # RemoteWrite 方式写出 --rw_url=<阿里云 Remote Write URL> \ # 例如:https://xxx.region.aliyuncs.com/api/v1/write --rw_username=<Access Key ID> \ # 阿里云实例的访问密钥 ID --rw_password=<Access Key Secret> \ # 阿里云实例的访问密钥 Secret --min_time=START_TIMESTAMP \ # 可选:迁移起始时间(毫秒级时间戳) --max_time=END_TIMESTAMP \ # 可选:迁移结束时间(毫秒级时间戳)
参数
描述
示例值
--db_dir
自建 Prometheus 的 TSDB 数据目录路径(必填)
/opt/prometheus/data
--rw_url
阿里云 Prometheus 实例的 Remote Write 地址(必填)
https://xxx.region.aliyuncs.com/api/v1/write
--output
数据输出方式
取值:
rw
或stdout
--rw_username
阿里云实例的 Access Key ID(未开启免密则必填)
LTAI5tXXXXXXX
--rw_password
阿里云实例的 Access Key Secret(未开启免密则必填)
Co4XXXXXXXXX
--min_time
迁移数据的起始时间(毫秒级时间戳),默认为 0,即从最早的数据开始迁移。
1609433200000
--max_time
迁移数据的结束时间(毫秒级时间戳),默认为迁移开始的时间。
1640972400000
--match
过滤迁移的指标(支持正则表达式)
'__name__=~"http_*"'
--rw_batch_size
每批次发送的样本数量(调整性能)
1000
验证迁移结果。
检查阿里云 Prometheus 实例。
登录云监控控制台或者Prometheus控制台,在实例列表页面单击目标实例名称。
在左侧导航栏单击指标管理,单击指标探索页签,执行 PromQL 查询。
确认历史数据时间范围与自建 Prometheus 对应。
对比数据完整性。
选择关键指标(如
up
、node_cpu_seconds_total
),对比迁移前后数据量是否一致。
注意事项
数据过滤
若需迁移特定时间段或指标,务必通过
--min_time
、--max_time
和--match
参数精确控制,避免冗余传输。性能优化
调整
--rw_batch_size
:增大值可提升迁移速度,但需根据网络带宽和阿里云实例负载调整(建议 500~2000)。分批次迁移:若数据量过大,可分时间段执行迁移(如按周或按月)。
安全性
避免在命令行直接暴露
--rw_password
,可改用环境变量或配置文件传递敏感信息。网络与稳定性
确保迁移期间网络稳定,若中途失败可从上次中断的时间点继续(通过
--min_time
指定)。
常见问题
Q1:迁移过程中出现认证失败?
检查
--rw_username
和--rw_password
是否与阿里云实例的 Access Key 匹配。确认 Remote Write URL 是否正确,且阿里云实例已启用 Remote Write 端点。
Q2:迁移速度过慢?
增大
--rw_batch_size
值(如--rw_batch_size=1000
)。确保自建 Prometheus 服务器与阿里云实例的网络带宽充足。
Q3:迁移后数据未显示?
等待几分钟让阿里云实例同步数据。
检查 Prometheus 实例的 数据保留时间 设置是否足够。
时间戳转换工具
可通过以下命令将日期转换为毫秒级时间戳。
date -d "2020-01-01 00:00:00" +%s%3N
# 输出示例:1577836800000(对应 2020-01-01 00:00:00 UTC)