将规模集模板转换为托管磁盘规模集模板Convert a scale set template to a managed disk scale set template

使用 Resource Manager 模板创建不使用托管的磁盘的规模集的客户可能希望改为使用托管的磁盘。Customers with a Resource Manager template for creating a scale set not using managed disk may wish to modify it to use managed disk. 本文演示如何使用托管磁盘,以 Azure 快速入门模板(适用于示例资源管理器模板的社区主导存储库)中的拉取请求为例。This article shows how to use managed disks, using as an example a pull request from the Azure Quickstart Templates, a community-driven repo for sample Resource Manager templates. 可在此处查看完整的拉取请求:https://github.com/Azure/azure-quickstart-templates/pull/2998,差异的相关部分如下,并进行了说明:The full pull request can be seen here: https://github.com/Azure/azure-quickstart-templates/pull/2998, and the relevant parts of the diff are below, along with explanations:

托管 OS 磁盘Making the OS disks managed

在以下差异部分中,已删除与存储帐户和磁盘属性相关的多个变量。In the following diff, several variables related to storage account and disk properties are removed. 不再需要存储帐户类型(Standard_LRS 是默认值),但可以指定它(如果需要)。Storage account type is no longer necessary (Standard_LRS is the default), but you could specify it if desired. 托管磁盘仅支持 Standard_LRS 和 Premium_LRS。Only Standard_LRS and Premium_LRS are supported with managed disk. 旧模板中使用了新存储帐户后缀、唯一的字符串数组和 sa 计数来生成存储帐户名称。New storage account suffix, unique string array, and sa count were used in the old template to generate storage account names. 新模板不再需要这些变量,因为托管磁盘代表客户自动创建存储帐户。These variables are no longer necessary in the new template because managed disk automatically creates storage accounts on the customer's behalf. 同样,vhd 容器名称和 OS 磁盘名称也不再需要,因为托管磁盘会自动命名基础存储 blob 容器和磁盘。Similarly, vhd container name and OS disk name are no longer necessary because managed disk automatically names the underlying storage blob containers and disks.

   "variables": {
-    "storageAccountType": "Standard_LRS",
     "namingInfix": "[toLower(substring(concat(parameters('vmssName'), uniqueString(resourceGroup().id)), 0, 9))]",
     "longNamingInfix": "[toLower(parameters('vmssName'))]",
-    "newStorageAccountSuffix": "[concat(variables('namingInfix'), 'sa')]",
-    "uniqueStringArray": [
-      "[concat(uniqueString(concat(resourceGroup().id, variables('newStorageAccountSuffix'), '0')))]",
-      "[concat(uniqueString(concat(resourceGroup().id, variables('newStorageAccountSuffix'), '1')))]",
-      "[concat(uniqueString(concat(resourceGroup().id, variables('newStorageAccountSuffix'), '2')))]",
-      "[concat(uniqueString(concat(resourceGroup().id, variables('newStorageAccountSuffix'), '3')))]",
-      "[concat(uniqueString(concat(resourceGroup().id, variables('newStorageAccountSuffix'), '4')))]"
-    ],
-    "saCount": "[length(variables('uniqueStringArray'))]",
-    "vhdContainerName": "[concat(variables('namingInfix'), 'vhd')]",
-    "osDiskName": "[concat(variables('namingInfix'), 'osdisk')]",
     "addressPrefix": "10.0.0.0/16",
     "subnetPrefix": "10.0.0.0/24",
     "virtualNetworkName": "[concat(variables('namingInfix'), 'vnet')]",

在以下差异部分中,已将计算 API 更新到版本 2016-04-30-preview,该版本是支持带规模集的托管磁盘所需的最早版本。In the following diff, you compute API is updated to version 2016-04-30-preview, which is the earliest required version for managed disk support with scale sets. 可以在使用旧语法的新 API 版本中使用非托管磁盘(如果需要)。You could use unmanaged disks in the new API version with the old syntax if desired. 如果只更新计算 API 版本,而不更改任何其他内容,则模板应像以前那样继续工作。If you only update the compute API version and don't change anything else, the template should continue to work as before.

@@ -86,7 +74,7 @@
       "version": "latest"
     },
     "imageReference": "[variables('osType')]",
-    "computeApiVersion": "2016-03-30",
+    "computeApiVersion": "2016-04-30-preview",
     "networkApiVersion": "2016-03-30",
     "storageApiVersion": "2015-06-15"
   },

在以下差异部分中,已将存储帐户资源从资源数组中完全删除。In the following diff, the storage account resource is removed from the resources array completely. 不再需要该资源,因为托管磁盘会自动创建它们。The resource is no longer needed as managed disk creates them automatically.

@@ -113,19 +101,6 @@
       }
     },
