本文介紹如何將自建開源 Prometheus 歷史資料移轉到阿里雲 Prometheus。
執行條件
資料來源:自建開源 Prometheus 執行個體必須使用 TSDB 儲存(預設儲存引擎)。
阿里雲Prometheus執行個體:需確保目標阿里雲 Prometheus 執行個體已建立且處於運行狀態。
網路連通性:自建 Prometheus 伺服器需能訪問阿里雲 Prometheus 的遠程寫入(Remote Write)端點。
工具依賴:需安裝阿里雲提供的 promtool 工具。
遷移步驟
步驟1:建立或擷取阿里雲 Prometheus 執行個體
登入阿里雲控制台。
登入CloudMonitor控制台或者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過濾遷移的指標(支援Regex)
'__name__=~"http_*"'--rw_batch_size每批次發送的樣本數量(調整效能)
1000驗證遷移結果。
檢查阿里雲 Prometheus 執行個體。
登入CloudMonitor控制台或者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)