在 Azure 资源管理器模板中使用托管磁盘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.

Note

建议使用 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 and can update your virtual machine resource as follows. 特别要注意,apiVersion 反映 2017-03-30,并且 osDiskdataDisks 不再为 VHD 引用特定 URI。Specifically note that the apiVersion reflects 2017-03-30 and the 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),则系统将使用 Premium_LRS 存储。For example, if you are using a Premium capable VM Size (sizes with "s" in their name such as Standard_D2s_v3) then system will use Premium_LRS storage. 使用磁盘的 SKU 设置指定存储类型。Use the sku setting of the disk to specify a Storage type. 如果未指定任何名称,则 OS 磁盘采用格式 <VMName>_OsDisk_1_<randomstring>,每个数据磁盘采用格式 <VMName>_disk<#>_<randomstring>If no name is specified, it takes the format of <VMName>_OsDisk_1_<randomstring> for the OS disk and <VMName>_disk<#>_<randomstring> for each data disk. 默认情况下,Azure 磁盘加密处于禁用状态;缓存对于 OS 磁盘为“读/写”,对于数据磁盘则为“无”。By default, Azure disk encryption is disabled; caching is Read/Write for the OS disk and None for data disks. 你可能会注意到以下示例中仍然存在一个存储帐户依赖项,但这仅用于诊断的存储,磁盘存储并不需要。You may notice 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_LRS Specify 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