将自定义映像添加到 Azure 规模集模板Add a custom image to an Azure scale set template

本文介绍了如何修改基本规模集模板,以便通过自定义映像进行部署。This article shows how to modify the basic scale set template to deploy from custom image.

更改模板定义Change the template definition

此前的文章中,我们创建了基本的规模集模板。In a previous article we had created a basic scale set template. 我们现在将使用这个此前的模板并对其进行修改,以便创建一个模板来从自定义映像部署规模集。We will now use that earlier template and modify it to create a template that deploys a scale set from a custom image.

创建托管磁盘映像Creating a managed disk image

如果已有自定义的托管磁盘映像(Microsoft.Compute/images 类型的资源),则可以跳过此部分。If you already have a custom managed disk image (a resource of type Microsoft.Compute/images), then you can skip this section.

首先添加 sourceImageVhdUri 参数,它是 Azure 存储中通用 Blob 的 URI,包含要从其部署的自定义映像。First, add a sourceImageVhdUri parameter, which is the URI to the generalized blob in Azure Storage that contains the custom image to deploy from.

     },
     "adminPassword": {
       "type": "securestring"
+    },
+    "sourceImageVhdUri": {
+      "type": "string",
+      "metadata": {
+        "description": "The source of the generalized blob containing the custom image"
+      }
     }
   },
   "variables": {},

接下来,添加类型为 Microsoft.Compute/images 的资源,该资源是托管磁盘映像,基于 URI sourceImageVhdUri 处的通用 Blob。Next, add a resource of type Microsoft.Compute/images, which is the managed disk image based on the generalized blob located at URI sourceImageVhdUri. 该映像必须与使用它的规模集位于同一区域。This image must be in the same region as the scale set that uses it. 在映像属性中,指定操作系统类型、Blob 位置(通过 sourceImageVhdUri 参数)和存储帐户类型:In the properties of the image, specify the OS type, the location of the blob (from the sourceImageVhdUri parameter), and the storage account type:

   "resources": [
     {
+      "type": "Microsoft.Compute/images",
+      "apiVersion": "2019-03-01",
+      "name": "myCustomImage",
+      "location": "[resourceGroup().location]",
+      "properties": {
+        "storageProfile": {
+          "osDisk": {
+            "osType": "Linux",
+            "osState": "Generalized",
+            "blobUri": "[parameters('sourceImageVhdUri')]",
+            "storageAccountType": "Standard_LRS"
+          }
+        }
+      }
+    },
+    {
       "type": "Microsoft.Network/virtualNetworks",
       "name": "myVnet",
       "location": "[resourceGroup().location]",

在规模集资源中,添加引用自定义映像的 dependsOn 子句,确保在规模集尝试从该映像部署之前创建映像:In the scale set resource, add a dependsOn clause referring to the custom image to make sure the image gets created before the scale set tries to deploy from that image:

       "location": "[resourceGroup().location]",
       "apiVersion": "2019-03-01-preview",
       "dependsOn": [
-        "Microsoft.Network/virtualNetworks/myVnet"
+        "Microsoft.Network/virtualNetworks/myVnet",
+        "Microsoft.Compute/images/myCustomImage"
       ],
       "sku": {
         "name": "Standard_A1",

更改规模集属性,使用托管磁盘映像Changing scale set properties to use the managed disk image

在规模集 storageProfileimageReference 中,请勿指定平台映像的发布者、产品/服务、SKU 和版本,而是指定 Microsoft.Compute/images 资源的 idIn the imageReference of the scale set storageProfile, instead of specifying the publisher, offer, sku, and version of a platform image, specify the id of the Microsoft.Compute/images resource:

         "virtualMachineProfile": {
           "storageProfile": {
             "imageReference": {
-              "publisher": "Canonical",
-              "offer": "UbuntuServer",
-              "sku": "16.04-LTS",
-              "version": "latest"
+              "id": "[resourceId('Microsoft.Compute/images', 'myCustomImage')]"
             }
           },
           "osProfile": {

本示例中,使用 resourceId 函数获取在同一模板中创建的映像的资源 ID。In this example, use the resourceId function to get the resource ID of the image created in the same template. 如果事先创建了托管磁盘映像,应改为提供该映像的 ID。If you have created the managed disk image beforehand, you should provide the ID of that image instead. 此 ID 必须采用以下格式:/subscriptions/<subscription-id>resourceGroups/<resource-group-name>/providers/Microsoft.Compute/images/<image-name>This ID must be of the form: /subscriptions/<subscription-id>resourceGroups/<resource-group-name>/providers/Microsoft.Compute/images/<image-name>.

后续步骤Next Steps

可以按照 Azure Resource Manager 文档部署上述模板。You can deploy the preceding template by following the Azure Resource Manager documentation.

若要阅读这一系列的教程,可先阅读最小可行规模集模板文章You can start this tutorial series from the minimum viable scale set template article.

可以了解如何修改最小可行规模集模板,以便将规模集部署到现有的虚拟网络You can see how to modify the minimum viable scale set template to deploy the scale set into an existing virtual network.

可以了解如何修改最小可行规模集模板,并使用自定义映像部署规模集You can see how to modify the minimum viable scale set template to deploy the scale set with a custom image.

你可以了解如何修改最小可行规模集模板,以使用基于来宾的自动缩放部署 Linux 规模集You can see how to modify the minimum viable scale set template to deploy a Linux scale set with guest-based autoscale.

有关规模集的更多常规信息,请参阅规模集概述页For more general information about scale sets, refer to the scale set overview page.