了解虚拟机规模集模板Learn about virtual machine scale set templates

Azure Resource Manager 模板是部署成组的相关资源的好办法。Azure Resource Manager templates are a great way to deploy groups of related resources. 本系列教程演示如何创建基本规模集模板,以及如何修改此模板以满足各种场景。This tutorial series shows how to create a basic scale set template and how to modify this template to suit various scenarios. 所有示例都来自此 GitHub 存储库All examples come from this GitHub repository.

此模板简单易用。This template is intended to be simple. 有关更完整的规模集模板的示例,请参阅 Azure 快速入门模板 GitHub 存储库,并搜索包含字符串 vmss 的文件夹。For more complete examples of scale set templates, see the Azure Quickstart Templates GitHub repository and search for folders that contain the string vmss.

如果已能够熟练创建模板,可以跳到“后续步骤”部分,查看如何修改此模板。If you are already familiar with creating templates, you can skip to the "Next steps" section to see how to modify this template.

定义 $schema 和 contentVersionDefine $schema and contentVersion

首先,定义模板中的 $schemacontentVersionFirst, define $schema and contentVersion in the template. $schema 元素定义模板语言的版本,用于 Visual Studio 语法突出显示和类似的验证功能。The $schema element defines the version of the template language and is used for Visual Studio syntax highlighting and similar validation features. Azure 不使用 contentVersion 元素。The contentVersion element is not used by Azure. 而是帮助跟踪模板版本。Instead, it helps you keep track of the template version.

