使用 VM 托管磁盘模板Use VM managed disks templates

本文介绍使用 Azure 资源管理器模板在 Azure Stack Hub 中预配虚拟机时托管与非托管磁盘之间的差异。This article describes the differences between managed and unmanaged disks when using Azure Resource Manager templates to provision virtual machines in Azure Stack Hub. 这些示例可帮助你将使用非托管磁盘的现有模板转换为托管磁盘。The examples help you to convert existing templates that use unmanaged disks to managed disks.

非托管磁盘模板的格式设置Unmanaged disks template formatting

在开始之前,我们先了解一下非托管磁盘的部署方式。To begin, let's take a look at how unmanaged disks are deployed. 创建非托管磁盘时,需要一个用于保存 VHD 文件的存储帐户。When creating unmanaged disks, you need a storage account to hold the VHD files. 可以创建新存储帐户,或使用现有的存储帐户。You can create a new storage account or use one that already exists. 在模板的资源块中创建新存储帐户资源,如下所示:Create a new storage account resource in the resources block of the template, as follows:

{
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2017-10-01",
    "name": "[variables('storageAccountName')]",
    "location": "[resourceGroup().location]",
    "sku": {
        "name": "Standard_LRS"
    },
    "kind": "Storage"
}

在虚拟机对象中,在存储帐户中添加一个依赖项才能确保先创建存储帐户,再创建虚拟机。Within the virtual machine object, add a dependency on the storage account to ensure that it's created before the virtual machine. 然后,在 storageProfile 节中,指定 VHD 位置的完整 URI,该 URI 引用存储帐户,并且 OS 磁盘和任何数据磁盘都需要它。Within the storageProfile section, specify the full URI of the VHD location, which references the storage account and is needed for the OS disk and any data disks. 以下示例从映像创建一个 OS 磁盘,并创建一个大小为 1023GB 的空数据磁盘:The following example creates one OS disk from image and one empty data disk with a 1023GB size:

{
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2017-12-01",
    "name": "[variables('vmName')]",
    "location": "[resourceGroup().location]",
    "dependsOn": [
    "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
    "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
    ],
    "properties": {
        "hardwareProfile": {...},
        "osProfile": {...},
        "storageProfile": {
            "imageReference": {
                "publisher": "MicrosoftWindowsServer",
                "offer": "WindowsServer",
                "sku": "[parameters('windowsOSVersion')]",
                "version": "latest"
            },
            "osDisk": {
                "name": "osdisk",
                "vhd": {
                    "uri": "[concat(reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob, 'vhds/osdisk.vhd')]"
                },
                "caching": "ReadWrite",
                "createOption": "FromImage"
            },
            "dataDisks": [
                {
                    "name": "datadisk1",
                    "diskSizeGB": 1023,
                    "lun": 0,
                    "vhd": {
                        "uri": "[concat(reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob, 'vhds/datadisk1.vhd')]"
                    },
                    "createOption": "Empty"
                }
            ]
        },
        "networkProfile": {...},
        "diagnosticsProfile": {...}
    }
}

托管磁盘模板的格式Managed disks template formatting

若使用 Azure 托管磁盘,磁盘将成为顶级资源,不再需要用户创建存储帐户。With Azure managed disks, the disk becomes a top-level resource and no longer requires a storage account to be created by the user. 托管磁盘是在 2017-03-30 API 版本中首次引入的。Managed disks were first introduced in the 2017-03-30 API version. 以下各部分将讲解默认设置,并说明如何进一步自定义磁盘。The following sections walk through the default settings and explain how to further customize your disks.

默认的托管磁盘设置Default managed disk settings

若要使用托管磁盘创建 VM,则不再需要创建存储帐户资源。To create a VM with managed disks, you no longer need to create the storage account resource. 在下面的模板示例中,与前面的非托管磁盘示例有些不同:In the template example below, there are some differences from the previous unmanaged disk examples:

  • apiVersion 是支持托管磁盘的“virtualMachines”资源类型的版本。The apiVersion is a version for a "virtualMachines" resource type which supports managed disks.
  • osDiskdataDisks 不再引用 VHD 的特定 URI。osDisk and dataDisks no longer refer to a specific URI for the VHD.
  • 如果部署时未指定其他属性,磁盘会根据 VM 大小使用某个存储类型。When deploying without specifying additional properties, the disk uses a storage type based on the size of the VM. 例如,如果你使用的是支持高级存储的 VM 大小(其名称中包含“s”的大小,例如 Standard_DS2_v2),则默认情况下将配置高级磁盘。For example, if you are using a VM size that supports premium storage (sizes with "s" in their name such as Standard_DS2_v2), then premium disks will be configured by default. 若要更改此行为,可以使用磁盘的 SKU 设置来指定存储类型。You can change this by using the sku setting of the disk to specify a storage type.
  • 如果没有为磁盘指定名称,则 OS 磁盘采用格式 <VMName>_OsDisk_1_<randomstring>,每个数据磁盘采用格式 <VMName>_disk<#>_<randomstring>If no name for the disk is specified, it takes the format of <VMName>_OsDisk_1_<randomstring> for the OS disk and <VMName>_disk<#>_<randomstring> for each data disk.
    • 如果正在基于自定义映像创建 VM,则会从在自定义映像资源中定义的磁盘属性中检索存储帐户类型和磁盘名称的默认设置。If a VM is being created from a custom image, then the default settings for storage account type and disk name are retrieved from the disk properties defined in the custom image resource. 可以通过在模板中指定这些值来替代它们。These can be overridden by specifying values for these in the template.
  • 默认情况下,磁盘缓存对于 OS 磁盘为“读/写”,对于数据磁盘则为“无” 。By default, disk caching is read/write for the OS disk and None for data disks.
  • 在下面的示例中,仍然存在一个存储帐户依赖项,但这仅用于诊断的存储,磁盘存储并不需要:In the following example, there is still a storage account dependency, though this is only for storage of diagnostics and is not needed for disk storage:
{
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2017-12-01",
    "name": "[variables('vmName')]",
    "location": "[resourceGroup().location]",
    "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
    ],
    "properties": {
        "hardwareProfile": {...},
        "osProfile": {...},
        "storageProfile": {
            "imageReference": {
                "publisher": "MicrosoftWindowsServer",
                "offer": "WindowsServer",
                "sku": "[parameters('windowsOSVersion')]",
                "version": "latest"
            },
            "osDisk": {
                "createOption": "FromImage"
            },
            "dataDisks": [
                {
                    "diskSizeGB": 1023,
                    "lun": 0,
                    "createOption": "Empty"
                }
            ]
        },
        "networkProfile": {...},
        "diagnosticsProfile": {...}
    }
}

