鎖定您的 Swarm 以保護其加密金鑰

Swarm 管理器使用的 Raft 記錄預設會在磁碟上加密。這種靜態加密可以保護您的服務設定和資料免受取得加密 Raft 記錄存取權的攻擊者攻擊。引入此功能的原因之一是為了支援 Docker secrets 功能。

當 Docker 重新啟動時,用於加密 Swarm 節點之間通訊的 TLS 金鑰和用於加密和解密磁碟上 Raft 記錄的金鑰都會載入到每個管理器節點的記憶體中。 Docker 可以保護用於加密和解密靜態 Raft 記錄的相互 TLS 加密金鑰和金鑰,方法是允許您取得這些金鑰的所有權,並要求手動解除鎖定您的管理器。此功能稱為自動鎖定。

當 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.

警告

當您輪替解鎖金鑰時,請將舊金鑰的記錄保留幾分鐘,以便如果管理節點在取得新金鑰之前發生故障,仍然可以使用舊金鑰將其解鎖。