consul备份
一 consul备份内容
Consul 提供了可以使用 CLI 或 API 运行的快照命令。该snapshot
命令保存了 Consul 服务器状态的时间点快照,其中包括但不限于:
- Key-Value entries
- the service catalog
- prepared queries
- sessions
- ACLs
二 consul备份命令格式
2.1 consul snapshot命令格式
root@consul-01:~# consul snapshot --help
Usage: consul snapshot [options] [args]
此命令具有用于保存、恢复和检查状态的子命令用于灾难恢复的 Consul 服务器。 这些是原子的,时间点快照,包括 key/value entries, service catalog, prepared queries,sessions, and ACLs.
如果启用了 ACL,则必须提供管理令牌才能执行快照操作。
创建快照:
$ consul snapshot save backup.snap
恢复快照:
$ consul snapshot restore backup.snap
检查快照:
$ consul snapshot inspect backup.snap
运行一个守护进程,每小时在本地保存一个快照(available only in
Consul Enterprise) :
$ consul snapshot agent
For more examples, ask for subcommand help or view the documentation.
Subcommands:
inspect 显示有关 Consul 快照文件的信息
restore 恢复 Consul 服务器状态的快照
save 保存 Consul 服务器状态的快照
2.2 consul snapshot 子命令使用帮助
点击查看代码
root@consul-01:~# consul snapshot save --help Usage: consul snapshot save [options] FILE Retrieves an atomic, point-in-time snapshot of the state of the Consul servers which includes key/value entries, service catalog, prepared queries, sessions, and ACLs. If ACLs are enabled, a management token must be supplied in order to perform snapshot operations. To create a snapshot from the leader server and save it to "backup.snap": $ consul snapshot save backup.snap To create a potentially stale snapshot from any available server (useful if no leader is available): $ consul snapshot save -stale backup.snap For a full list of options and examples, please see the Consul documentation. HTTP API Options -ca-file=
Path to a CA file to use for TLS when communicating with Consul. This can also be specified via the CONSUL_CACERT environment variable. -ca-path= Path to a directory of CA certificates to use for TLS when communicating with Consul. This can also be specified via the CONSUL_CAPATH environment variable. -client-cert= Path to a client cert file to use for TLS when 'verify_incoming' is enabled. This can also be specified via the CONSUL_CLIENT_CERT environment variable. -client-key= Path to a client key file to use for TLS when 'verify_incoming' is enabled. This can also be specified via the CONSUL_CLIENT_KEY environment variable. -datacenter= Name of the datacenter to query. If unspecified, this will default to the datacenter of the queried agent. -http-addr= The `address` and port of the Consul HTTP agent. The value can be an IP address or DNS address, but it must also include the port. This can also be specified via the CONSUL_HTTP_ADDR environment variable. The default value is http://127.0.0.1:8500. The scheme can also be set to HTTPS by setting the environment variable CONSUL_HTTP_SSL=true. -stale Permit any Consul server (non-leader) to respond to this request. This allows for lower latency and higher throughput, but can result in stale data. This option has no effect on non-read operations. The default value is false. -tls-server-name=
The server name to use as the SNI host when connecting via TLS. This can also be specified via the CONSUL_TLS_SERVER_NAME environment variable. -token= ACL token to use in the request. This can also be specified via the CONSUL_HTTP_TOKEN environment variable. If unspecified, the query will default to the token of the Consul agent at the HTTP address. -token-file= File containing the ACL token to use in the request instead of one specified via the -token argument or CONSUL_HTTP_TOKEN environment variable. This can also be specified via the CONSUL_HTTP_TOKEN_FILE environment variable.
三 备份consul数据
3.1 注册服务
root@consul-01:~# curl --request PUT --data @k8s-node.json http://192.168.174.103:8500/v1/agent/service/register
3.2 查看当前服务状态
3.3 创建备份
root@consul-01:~# consul snapshot save -stale k8s-node-01.snap
Saved and verified snapshot to index 865
3.4 查看备份信息
root@consul-01:~# consul snapshot inspect k8s-node-01.snap
ID 3-771-1637895845606 #快照的唯一 ID,仅用于区分目的。
Size 14055 #快照的大小,以字节为单位。
Index 771 #快照中最新日志条目的 Raft 索引。
Term 3 #快照中最新日志条目的 Raft term。
Version 1 #快照格式版本。这仅指快照的结构,而不是其中包含的数据。
Type Count Size
---- ---- ----
Register 9 9.9KB
ConnectCA 1 1.2KB
ConnectCAProviderState 1 1.1KB
CoordinateBatchUpdate 3 486B
Index 15 453B
Autopilot 1 199B
ConnectCAConfig 1 176B
FederationState 1 141B
SystemMetadata 1 68B
ChunkingState 1 12B
---- ---- ----
Total 13.7KB
四 从备份恢复数据
4.1 查看consul当前状态
4.2 模拟consul存储目录丢失
root@consul-01:~# rm -rf /opt/consul/*
root@consul-01:~# systemctl restart consul
4.3 验证当前状态信息
4.4 从备份的快照恢复数据
root@consul-01:~# consul snapshot restore k8s-node-01.snap
Restored snapshot
4.5 验证当前状态