您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Azure 规模集模板中添加对现有虚拟网络的引用Add reference to an existing virtual network in an Azure scale set template

本文介绍了如何修改基本规模集模板,以便将其部署到现有虚拟网络而非创建新的虚拟网络。This article shows how to modify the basic scale set template to deploy into an existing virtual network instead of creating a new one.

更改模板定义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 into an existing virtual network.

首先,添加一个 subnetId 参数。First, add a subnetId parameter. 此字符串将传递到规模集配置,使得规模集能够识别要将虚拟机部署到的预先创建的子网。This string is passed into the scale set configuration, allowing the scale set to identify the pre-created subnet to deploy virtual machines into. 此字符串必须采用以下格式:/subscriptions/<subscription-id>resourceGroups/<resource-group-name>/providers/Microsoft.Network/virtualNetworks/<virtual-network-name>/subnets/<subnet-name>This string must be of the form: /subscriptions/<subscription-id>resourceGroups/<resource-group-name>/providers/Microsoft.Network/virtualNetworks/<virtual-network-name>/subnets/<subnet-name>

例如,要将规模集部署到具有名称 myvnet、子网 mysubnet、资源组 myrg 和订阅 00000000-0000-0000-0000-000000000000 的现有虚拟网络,则 subnetId 将是:/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myrg/providers/Microsoft.Network/virtualNetworks/myvnet/subnets/mysubnetFor instance, to deploy the scale set into an existing virtual network with name myvnet, subnet mysubnet, resource group myrg, and subscription 00000000-0000-0000-0000-000000000000, the subnetId would be: /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myrg/providers/Microsoft.Network/virtualNetworks/myvnet/subnets/mysubnet.

     },
     "adminPassword": {
       "type": "securestring"
+    },
+    "subnetId": {
+      "type": "string"
     }
   },

接下来,从 resources 阵列中删除虚拟网络资源,由于使用现有虚拟网络,因此不需要部署新的虚拟网络。Next, delete the virtual network resource from the resources array, as you use an existing virtual network and don't need to deploy a new one.

   "variables": {},
   "resources": [
-    {
-      "type": "Microsoft.Network/virtualNetworks",
-      "name": "myVnet",
-      "location": "[resourceGroup().location]",
-      "apiVersion": "2018-11-01",
-      "properties": {
-        "addressSpace": {
-          "addressPrefixes": [
-            "10.0.0.0/16"
-          ]
-        },
-        "subnets": [
-          {
-            "name": "mySubnet",
-            "properties": {
-              "addressPrefix": "10.0.0.0/16"
-            }
-          }
-        ]
-      }
-    },

虚拟网络在部署模板前已存在,因此不需要指定从规模集到虚拟网络的 dependsOn 子句。The virtual network already exists before the template is deployed, so there is no need to specify a dependsOn clause from the scale set to the virtual network. 删除以下行:Delete the following lines:

     {
       "type": "Microsoft.Compute/virtualMachineScaleSets",
       "name": "myScaleSet",
       "location": "[resourceGroup().location]",
       "apiVersion": "2019-03-01",
-      "dependsOn": [
-        "Microsoft.Network/virtualNetworks/myVnet"
-      ],
       "sku": {
         "name": "Standard_A1",
         "capacity": 2

最后,传入用户设置的 subnetId 参数(而非使用 resourceId 获取同一部署中某个 vnet 的 ID,这是基本可行规模集模板执行的操作)。Finally, pass in the subnetId parameter set by the user (instead of using resourceId to get the ID of a vnet in the same deployment, which is what the basic viable scale set template does).

                       "name": "myIpConfig",
                       "properties": {
                         "subnet": {
-                          "id": "[concat(resourceId('Microsoft.Network/virtualNetworks', 'myVnet'), '/subnets/mySubnet')]"
+                          "id": "[parameters('subnetId')]"
                         }
                       }
                     }

后续步骤Next steps

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

你可以开始从本教程系列基本的规模集模板文章You can start this tutorial series from the basic scale set template article.

您可以了解如何修改基本的规模集模板部署规模集到现有虚拟网络You can see how to modify the basic scale set template to deploy the scale set into an existing virtual network.

您可以了解如何修改基本的规模集模板部署规模集使用自定义映像You can see how to modify the basic scale set template to deploy the scale set with a custom image.

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

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