Atlantis
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

1.4 备份和恢复

官方文档:备份和恢复

集群的高可用简单来说就是 etcd 的高可用,一般在生产环境中会将 etcd 独立部署,并做高可用,这样即使遇到 master 节点异常也只需要替换节点即可修复集群。

而对于 K3s,由于是部署在资源受限的环境下,且一般是单节点保存所有数据,且集群数据中也保存了节点信息,无法做到将数据备份迁移到新机器的操作,笔者认为这里的备份和恢复方法仅适用于本机,例如回滚集群或者重装系统的情况。这种变更操作前后的机器的 hostname、IP 等保持不变,下面只关注使用内嵌的 etcd/SQLite的场景。

第一步是备份还原 Server Token 文件,位于 /var/lib/rancher/k3s/server/token

使用 SQLite 作为集群数据存储时,直接备份和还原 /var/lib/rancher/k3s/server/db 目录即可。

使用 etcd 作为集群数据存储时,需要使用 k3s etcd-snapshot 命令导出快照然后恢复。

K3s 默认在系统时间的 0 点和 12 点生成 etcd 快照,并保留最近 5 个,使用 k3s etcd-snapshot save 可以手动创建快照。下面是手动创建快照,然后列出快照的示例:

➜  ~ k3s etcd-snapshot save
INFO[0000] Managed etcd cluster bootstrap already complete and initialized
INFO[0000] Applying CRD addons.k3s.cattle.io
INFO[0000] Applying CRD helmcharts.helm.cattle.io
INFO[0000] Applying CRD helmchartconfigs.helm.cattle.io
INFO[0000] Saving etcd snapshot to /var/lib/rancher/k3s/server/db/snapshots/on-demand-lighthouse-1712296377
{"level":"info","ts":"2024-04-05T13:52:57.225+0800","caller":"snapshot/v3_snapshot.go:65","msg":"created temporary db file","path":"/var/lib/rancher/k3s/server/db/snapshots/on-demand-lighthouse-1712296377.part"}
{"level":"info","ts":"2024-04-05T13:52:57.226+0800","logger":"client","caller":"[email protected]/maintenance.go:211","msg":"opened snapshot stream; downloading"}
{"level":"info","ts":"2024-04-05T13:52:57.226+0800","caller":"snapshot/v3_snapshot.go:73","msg":"fetching snapshot","endpoint":"https://127.0.0.1:2379"}
{"level":"info","ts":"2024-04-05T13:52:57.301+0800","logger":"client","caller":"[email protected]/maintenance.go:219","msg":"completed snapshot read; closing"}
{"level":"info","ts":"2024-04-05T13:52:57.323+0800","caller":"snapshot/v3_snapshot.go:88","msg":"fetched snapshot","endpoint":"https://127.0.0.1:2379","size":"5.6 MB","took":"now"}
{"level":"info","ts":"2024-04-05T13:52:57.323+0800","caller":"snapshot/v3_snapshot.go:97","msg":"saved","path":"/var/lib/rancher/k3s/server/db/snapshots/on-demand-lighthouse-1712296377"}
INFO[0000] Reconciling etcd snapshot data in k3s-etcd-snapshots ConfigMap
INFO[0000] Reconciliation of snapshot data in k3s-etcd-snapshots ConfigMap complete
➜  ~ k3s etcd-snapshot ls
Name                                Location                                                                            Size    Created
on-demand-lighthouse-1712296377     file:///var/lib/rancher/k3s/server/db/snapshots/on-demand-lighthouse-1712296377     5570592 2024-04-05T13:52:57+08:00
etcd-snapshot-lighthouse-1712116800 file:///var/lib/rancher/k3s/server/db/snapshots/etcd-snapshot-lighthouse-1712116800 5570592 2024-04-03T12:00:00+08:00
etcd-snapshot-lighthouse-1712160000 file:///var/lib/rancher/k3s/server/db/snapshots/etcd-snapshot-lighthouse-1712160000 5570592 2024-04-04T00:00:00+08:00
etcd-snapshot-lighthouse-1712203200 file:///var/lib/rancher/k3s/server/db/snapshots/etcd-snapshot-lighthouse-1712203200 5570592 2024-04-04T12:00:00+08:00
etcd-snapshot-lighthouse-1712246400 file:///var/lib/rancher/k3s/server/db/snapshots/etcd-snapshot-lighthouse-1712246400 5570592 2024-04-05T00:00:00+08:00
etcd-snapshot-lighthouse-1712289600 file:///var/lib/rancher/k3s/server/db/snapshots/etcd-snapshot-lighthouse-1712289600 5570592 2024-04-05T12:00:00+08:00

将快照文件备份后,可以执行命令:k3s server --cluster-reset --cluster-reset-restore-path=<PATH-TO-SNAPSHOT>,指定快照文件恢复数据。