
【玩轉容器持久化存儲】
點我進入活動頁面随着Windows容器逐漸普及和發展,Windows容器持久化存儲以及容器間共享的需求越來越高漲。
本文介紹如何讓Windows主機正确配置NAS SMB檔案系統,支援Windows容器讓Docker鏡像使用挂載NAS SMB檔案系統的子目錄作為持久化存儲目錄。
手工挂載步驟
1. 在阿裡雲控制台建立ECS虛拟機,選擇Windows Container版本。
2. 參考《 Windows系統挂載SMB檔案系統 》官方文檔,修改系統資料庫允許匿名通路,建立SMB檔案系統,建立挂載點。以下為在cmd指令行中修改系統資料庫允許匿名通路的指令。
REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\LanmanWorkstation\Parameters /f /v AllowInsecureGuestAuth /t REG_DWORD /d 1
3. 打開Powershell,使用New-SmbGlobalMapping指令進行挂載
# Define clear text string for username and password
[string]$userName = 'workshop\administrator'
[string]$userPassword = '???'
# Convert to SecureString
[securestring]$secStringPassword = ConvertTo-SecureString $userPassword -AsPlainText -Force
[pscredential]$credObject = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)
# Mount SMB share for container
New-SmbGlobalMapping -LocalPath z: -RemotePath \\file-system-id.region.nas.aliyuncs.com\myshare -Persistent $true -Credential $credObject
注意New-SmbGlobalMapping挂載指令使用的使用者名是workshop/administrator,密碼需要填上ECS的administrator密碼。
4. 使用New-Item建立連結,作為容器的資料共享盤
New-Item -ItemType SymbolicLink -Value \\file-system-id.region.nas.aliyuncs.com\myshare -Path c:\datashare
5. 在cmd指令行使用docker run指令運作容器。注意選擇ECS作業系統版本對應的容器鏡像
docker run -ti --rm -v c:\datashare:c:\data --entrypoint="" registry.cn-hangzhou.aliyuncs.com/acs/flexvolume:v1.16.9.205f5a3-windows1809 pwsh.exe
完成之後在彈出的容器指令行界面中,即可對c:\data目錄進行操作,内容會存儲在NAS SMB卷中
開機自啟動步驟
以上步驟經過改進,可以做成開啟啟動腳本,使得機器啟動後即可生成帶持久化存儲目錄的容器。
1. 在C槽建立c:\startup_script.ps1腳本
# Define clear text string for username and password
[string]$userName = 'workshop\administrator'
[string]$userPassword = '???'
# Convert to SecureString
[securestring]$secStringPassword = ConvertTo-SecureString $userPassword -AsPlainText -Force
[pscredential]$credObject = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)
New-SmbGlobalMapping -LocalPath y: -RemotePath \\file-system-id.region.nas.aliyuncs.com\myshare -Persistent $true -Credential $credObject
New-Item -ItemType SymbolicLink -Value \\file-system-id.region.nas.aliyuncs.com\myshare -Path c:\datashare
2. 在C槽建立startup_script.bat腳本
ECHO ON
ECHO This startup_script will mount smb share and start container
powershell -file "c:\startup_script.ps1" >> c:\startup_script.output.txt
docker run -ti --rm -v c:\datashare:c:\data --entrypoint="" registry.cn-hangzhou.aliyuncs.com/acs/flexvolume:v1.16.9.205f5a3-windows1809 pwsh.exe
3. 在cmd指令行運作以下指令,生成開機任務
schtasks /create /tn "startup_script" /tr "c:\startup_script.bat" /sc onstart /RU SYSTEM /RL HIGHEST
schtasks /run /tn "startup_script"