-    {
-      "type": "Microsoft.Storage/storageAccounts",
-      "name": "[concat(variables('uniqueStringArray')[copyIndex()], variables('newStorageAccountSuffix'))]",
-      "location": "[resourceGroup().location]",
-      "apiVersion": "[variables('storageApiVersion')]",
-      "copy": {
-        "name": "storageLoop",
-        "count": "[variables('saCount')]"
-      },
-      "properties": {
-        "accountType": "[variables('storageAccountType')]"
-      }
-    },
     {
       "type": "Microsoft.Network/publicIPAddresses",
       "name": "[variables('publicIPAddressName')]",
       "location": "[resourceGroup().location]",

在以下差异部分中,我们可以看到,我们要删除从规模集引用创建存储帐户的循环的 depends on 子句。In the following diff, we can see that we are removing the depends on clause referring from the scale set to the loop that was creating storage accounts. 在旧的模板中,这可以确保在开始创建规模集之前已创建存储帐户,但是托管磁盘不再需要该子句。In the old template, this was ensuring that the storage accounts were created before the scale set began creation, but this clause is no longer necessary with managed disk. 还会删除 vhd 容器属性和 OS 磁盘名称属性,因为托管磁盘会在后台自动处理这些属性。The vhd containers property is also removed, along with the OS disk name property as these properties are automatically handled under the hood by managed disk. 如果想要高级 OS 磁盘,可以在“osDisk”配置中添加 "managedDisk": { "storageAccountType": "Premium_LRS" }You could add "managedDisk": { "storageAccountType": "Premium_LRS" } in the "osDisk" configuration if you wanted premium OS disks. 只有在 VM sku 中包含大写或小写的“s”的 VM 才可以使用高级磁盘。Only VMs with an uppercase or lowercase 's' in the VM sku can use premium disks.

@@ -183,7 +158,6 @@
       "location": "[resourceGroup().location]",
       "apiVersion": "[variables('computeApiVersion')]",
       "dependsOn": [
-        "storageLoop",
         "[concat('Microsoft.Network/loadBalancers/', variables('loadBalancerName'))]",
         "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
       ],
@@ -200,16 +174,8 @@
         "virtualMachineProfile": {
           "storageProfile": {
             "osDisk": {
-              "vhdContainers": [
-                "[concat('https://', variables('uniqueStringArray')[0], variables('newStorageAccountSuffix'), '.blob.core.chinacloudapi.cn/', variables('vhdContainerName'))]",
-                "[concat('https://', variables('uniqueStringArray')[1], variables('newStorageAccountSuffix'), '.blob.core.chinacloudapi.cn/', variables('vhdContainerName'))]",
-                "[concat('https://', variables('uniqueStringArray')[2], variables('newStorageAccountSuffix'), '.blob.core.chinacloudapi.cn/', variables('vhdContainerName'))]",
-                "[concat('https://', variables('uniqueStringArray')[3], variables('newStorageAccountSuffix'), '.blob.core.chinacloudapi.cn/', variables('vhdContainerName'))]",
-                "[concat('https://', variables('uniqueStringArray')[4], variables('newStorageAccountSuffix'), '.blob.core.chinacloudapi.cn/', variables('vhdContainerName'))]"
-              ],
-              "name": "[variables('osDiskName')]",
             },
             "imageReference": "[variables('imageReference')]"
           },

规模集配置中不存在是使用托管磁盘还是非托管磁盘的显式属性。There is no explicit property in the scale set configuration for whether to use managed or unmanaged disk. 根据存储配置文件中显示的属性,规模集知道使用哪种磁盘。The scale set knows which to use based on the properties that are present in the storage profile. 因此,务必在修改模板时确保规模集的存储配置文件中具有正确的属性。Thus, it is important when modifying the template to ensure that the right properties are in the storage profile of the scale set.

数据磁盘数Data disks

进行上述更改后,规模集对 OS 磁盘使用托管磁盘,但是如果要使用数据磁盘呢?With the changes above, the scale set uses managed disks for the OS disk, but what about data disks? 若要添加数据磁盘,请在与“osDisk”同一级别的“storageProfile”下添加“dataDisks”属性。To add data disks, add the "dataDisks" property under "storageProfile" at the same level as "osDisk". 该属性的值是 JSON 对象列表,其中每个对象均具有属性“lun”(必须对于 VM 上的每个数据磁盘唯一)、“createOption”(“empty”是当前唯一支持的选项)和“diskSizeGB”(磁盘的大小,以 GB 为单位;必须大于 0 且小于 1024), 如以下示例所示:The value of the property is a JSON list of objects, each of which has properties "lun" (which must be unique per data disk on a VM), "createOption" ("empty" is currently the only supported option), and "diskSizeGB" (the size of the disk in gigabytes; must be greater than 0 and less than 1024) as in the following example:

"dataDisks": [
  {
    "lun": "1",
    "createOption": "empty",
    "diskSizeGB": "1023"
  }
]

如果在此数组中指定 n 个磁盘,则规模集中的每个 VM 将获得 n 个数据磁盘。If you specify n disks in this array, each VM in the scale set gets n data disks. 但是,请注意,这些数据磁盘都是原始设备。Do note, however, that these data disks are raw devices. 它们未经格式化。They are not formatted. 在使用这些磁盘之前,由客户负责附加它们并对其进行分区和格式化。It is up to the customer to attach, partition, and format the disks before using them. (可选)还可以在每个数据磁盘对象中指定 "managedDisk": { "storageAccountType": "Premium_LRS" },以指定它应是高级数据磁盘。Optionally, you could also specify "managedDisk": { "storageAccountType": "Premium_LRS" } in each data disk object to specify that it should be a premium data disk. 只有在 VM sku 中包含大写或小写的“s”的 VM 才可以使用高级磁盘。Only VMs with an uppercase or lowercase 's' in the VM sku can use premium disks.

若要详细了解如何在规模集中使用数据磁盘,请参阅此文To learn more about using data disks with scale sets, see this article.

后续步骤Next steps

有关使用规模集的示例 Resource Manager 模板,请在 Azure 快速入门模板 github 存储库中搜索“vmss”。For example Resource Manager templates using scale sets, search for "vmss" in the Azure Quickstart Templates github repo.

有关一般信息,请参阅规模集的主要登陆页For general information, check out the main landing page for scale sets.