锁定您的集群以保护其加密密钥
Swarm 管理器使用的 Raft 日志默认在磁盘上是加密的。这种静态加密可以保护您的服务配置和数据,防止攻击者在获取对加密 Raft 日志的访问权后泄露信息。引入此功能的原因之一是为了支持 Docker Secrets 功能。
当 Docker 重启时,用于加密 Swarm 节点间通信的 TLS 密钥以及用于加密和解密磁盘上 Raft 日志的密钥都会被加载到每个管理节点的内存中。Docker 允许您接管这些密钥并强制要求手动解锁您的管理节点,从而保护处于静默状态(at rest)的相互认证 TLS 加密密钥以及用于加密和解密 Raft 日志的密钥。此功能称为自动锁定(autolock)。
当 Docker 重启时,您必须首先使用 Docker 在锁定集群时生成的密钥加密密钥 解锁集群。您可以随时轮换此密钥加密密钥。
注意
当新节点加入集群时,您无需解锁集群, 因为密钥会通过双向 TLS 传播到该节点。
初始化一个启用自动锁定功能的集群
初始化新集群时,您可以使用 --autolock 标志来启用 Docker 重启时的集群管理器节点自动锁定功能。
$ docker swarm init --autolock
Swarm initialized: current node (k1q27tfyx9rncpixhk69sa61v) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-0j52ln6hxjpxk2wgk917abcnxywj3xed0y8vi1e5m9t3uttrtu-7bnxvvlz2mrcpfonjuztmtts9 \
172.31.46.109:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
To unlock a swarm manager after it restarts, run the `docker swarm unlock`
command and provide the following key:
SWMKEY-1-WuYH/IX284+lRcXuoVf38viIDK3HJEKY13MIHX+tTt8
将密钥保存在安全的地方,例如密码管理器中。
当 Docker 重启时,您需要 解锁集群。锁定的集群在尝试启动或重启服务时会引发如下错误:
$ sudo service docker restart
$ docker service ls
Error response from daemon: Swarm is encrypted and needs to be unlocked before it can be used. Use "docker swarm unlock" to unlock it.
启用或禁用现有群集的自动锁定
要在现有集群中启用自动锁定,请将 autolock 标志设置为 true。
$ docker swarm update --autolock=true
Swarm updated.
To unlock a swarm manager after it restarts, run the `docker swarm unlock`
command and provide the following key:
SWMKEY-1-+MrE8NgAyKj5r3NcR4FiQMdgu+7W72urH0EZeSmP/0Y
Please remember to store this key in a password manager, since without it you
will not be able to restart the manager.
要禁用自动锁定,请将 --autolock 设置为 false。用于读写 Raft 日志的相互 TLS 密钥和加密密钥将以未加密形式存储在磁盘上。这需要在“静态存储未加密密钥的风险”与“无需解锁每个管理器即可重启 Swarm 的便利性”之间进行权衡。
$ docker swarm update --autolock=false
在禁用自动锁定后,请暂时保留解锁密钥,以防某个管理器仍配置为使用旧密钥进行锁定。
释放一个蜂群
要解锁已锁定的集群,请使用 docker swarm unlock。
$ docker swarm unlock
Please enter unlock key:
输入在锁定群集或轮换密钥时,命令输出中生成并显示的加密密钥,以解锁该群集。
查看运行中 Swarm 的当前解锁密钥
假设您的集群运行正常,随后某个管理节点变得不可用。您排查了问题并将物理节点重新上线,但需要提供解锁密钥来读取加密的凭据和 Raft 日志,从而解锁该管理节点。
如果节点离开集群后密钥尚未轮换,并且集群中拥有法定数量的可用管理节点,您可以使用不带任何参数的 docker swarm unlock-key 查看当前的解锁密钥。
$ docker swarm unlock-key
To unlock a swarm manager after it restarts, run the `docker swarm unlock`
command and provide the following key:
SWMKEY-1-8jDgbUNlJtUe5P/lcr9IXGVxqZpZUXPzd+qzcGp4ZYA
Please remember to store this key in a password manager, since without it you
will not be able to restart the manager.
如果密钥在 Swarm 节点不可用后已轮换,且您没有之前密钥的记录,则可能需要强制管理器离开 Swarm,然后作为新管理器重新加入 Swarm。
旋转解锁密钥
您应定期轮换已锁定集群的解锁密钥。
$ docker swarm unlock-key --rotate
Successfully rotated manager unlock key.
To unlock a swarm manager after it restarts, run the `docker swarm unlock`
command and provide the following key:
SWMKEY-1-8jDgbUNlJtUe5P/lcr9IXGVxqZpZUXPzd+qzcGp4ZYA
Please remember to store this key in a password manager, since without it you
will not be able to restart the manager.
警告
当您轮换解锁密钥时,请保留旧密钥几分钟,以便如果管理器在获取新密钥之前发生故障,仍可以使用旧密钥对其进行解锁。