在 Azure Resource Manager 模板中使用托管磁盘Using Managed Disks in Azure Resource Manager Templates

本文档逐步讲解在使用 Azure 资源管理器模板预配虚拟机时托管磁盘与非托管磁盘之间的差异。This document walks through the differences between managed and unmanaged disks when using Azure Resource Manager templates to provision virtual machines. 该示例可以帮助你将使用非托管磁盘的现有模板更新为托管磁盘。The examples help you to update existing templates that are using unmanaged Disks to managed disks. 为方便参考,我们将使用 101-vm-simple-windows 模板作为指导。For reference, we are using the 101-vm-simple-windows template as a guide. 你可以查看使用托管磁盘的模板,以及使用非托管磁盘的早期版本,因此可以根据需要直接对两者进行比较。You can see the template using both managed Disks and a prior version using unmanaged disks if you'd like to directly compare them.

非托管磁盘模板的格式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. 本文说明如何创建新存储帐户。This article shows you how to create a new storage account. 在资源块中创建一个如下所示的存储帐户资源。Create a storage account resource in the resources block as shown below.

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

在虚拟机对象中,在存储帐户中添加一个依赖项才能确保先创建存储帐户,再创建虚拟机。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.

{
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2018-10-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. 托管磁盘在 2016-04-30-preview API 版本中首次公开,并在所有后续 API 版本中可用,现在是默认磁盘类型。Managed disks were first exposed in the 2016-04-30-preview API version, they are available in all subsequent API versions and are now the default disk type. 以下部分逐步讲解默认设置,并详细说明如何进一步自定义磁盘。The following sections walk through the default settings and detail how to further customize your disks.

备注

建议使用 2016-04-30-preview 以后的 API 版本,因为在 2016-04-30-preview2017-03-30 之间存在重大更改。It is recommended to use an API version later than 2016-04-30-preview as there were breaking changes between 2016-04-30-preview and 2017-03-30.

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

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

  • apiVersion 是一个支持托管磁盘的版本。The apiVersion is a version that 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 will use a storage type based on the size of the VM. 例如,如果你使用的是支持高级存储的 VM 大小(其名称中包含“s”的大小,例如 Standard_D2s_v3),则默认情况下将配置高级磁盘。For example, if you are using a VM size that supports premium storage (sizes with "s" in their name such as Standard_D2s_v3) 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.
  • 默认情况下,Azure 磁盘加密处于禁用状态。By default, Azure disk encryption is disabled.
  • 默认情况下,缓存对于 OS 磁盘为“读/写”,对于数据磁盘则为“无”。By default, disk caching is Read/Write for the OS disk and None for data disks.
  • 下面的示例中仍然存在一个存储帐户依赖项,但这仅用于诊断的存储,磁盘存储并不需要。In the example below 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": "2018-10-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": {...}
    }
}

使用顶级托管磁盘资源Using 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. 例如,可按如下所示创建一个用作数据磁盘的磁盘资源。For example, you can create a disk resource as follows to use as a data disk.

{
    "type": "Microsoft.Compute/disks",
    "apiVersion": "2018-06-01",
    "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-03-30The apiVersion for the VM resource is set to 2017-03-30. 在磁盘资源中添加了一个依赖项,以确保在创建 VM 之前成功创建该磁盘资源。A dependency on the disk resource is added to ensure it's successfully created before VM creation.

{
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2018-10-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 设置为 2018-10-01Also note that the apiVersion for the availability set resource is set to 2018-10-01.

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

标准 SSD 盘Standard SSD disks

以下为创建标准 SSD 盘时资源管理器模板中所需的参数:Below are the parameters needed in the Resource Manager template to create Standard SSD Disks:

  • Microsoft.Compute 的 apiVersion 必须设置为 2018-04-01(或更高)apiVersion for Microsoft.Compute must be set as 2018-04-01 (or later)
  • 将 managedDisk.storageAccountType 指定为 StandardSSD_LRSSpecify managedDisk.storageAccountType as StandardSSD_LRS

以下示例显示了使用标准 SSD 盘的 VM 的 properties.storageProfile.osDisk 部分 :The following example shows the properties.storageProfile.osDisk section for a VM that uses Standard SSD Disks:

"osDisk": {
    "osType": "Windows",
    "name": "myOsDisk",
    "caching": "ReadWrite",
    "createOption": "FromImage",
    "managedDisk": {
        "storageAccountType": "StandardSSD_LRS"
    }
}

有关如何使用模板创建标准 SSD 盘的完整模板示例,请参阅使用标准 SSD 数据磁盘从 Windows 映像创建 VMFor a complete template example of how to create a Standard SSD disk with a template, see Create a VM from a Windows Image with Standard SSD Data Disks.

其他方案和自定义Additional scenarios and customizations

若要查找有关 REST API 规范的完整信息,请查看有关创建托管磁盘 REST API 的文档To find full information on the REST API specifications, please review the create a managed disk REST API documentation. 其中介绍了其他方案,以及可以通过模板部署提交到 API 的默认值和可接受值。You will find additional scenarios, as well as default and acceptable values that can be submitted to the API through template deployments.

后续步骤Next steps