恒美微站
首页
关于我们
建站服务
主题模板
案例展示
资讯中心
联系我们
如何用 etcdctl snapshot save 与 etcdutl snapshot restore 重建 etcd 集群并启动成员
首页
资讯中心
/
如何用 etcdctl snapshot save 与 etcdutl snapshot restore 重建 etcd 集群并启动成员
如何用 etcdctl snapshot save 与 etcdutl snapshot restore 重建 etcd 集群并启动成员
发布时间:2026/9/13 18:47:24
如何用 etcdctl snapshot save 与 etcdutl snapshot restore 重建 etcd 集群并启动成员【免费下载链接】etcdDistributed reliable key-value store for the most critical data of a distributed system项目地址: https://gitcode.com/GitHub_Trending/et/etcd当原 etcd 集群需要整体迁移或者数据目录损坏、节点全部丢失时可以用一份后端数据库快照在新位置重建集群先用etcdctl snapshot save把运行中集群的数据保存到文件再用etcdutl snapshot restore为每个待建成员恢复出数据目录最后按恢复时的集群配置逐个启动成员。这条路径在仓库文档中有一条完整的三节点示例etcdutl/README.md 的 SNAPSHOT RESTORE 一节给出的正是 Save a snapshot, restore into a new 3 node cluster, and start the cluster 流程。适用前提与工具分工需要 etcd 服务、etcdctl与etcdutl三个二进制。etcd 的预构建发布版包含这些组件见根目录 README.md Getting etcd 一节etcdctl/README.md 说明 etcdctl 也可以从源码构建。分工是固定的etcdctl走网络访问集群etcdutl直接操作 etcd 数据文件etcdutl/README.md 开头的定位说明。因此保存快照用etcdctl snapshot save恢复快照用etcdutl snapshot restore。注意版本行为etcdctl snapshot restore与etcdctl snapshot status在 v3.6 中已移除需改用etcdutl snapshot restore/etcdutl snapshot statusetcdctl/README.md SNAPSHOT RESTORE 与 SNAPSHOT STATUS 两节。主分支上 etcdctl 默认使用 v3 API如果使用早于 v3.4 的发布版需设置环境变量ETCDCTL_API3才使用 v3 APIetcdctl/README.md 开头说明。本文所有命令示例中的 URL 端口客户端 2379/22379/32379peer 12380/22380/32380均取自文档示例实际使用时按自己的部署替换但同一集群内--initial-cluster、--listen-peer-urls等配置必须与文档示例中的对应关系保持一致。第一步用 etcdctl snapshot save 保存快照etcdctl snapshot save会把运行中 etcd 后端数据库的某个时间点快照写入文件etcdctl/README.md SNAPSHOT SAVE 一节./etcdctl snapshot save snapshot.db文档在 etcdctl/ctlv3/command/snapshot_command.go 的命令示例即etcdctl snapshot save --help展示的内容中给出了若干变体按需选用# 指定集群地址 etcdctl snapshot save --endpoints127.0.0.1:3000 /backup/etcd-snapshot.db # 使用证书连接 TLS 集群 etcdctl --endpointshttps://127.0.0.1:2379 --cacert/etc/etcd/ca.crt --cert/etc/etcd/etcd.crt --key/etc/etcd/etcd.key snapshot save /backup/etcd-snapshot.db # 使用用户名密码 etcdctl --userroot --passwordpassword123 snapshot save /backup/etcd-snapshot.db # 指定连接超时 etcdctl --endpointshttps://127.0.0.1:2379 --dial-timeout20s snapshot save /backup/etcd-snapshot.db # 文件名带时间戳便于多份备份 etcdctl snapshot save /mnt/backup/etcd/backup_$(date %Y%m%d_%H%M%S).db不指定--endpoints时etcdctl 默认访问http://127.0.0.1:2379etcdctl/README.md --endpoint 一节。如何判断保存成功命令成功时按 snapshot_command.go 中的输出逻辑打印Snapshot saved at 路径若服务端返回了版本信息还会再打印Server version 版本。保存成功后建议先用etcdutl snapshot status检查快照文件它输出数据库哈希、revision、key 总数与大小。etcdutl/README.md 给出的示例输出示例结果实际数值不同./etcdutl snapshot status file.db # cf1550fb, 3, 3, 25 kB-w/--write-out可切换json、table等格式例如./etcdutl --write-outjson snapshot status file.db。记录这里的 hash 与 revision便于和恢复后的集群对照。第二步用 etcdutl snapshot restore 为每个成员恢复数据目录etcdutl snapshot restore从快照文件加一份新的集群配置为一个成员创建 etcd 数据目录对集群中每个成员各执行一次就得到一组预载快照数据的成员数据目录可以据此初始化一个新 etcd 集群etcdutl/README.md SNAPSHOT RESTORE 一节的描述。文档给出的三节点完整示例恢复阶段直接取自 etcdutl/README.md./etcdutl snapshot restore snapshot.db --initial-cluster-token etcd-cluster-1 --initial-advertise-peer-urls http://127.0.0.1:12380 --name sshot1 --initial-cluster sshot1http://127.0.0.1:12380,sshot2http://127.0.0.1:22380,sshot3http://127.0.0.1:32380 ./etcdutl snapshot restore snapshot.db --initial-cluster-token etcd-cluster-1 --initial-advertise-peer-urls http://127.0.0.1:22380 --name sshot2 --initial-cluster sshot1http://127.0.0.1:12380,sshot2http://127.0.0.1:22380,sshot3http://127.0.0.1:32380 ./etcdutl snapshot restore snapshot.db --initial-cluster-token etcd-cluster-1 --initial-advertise-peer-urls http://127.0.0.1:32380 --name sshot3 --initial-cluster sshot1http://127.0.0.1:12380,sshot2http://127.0.0.1:22380,sshot3http://127.0.0.1:32380各选项的含义与默认值etcdutl/README.md SNAPSHOT RESTORE 选项列表及 etcdutl/etcdutl/snapshot_command.go 中的 flag 定义--name成员的可读名缺省为default示例中三条命令分别对应 sshot1/2/3。--initial-cluster恢复出的新集群的成员列表三个成员的列表内容必须完全一致只改各自的--name和--initial-advertise-peer-urls。--initial-cluster-token集群 token默认etcd-cluster。--initial-advertise-peer-urls本成员的 peer URL默认http://localhost:2380。--data-dir输出数据目录不给时使用name.etcd。示例中两条命令都没传--data-dir即分别生成sshot1.etcd、sshot2.etcd、sshot3.etcd与第三步启动命令中的--name约定相匹配如果你显式指定了--data-dir启动成员时要保证 etcd 进程使用的是同一目录。--wal-dirWAL 目录不给时使用数据目录。--skip-hash-check忽略快照完整性哈希校验文档注明如果快照文件是从数据目录复制出来的则必须加。--bump-revision恢复后把最新 revision 提升多少--mark-compacted将恢复后的最新 revision 标记为计划压缩点。两者是联动的--bump-revision 0时必须同时给--mark-compacted--bump-revision为 0 时不允许给--mark-compactedsnapshot_command.go 中的参数校验。恢复阶段没有需要交互的输入按 etcdutl/README.md Exit codes 一节所有命令成功返回 0失败返回非零并把错误写到标准错误输出所以脚本里判断退出码即可。第三步启动恢复后的成员数据目录就绪后按文档示例逐个启动成员--name与各成员恢复时的名字一致peer/client 端口与恢复阶段声明的一致直接取自 etcdutl/README.md 示例./etcd --name sshot1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 ./etcd --name sshot2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 ./etcd --name sshot3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 注意这些命令的会把成员放到后台运行示例是单机演示三个进程占用本机端口 2379/22379/32379client与 12380/22380/32380peer。可选分支如果你只想在本机快速搭一个三节点集群练手仓库的 Procfile 提供了一套infra1/2/3成员的完整启动参数含--initial-cluster-token、--initial-cluster、--initial-cluster-state new配合 goreman 用goreman start启动根目录 README.md Running a local etcd cluster 一节说明了该用法。它适合演示不是快照恢复路径的一部分。验证新集群集群启动后用 etcdctl 对恢复出的集群做检查以下输出均为 etcdctl/README.md 中的示例结果实际数值不同健康检查——检查与默认端点关联的集群中所有端点./etcdctl endpoint --cluster health # http://127.0.0.1:2379 is healthy: successfully committed proposal: took 1.060091ms # http://127.0.0.1:22379 is healthy: successfully committed proposal: took 903.138µs # http://127.0.0.1:32379 is healthy: successfully committed proposal: took 1.113848ms文档说明能参与共识的端点输出 healthy不能参与共识的输出 unhealthy。成员列表——确认三个成员都在集群内./etcdctl member list # 8211f1d0f64f3269, started, infra1, http://127.0.0.1:12380, http://127.0.0.1:2379 # 91bc3c398fb3c146, started, infra2, http://127.0.0.1:22380, http://127.0.0.1:22379 # fd422379fda50e48, started, infra3, http://127.0.0.1:32380, http://127.0.0.1:32379端点状态——查看每个端点的版本、数据库大小、领导者等./etcdctl -w table endpoint --cluster statusKV 哈希——endpoint hashkv取各端点键值存储的哈希文档示例中三个端点的哈希一致./etcdctl endpoint hashkv --cluster # http://127.0.0.1:2379, 2064120424, 13 # http://127.0.0.1:22379, 2064120424, 13 # http://127.0.0.1:32379, 2064120424, 13此外可以对恢复出的成员数据目录离线核对etcdutl hashkv db 文件输出 KV 哈希、hash revision 与 compact revisionetcdutl/README.md HASHKV 一节的示例为35c86e9b, 214, 150属示例结果与在线的endpoint hashkv输出可相互对照。限制与边界etcdctl snapshot restore与etcdctl snapshot status在 v3.6 已移除只保留etcdctl snapshot save恢复与状态检查一律走etcdutletcdctl/README.md。快照文件如果是直接从某成员数据目录而非snapshot save生成复制出来的restore 时必须加--skip-hash-check否则完整性哈希校验不通过etcdutl/README.md。--bump-revision与--mark-compacted只能按文档描述的联动关系使用参数校验失败会直接报错退出。本文示例输出snapshot status、health、member list、hashkv等全部来自文档仅作示例结果展示不要把它们当作恢复后必须得到的固定数值。命令的退出码约定etcdutl 侧成功为 0失败为非零错误信息写入标准错误输出etcdutl/README.md Exit codes 一节可作为自动化脚本里的成功判定依据。【免费下载链接】etcdDistributed reliable key-value store for the most critical data of a distributed system项目地址: https://gitcode.com/GitHub_Trending/et/etcd创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考