鎖定您的 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.
警告當您輪換解鎖金鑰時,請將舊金鑰的記錄保留幾分鐘,這樣如果某個管理器在獲得新金鑰之前宕機,它仍然可以用舊金鑰解鎖。