锁定你的 Swarm 以保护其加密密钥
默认情况下,swarm 管理器使用的 Raft 日志在磁盘上进行加密。这种静态加密可以保护你的服务的配置和数据,使其免受获得加密 Raft 日志访问权限的攻击者的攻击。引入此功能的原因之一是为了支持Docker 密钥功能。
当 Docker 重启时,用于加密 swarm 节点之间通信的 TLS 密钥以及用于在磁盘上加密和解密 Raft 日志的密钥都会加载到每个管理器节点的内存中。Docker 能够通过允许你拥有这些密钥并要求手动解锁你的管理器来保护相互 TLS 加密密钥和用于静态加密和解密 Raft 日志的密钥。此功能称为自动锁定。
当 Docker 重启时,你必须首先解锁 swarm,使用 Docker 在 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
。用于读取和写入 Raft 日志的相互 TLS 密钥和加密密钥将未加密地存储在磁盘上。在将加密密钥未加密地存储在静态位置的风险与无需解锁每个管理器即可重启 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.
警告
轮换解锁密钥时,请保留旧密钥几分钟,以便如果管理器在获得新密钥之前出现故障,仍然可以使用旧密钥解锁。