{
  "$schema": "https://schema.management.chinacloudapi.cn/schemas/2015-01-01/deploymentTemplate.json",
  "contentVersion": "1.0.0.0",

定义参数Define parameters

接下来,定义两个参数:adminUsernameadminPasswordNext, define two parameters, adminUsername and adminPassword. 参数是部署时指定的值。Parameters are values you specify at the time of deployment. adminUsername 参数只是一个 string 类型,但是由于 adminPassword 是一个机密,因此将其类型指定为 securestringThe adminUsername parameter is simply a string type, but because adminPassword is a secret, give it type securestring. 稍后,要将这些参数传递到规模集配置。Later, these parameters are passed into the scale set configuration.

  "parameters": {
    "adminUsername": {
      "type": "string"
    },
    "adminPassword": {
      "type": "securestring"
    }
  },

定义变量Define variables

Resource Manager 模板还可用于定义稍后要在模板中使用的变量。Resource Manager templates also let you define variables to be used later in the template. 本示例不使用任何变量,因此 JSON 对象为空。The example doesn't use any variables, so the JSON object is empty.

  "variables": {},

定义资源Define resources

接下来是模板的资源部分。Next is the resources section of the template. 在此处定义实际要部署的内容。Here, you define what you actually want to deploy. parametersvariables(JSON 对象)不同,resources 是 JSON 对象的 JSON 列表。Unlike parameters and variables (which are JSON objects), resources is a JSON list of JSON objects.

   "resources": [

所有资源都需要 typenameapiVersionlocation 属性。All resources require type, name, apiVersion, and location properties. 此示例的第一个资源具有类型 Microsoft.Network/virtualNetwork、名称 myVnet 和 apiVersion 2018-11-01This example's first resource has type Microsoft.Network/virtualNetwork, name myVnet, and apiVersion 2018-11-01. (若要查找资源类型的最新 API 版本,请参阅 Azure 资源管理器模板参考。)(To find the latest API version for a resource type, see the Azure Resource Manager template reference.)

     {
       "type": "Microsoft.Network/virtualNetworks",
       "name": "myVnet",
       "apiVersion": "2018-11-01",

指定位置Specify location

若要指定虚拟网络的位置,请使用资源管理器模板函数To specify the location for the virtual network, use a Resource Manager template function. 必须将此函数括在引号和方括号中,如下所示:"[<template-function>]"This function must be enclosed in quotes and square brackets like this: "[<template-function>]". 在本例中,使用 resourceGroup 函数。In this case, use the resourceGroup function. 该函数不使用任何参数,返回 JSON 对象以及有关要将部署部署到的资源组的元数据。It takes in no arguments and returns a JSON object with metadata about the resource group this deployment is being deployed to. 资源组在部署时由用户进行设置。The resource group is set by the user at the time of deployment. 然后此值会通过 .location 编入到该 JSON 对象的索引中,以便从该 JSON 对象中获取位置。This value is then indexed into this JSON object with .location to get the location from the JSON object.

       "location": "[resourceGroup().location]",

指定虚拟网络属性Specify virtual network properties

每个 Resource Manager 资源都有自己的针对特定于的资源的配置的 properties 部分。Each Resource Manager resource has its own properties section for configurations specific to the resource. 在本示例中,使用专用 IP 地址范围 10.0.0.0/16 指定虚拟网络应具有一个子网。In this case, specify that the virtual network should have one subnet using the private IP address range 10.0.0.0/16. 规模集始终包含在一个子网中。A scale set is always contained within one subnet. 它不能跨子网。It cannot span subnets.

       "properties": {
         "addressSpace": {
           "addressPrefixes": [
             "10.0.0.0/16"
           ]
         },
         "subnets": [
           {
             "name": "mySubnet",
             "properties": {
               "addressPrefix": "10.0.0.0/16"
             }
           }
         ]
       }
     },

添加 dependsOn 列表Add dependsOn list

除了所需的 typenameapiVersionlocation 属性以外,每个资源还可以包含字符串的可选 dependsOn 列表。In addition to the required type, name, apiVersion, and location properties, each resource can have an optional dependsOn list of strings. 此列表指定在部署此资源之前必须先完成此部署中的其他哪些资源。This list specifies which other resources from this deployment must finish before deploying this resource.

在本例中,列表只包含一个元素,即上一示例中所述的虚拟网络。In this case, there is only one element in the list, the virtual network from the previous example. 指定此依赖项的原因是在创建任何 VM 之前规模集需要有网络。You specify this dependency because the scale set needs the network to exist before creating any VMs. 这样,规模集可为这些虚拟机提供前面在网络属性中指定的 IP 地址范围中的专用 IP 地址。This way, the scale set can give these VMs private IP addresses from the IP address range previously specified in the network properties. dependsOn 列表中每个字符串的格式为 <type>/<name>The format of each string in the dependsOn list is <type>/<name>. 使用前面在虚拟网络资源定义中所用的 typenameUse the same type and name used previously in the virtual network resource definition.

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

指定规模集属性Specify scale set properties

规模集提供许多用于在规模集中自定义 VM 的属性。Scale sets have many properties for customizing the VMs in the scale set. 有关这些属性的完整列表,请参阅模板参考For a full list of these properties, see the template reference. 在本教程中,仅设置一些常用属性。For this tutorial, only a few commonly used properties are set.

提供 VM 大小和容量Supply VM size and capacity

规模集需要知道要创建的 VM 的大小(“sku name”)和要创建多少个这样的 VM(“sku capacity”)。The scale set needs to know what size of VM to create ("sku name") and how many such VMs to create ("sku capacity"). 若要查看可用的 VM 大小,请参阅 VM 大小文档To see which VM sizes are available, see the VM Sizes documentation.

       "sku": {
         "name": "Standard_A1",
         "capacity": 2
       },

选择更新类型Choose type of updates

规模集还需要知道如何处理规模集的更新。The scale set also needs to know how to handle updates on the scale set. 目前有三个选项:ManualRollingAutomaticCurrently, there are three options, Manual, Rolling and Automatic. 有关这两者之间的区别的详细信息,请参阅文档如何升级规模集For more information on the differences between the two, see the documentation on how to upgrade a scale set.

       "properties": {
         "upgradePolicy": {
           "mode": "Manual"
         },

选择 VM 操作系统Choose VM operating system

规模集需要知道要在虚拟机上安装什么操作系统。The scale set needs to know what operating system to put on the VMs. 此处使用完全修补的 Ubuntu 16.04-LTS 映像创建 VM。Here, create the VMs with a fully patched Ubuntu 16.04-LTS image.

         "virtualMachineProfile": {
           "storageProfile": {
             "imageReference": {
               "publisher": "Canonical",
               "offer": "UbuntuServer",
               "sku": "16.04-LTS",
               "version": "latest"
             }
           },

指定 computerNamePrefixSpecify computerNamePrefix

规模集部署多个 VM。The scale set deploys multiple VMs. 指定 computerNamePrefix 而不是指定每个 VM 名称。Instead of specifying each VM name, specify computerNamePrefix. 规模集在每个 VM 的前缀中附加一个索引,因此 VM 名称格式为 <computerNamePrefix>_<auto-generated-index>The scale set appends an index to the prefix for each VM, so VM names have the form <computerNamePrefix>_<auto-generated-index>.

在以下代码片段中,使用前面提到的参数为规模集中所有 VM 设置管理员用户名和密码。In the following snippet, use the parameters from before to set the administrator username and password for all VMs in the scale set. 此过程使用 parameters 模板函数。This process uses the parameters template function. 此函数将采用一个字符串来指定要引用的参数并输出该参数的值。This function takes in a string that specifies which parameter to refer to and outputs the value for that parameter.

           "osProfile": {
             "computerNamePrefix": "vm",
             "adminUsername": "[parameters('adminUsername')]",
             "adminPassword": "[parameters('adminPassword')]"
           },

指定 VM 网络配置Specify VM network configuration

最后,指定规模集中 VM 的网络配置。Finally, specify the network configuration for the VMs in the scale set. 在本例中,仅需指定之前创建的子网的 ID。In this case, you only need to specify the ID of the subnet created earlier. 这样,规模集便知道要在此子网中放置网络接口。This tells the scale set to put the network interfaces in this subnet.

可以使用 resourceId 模板函数获取包含该子网的虚拟网络的 ID。You can get the ID of the virtual network containing the subnet by using the resourceId template function. 此函数采用资源的类型和名称,并返回该资源的完全限定标识符。This function takes in the type and name of a resource and returns the fully qualified identifier of that resource. 此 ID 的格式为:/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/<resourceProviderNamespace>/<resourceType>/<resourceName>This ID has the form: /subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/<resourceProviderNamespace>/<resourceType>/<resourceName>

但是,仅有虚拟网络的标识符是不够的。However, the identifier of the virtual network is not enough. 提供规模集 VM 应位于的特定子网。Provide the specific subnet that the scale set VMs should be in. 为此,请将 /subnets/mySubnet 串联到虚拟网络的 ID。To do this, concatenate /subnets/mySubnet to the ID of the virtual network. 结果是该子网的完全限定的 ID。The result is the fully qualified ID of the subnet. 可以使用 concat 函数执行此串联,该函数采用一系列字符串并返回它们的串联字符串。Do this concatenation with the concat function, which takes in a series of strings and returns their concatenation.

           "networkProfile": {
             "networkInterfaceConfigurations": [
               {
                 "name": "myNic",
                 "properties": {
                   "primary": "true",
                   "ipConfigurations": [
                     {
                       "name": "myIpConfig",
                       "properties": {
                         "subnet": {
                           "id": "[concat(resourceId('Microsoft.Network/virtualNetworks', 'myVnet'), '/subnets/mySubnet')]"
                         }
                       }
                     }
                   ]
                 }
               }
             ]
           }
         }
       }
     }
   ]
}

后续步骤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.