使用顶级托管磁盘资源Use a top-level managed disk resource

在虚拟机对象中指定磁盘配置的一种替代方法是创建一个顶级磁盘资源,并在创建虚拟机的过程中附加该资源。As an alternative to specifying the disk configuration in the virtual machine object, you can create a top-level disk resource and attach it as part of the virtual machine creation. 请务必将 2017-03-30 用作 disks 资源 API 版本。Be sure to use 2017-03-30 as the disks resource API version. 例如,可按如下所示创建一个用作数据磁盘的磁盘资源。For example, you can create a disk resource as follows to use as a data disk. 在此示例中,vmName 用作磁盘名称的一部分:In this example, vmName is used as part of the disk name:

{
    "type": "Microsoft.Compute/disks",
    "apiVersion": "2017-03-30",
    "name": "[concat(variables('vmName'),'-datadisk1')]",
    "location": "[resourceGroup().location]",
    "sku": {
        "name": "Standard_LRS"
    },
    "properties": {
        "creationData": {
            "createOption": "Empty"
        },
        "diskSizeGB": 1023
    }
}

在 VM 对象中,引用要附加的磁盘对象。Within the VM object, reference the disk object to be attached. 指定在 managedDisk 属性中创建的托管磁盘的资源 ID 可以在创建 VM 时附加该磁盘。Specifying the resource ID of the managed disk created in the managedDisk property allows the attachment of the disk as the VM is created. 该 VM 资源的 apiVersion 设置为 2017-12-01The apiVersion for the VM resource is set to 2017-12-01. 在磁盘资源中添加了一个依赖项,以确保在创建 VM 之前成功创建该磁盘资源:A dependency on the disk resource is added to ensure it's successfully created before VM creation:

{
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2017-12-01",
    "name": "[variables('vmName')]",
    "location": "[resourceGroup().location]",
    "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]",
        "[resourceId('Microsoft.Compute/disks/', concat(variables('vmName'),'-datadisk1'))]"
    ],
    "properties": {
        "hardwareProfile": {...},
        "osProfile": {...},
        "storageProfile": {
            "imageReference": {
                "publisher": "MicrosoftWindowsServer",
                "offer": "WindowsServer",
                "sku": "[parameters('windowsOSVersion')]",
                "version": "latest"
            },
            "osDisk": {
                "createOption": "FromImage"
            },
            "dataDisks": [
                {
                    "lun": 0,
                    "name": "[concat(variables('vmName'),'-datadisk1')]",
                    "createOption": "attach",
                    "managedDisk": {
                        "id": "[resourceId('Microsoft.Compute/disks/', concat(variables('vmName'),'-datadisk1'))]"
                    }
                }
            ]
        },
        "networkProfile": {...},
        "diagnosticsProfile": {...}
    }
}

使用托管磁盘创建包含 VM 的托管可用性集Create managed availability sets with VMs using managed disks

若要使用托管磁盘创建包含 VM 的托管可用性集,请将 sku 对象添加到可用性集资源,并将 name 属性设置为 AlignedTo create managed availability sets with VMs using managed disks, add the sku object to the availability set resource and set the name property to Aligned. 该属性可确保每个 VM 的磁盘彼此充分隔离,避免发生单点故障。This property ensures that the disks for each VM are sufficiently isolated from each other to avoid single points of failure. 另请注意,可用性集资源的 apiVersion 设置为 2017-12-01Also note that the apiVersion for the availability set resource is set to 2017-12-01:

{
    "type": "Microsoft.Compute/availabilitySets",
    "apiVersion": "2017-12-01",
    "location": "[resourceGroup().location]",
    "name": "[variables('avSetName')]",
    "properties": {
        "PlatformUpdateDomainCount": 1,
        "PlatformFaultDomainCount": 2
    },
    "sku": {
        "name": "Aligned"
    }
}

后续步骤Next steps