我需要将数据磁盘附加到 VM(在 VMSS 中),并且喜欢立即格式化和使用磁盘而无需进一步手动干预。如何在 ARM 模板中直接实现这一点?
问问题
775 次
2 回答
1
我在 ARM 模板中添加了 3 个参数:
...
"scriptLocation": {
"type": "string",
"metadata": {
"description": "Location of custom extension scripts on storage account container"
}
},
"scriptStorageAccount": {
"type": "string",
"metadata": {
"description": "Name of custom extension scripts storage account"
}
},
"scriptStorageAccountKey": {
"type": "string",
"metadata": {
"description": "Key to custom extension scripts storage account"
}
},
...
这些参数填充在上传自定义扩展脚本文件并调用New-AzureRmResourceGroupDeployment
.
...
$StorageAccountName = "mydeploymentstorage"
$StorageContainerName = "ext"
$ArtifactStagingDirectory = ".\ExtensionScripts"
...
# transfer Extension script to Storage $StorageAccount = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -eq $StorageAccountName})
$StorageAccountContext = $StorageAccount.Context
New-AzureStorageContainer -Name $StorageContainerName -Context $StorageAccountContext -Permission Container -ErrorAction SilentlyContinue *>&1
$ArtifactFilePaths = Get-ChildItem $ArtifactStagingDirectory -Recurse -File | ForEach-Object -Process {$_.FullName}
foreach ($SourcePath in $ArtifactFilePaths) {
Write-Host "transfering" $SourcePath
$BlobName = $SourcePath.Substring($SourcePath.LastIndexOf("\")+1)
Set-AzureStorageBlobContent -File $SourcePath -Blob $BlobName -Container $StorageContainerName -Context $StorageAccountContext -Force -ErrorAction Stop
}
# prepare and pass script parameters
$DynamicParameters = New-Object -TypeName Hashtable
$DynamicParameters["scriptLocation"] = $StorageAccountContext.BlobEndPoint + $StorageContainerName
$DynamicParameters["scriptStorageAccount"] = $StorageAccountName
$DynamicParameters["scriptStorageAccountKey"] = ($StorageAccount | Get-AzureRmStorageAccountKey).Value[0]
...
# start deployment
New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) ` `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterFile $TemplateParametersFile `
@DynamicParameters `
-Verbose
在 VMSS 中extensionProfile
,我添加了自定义脚本扩展(将其与其他扩展放在一个地方):
...
"storageProfile": {
"imageReference": {
"publisher": "[parameters('vmImagePublisher')]",
"offer": "[parameters('vmImageOffer')]",
"sku": "[parameters('vmImageSku')]",
"version": "[parameters('vmImageVersion')]"
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage",
"managedDisk": {
"storageAccountType": "[parameters('storageAccountType')]"
}
},
"dataDisks": [
{
"diskSizeGB": 128,
"lun": 0,
"createOption": "Empty",
"managedDisk": {
"storageAccountType": "[parameters('storageAccountType')]"
}
}
]
}
...
"virtualMachineProfile": {
"extensionProfile": {
"extensions": [
...
{
"name": "[concat(parameters('vmNodeType0Name'),'_CreateDisk')]",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.9",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"[concat(parameters('scriptLocation'),'/CreateDisk.ps1')]"
]
},
"protectedSettings": {
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File CreateDisk.ps1",
"storageAccountName": "[parameters('scriptStorageAccount')]",
"storageAccountKey": "[parameters('scriptStorageAccountKey')]"
}
}
}
]
然后最后创建了脚本。我最初的问题是,我在 C: 上没有足够的空间让 ...-smalldisk VM SKU 保存所有 docker 映像,所以我搬到docker
了新驱动器。
# create and format disk
Get-Disk |
Where PartitionStyle -eq 'Raw' |
Select-Object -First 1 |
Initialize-Disk -PartitionStyle MBR -PassThru |
New-Partition -DriveLetter F -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "Containers" -Confirm:$false
# move docker to F:\docker
docker images -a -q | %{docker rmi $_ --force}
Stop-Service Docker
$service = (Get-Service Docker)
$service.WaitForStatus("Stopped","00:00:30")
@{"data-root"="F:\docker"} | ConvertTo-Json | Set-Content C:\programdata\docker\config\daemon.json
Get-Process docker* | % {Stop-Process -Id $_.Id -Force}
docker system info
Copy-Item C:\programdata\docker F:\docker -Recurse
Start-Service Docker
于 2018-07-07T05:10:22.083 回答
0
您可以在模板中使用指向脚本 lo的customscript对象
{
"type": "Microsoft.Compute/virtualMachineScaleSets/extensions",
"name": "[concat(variables('VmssName'),'/', variables('extensionName'))]",
"apiVersion": "2015-05-01-preview",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachineScaleSets/', variables('VmssName'))]"
],
"properties": {
"publisher": "Microsoft.Azure.Extensions",
"type": "CustomScript",
"typeHandlerVersion": "2.0",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"[parameters('BootScriptUri')]"
]
},
"protectedSettings": {
"commandToExecute": "[parameters('commandToExecute')]"
}
}
然后像这样的脚本
Get-Disk |
Where partitionstyle -eq 'raw' |
Initialize-Disk -PartitionStyle MBR -PassThru |
New-Partition -DriveLetter "F" -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "DataDisk" -Confirm:$false
有一个 linux 版本的脚本 - 有更多的功能!在https://github.com/Azure/azure-quickstart-templates/blob/master/shared_scripts/ubuntu/vm-disk-utils-0.1.sh
于 2018-07-06T08:08:03.500 回答