锁定您的 swarm 以保护其加密密钥

默认情况下,swarm 管理器使用的 Raft 日志在磁盘上是加密的。这种静态加密可以保护您的服务配置和数据,防止攻击者获取加密的 Raft 日志。引入此功能的原因之一是为了支持 Docker secrets 功能。

当 Docker 重新启动时,用于加密 swarm 节点间通信的 TLS 密钥以及用于在磁盘上加密和解密 Raft 日志的密钥都会加载到每个管理器节点的内存中。Docker 能够保护静态的相互 TLS 加密密钥以及用于加密和解密 Raft 日志的密钥,它允许您拥有这些密钥的所有权,并要求手动解锁您的管理器。此功能称为自动锁定。

当 Docker 重新启动时,您必须首先使用 Docker 在锁定 swarm 时生成的密钥加密密钥解锁 swarm。您可以随时轮换此密钥加密密钥。

注意

当新节点加入 swarm 时,您无需解锁 swarm,因为密钥会通过相互 TLS 传播给它。

启用自动锁定初始化 swarm

初始化一个新的 swarm 时,您可以使用 --autolock 标志来启用 swarm 管理器节点在 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 重新启动时,您需要解锁 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.
警告

当您轮换解锁密钥时,请将旧密钥的记录保留几分钟,这样如果某个管理器在获得新密钥之前宕机,它仍然可以用旧密钥解锁。

© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.