锁定你的 Swarm 以保护其加密密钥
Swarm 管理器使用的 Raft 日志默认在磁盘上加密。这种静态加密保护服务的配置和数据免受获取加密 Raft 日志的攻击者侵害。引入此功能的一个原因是支持 Docker 机密信息 功能。
当 Docker 重启时,用于加密 Swarm 节点间通信的 TLS 密钥以及用于加密和解密磁盘上的 Raft 日志的密钥会被加载到每个管理器节点的内存中。Docker 能够通过允许你拥有这些密钥并在重启时要求手动解锁管理器来保护相互 TLS 加密密钥和用于静态加密及解密 Raft 日志的密钥。此功能称为自动锁定 (autolock)。
当 Docker 重启时,你必须首先使用 Docker 锁定 Swarm 时生成的密钥加密密钥来解锁 Swarm。你可以随时轮换此密钥加密密钥。
注意
当新节点加入 Swarm 时,你无需解锁 Swarm,因为密钥通过相互 TLS 传播给了它。
初始化启用自动锁定功能的 Swarm
初始化新 Swarm 时,你可以使用 --autolock
标志在 Docker 重启时启用 Swarm 管理器节点的自动锁定。
$ 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 重启时,你需要解锁 Swarm。锁定的 Swarm 在尝试启动或重启服务时会引发如下错误
$ 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.
在现有 Swarm 上启用或禁用自动锁定
要在现有 Swarm 上启用自动锁定,将 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
。相互 TLS 密钥和用于读写 Raft 日志的加密密钥将以未加密的形式存储在磁盘上。这需要在静态存储未加密加密密钥的风险与无需解锁每个管理器即可重启 Swarm 的便利性之间进行权衡。
$ docker swarm update --autolock=false
禁用自动锁定后,请将解锁密钥保留一小段时间,以防某个管理器在仍配置为使用旧密钥锁定时发生故障。
解锁 Swarm
要解锁锁定的 Swarm,请使用 docker swarm unlock
。
$ docker swarm unlock
Please enter unlock key:
输入锁定 Swarm 或轮换密钥时命令输出中生成并显示的加密密钥,即可解锁 Swarm。
查看运行中 Swarm 的当前解锁密钥
考虑一种情况,你的 Swarm 正常运行,但某个管理器节点变得不可用。你排查问题并将物理节点重新上线,但需要通过提供解锁密钥来解锁管理器,以读取加密的凭据和 Raft 日志。
如果自节点离开 Swarm 以来密钥未曾轮换,并且 Swarm 中有足够数量的正常运行的管理器节点,你可以使用不带任何参数的 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。
轮换解锁密钥
你应该定期轮换锁定的 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.
警告
轮换解锁密钥时,请将旧密钥保留几分钟,这样即使某个管理器在获取新密钥之前发生故障,仍然可以使用旧密钥解锁它。