鎖定您的叢集以保護其加密金鑰
Swarm 管理節點所使用的 Raft 日誌預設會在磁碟上加密。此靜態加密可保護您服務的設定與資料,防止攻擊者取得加密後的 Raft 日誌存取權。引入此功能的其中一個原因,是為了支援 Docker secrets 功能。
當 Docker 重新啟動時,用於加密 swarm 節點間通訊的 TLS 金鑰,以及用於加密和解密磁碟上 Raft 日誌的金鑰,都會載入到每個管理節點的記憶體中。Docker 具備保護相互 TLS 加密金鑰以及靜態加密與解密 Raft 日誌之金鑰的能力,透過讓您取得這些金鑰的所有權,並要求手動解鎖您的管理節點。此功能稱為自動鎖定 (autolock)。
當 Docker 重新啟動時,您必須先解鎖 swarm,使用 Docker 在鎖定 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.
警告當您輪替解鎖金鑰時,請保留舊金鑰的紀錄幾分鐘,以便在管理節點在取得新金鑰前發生故障時,仍能使用舊金鑰進行解鎖。