- Hands-On Kubernetes on Windows
- Piotr Tylenda
- 836字
- 2021-06-24 16:54:02
Mounting Azure Files SMB share in a container
In order to mount the new Azure Files SMB share as a bind mount in a container, we will leverage the SMB Global Mapping feature that was introduced in Window Server 1709. Global mappings have been introduced specifically for this purpose, that is, mounting SMB shares on the host so that they're visible to containers. Let's get started:
- First, ensure that you are logged in so that you can execute Azure PowerShell (using the Connect-AzAccount command).
- Next, let's define a few variables that will be used in the commands we'll execute soon:
$resourceGroupName = "docker-storage-resource-group"
$storageAccountName = "dockerstorageaccount"
$fileShareName = "docker-bind-mount-share"
The names being used here are exactly the same as the ones we used in the previous subsection while creating the Azure Files SMB share.
- The next step is to define the $storageAccount and $storageAccountKeys variables:
$storageAccount = Get-AzStorageAccount `
-ResourceGroupName $resourceGroupName `
-Name $storageAccountName
$storageAccountKeys = Get-AzStorageAccountKey `
-ResourceGroupName $resourceGroupName `
-Name $storageAccountName
These variables will be used for the retrieval of file share details and credentials for access, both of which are needed for SMB Global Mapping.
- Now, optionally, you can persist your share credentials in Windows Credential Manager using the cmdkey command:
Invoke-Expression -Command `
("cmdkey /add:$([System.Uri]::new($storageAccount.Context.FileEndPoint).Host) " + `
"/user:AZURE\$($storageAccount.StorageAccountName) /pass:$($storageAccountKeys[0].Value)")
- We will also need details regarding Azure Files SMB share, so let's define a new variable called $fileShare:
$fileShare = Get-AzStorageShare -Context $storageAccount.Context | Where-Object {
$_.Name -eq $fileShareName -and $_.IsSnapshot -eq $false
}
- At this point, you can also check if the file share details have been retrieved successfully. By doing this, you will be able to detect if, for example, $fileShareName contains the wrong share name:
if ($fileShare -eq $null) {
Write-Error "Azure File share not found"
}
- The last step, before creating an SMB Global Mapping, is to define a credentials object, which will be used for mapping creation:
$password = ConvertTo-SecureString `
-String $storageAccountKeys[0].Value `
-AsPlainText `
-Force
$credential = New-Object System.Management.Automation.PSCredential `-ArgumentList "AZURE\$($storageAccount.StorageAccountName)", $password
- Finally, we can use the New-SmbGlobalMapping command in order to create the mapping for Azure Files SMB share:
New-SmbGlobalMapping `
-RemotePath "\\$($fileShare.StorageUri.PrimaryUri.Host)\$($fileShare.Name)" `
-Credential $credential `
-Persistent $true `
-LocalPath G:
The preceding command will mount your Azure Files SMB share persistently as the G: drive. You can use this path later for bind mounts for Docker containers. Now, you can test if your mapping works correctly by moving some test files to the G: drive using Windows Explorer.
- A traditional file server hosted in your local network
- A third-party implementation of the SMB protocol,such as NAS appliances
- A traditional SAN or Scale-out File Server (SoFS) on top of Storage Spaces Direct (S2D)
Globally mapped SMB shares, when used as bind mounts, are transparently visible for the containers as regular directories in the local filesystem. All of the "heavy lifting" is performed by the container host, which is responsible for managing the SMB share connection.
Let's demonstrate this feature by creating a simple PowerShell process-isolated container:
- First, create a directory called G:\ContainerData in the SMB share for our demonstration container:
New-Item -ItemType Directory -Force -Path G:\ContainerData
- Now, we can run the container by providing the new directory in the Azure Files SMB share as a bind mount with C:\Data as the target:
docker run -it --rm `
--isolation=process `
--mount type=bind,source=G:\ContainerData,target=C:\Data `mcr.microsoft.com/powershell:windowsservercore-1903
With this, we can easily prove that our solution works and that the container state files are indeed stored in Azure Cloud!
- In the running container, create a file that contains data. For example, get a list of the currently running processes and store it as a processes.txt file:
Get-Process > C:\Data\processes.txt
- Now, log in to Azure Portal (https://portal.azure.com/) and do the following:
- Navigate to Storage accounts from the main menu.
- Open the dockerstorageaccount account.
- In the storage account menu, open Files under the File service group.
- Open the docker-bind-mount-share file share from the list.
You will see a familiar directory structure. Navigate into the ContainerData directory to see that the processes.txt file is indeed there and contains the data that was stored in the container:
Please note that this scenario can also be achieved with a regular SMB File Server hosted in your local network, which may be a suitable solution if you use them in your infrastructure already.
Congratulations! You have successfully created a Windows container that uses Azure Cloud storage to persist container state. In the next section, we will learn how to run MongoDB inside Windows containers as an example of a multi-container solution.
- MariaDB High Performance
- Building a Recommendation Engine with Scala
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- Mastering Drupal 8 Views
- Flutter跨平臺開發入門與實戰
- .NET 3.5編程
- C++面向對象程序設計習題解答與上機指導(第三版)
- 蘋果的產品設計之道:創建優秀產品、服務和用戶體驗的七個原則
- Go語言入門經典
- Instant Apache Camel Messaging System
- Flink入門與實戰
- 計算機應用基礎案例教程(第二版)
- Vue.js 3.x高效前端開發(視頻教學版)
- PhantomJS Cookbook
- Practical Linux Security Cookbook