教程:使用 Azure PowerShell 创建和管理虚拟机规模集Tutorial: Create and manage a virtual machine scale set with Azure PowerShell

利用虚拟机规模集,可以部署和管理一组相同的、自动缩放的虚拟机。A virtual machine scale set allows you to deploy and manage a set of identical, auto-scaling virtual machines. 在虚拟机规模集的整个生命周期内,可能需要运行一个或多个管理任务。Throughout the lifecycle of a virtual machine scale set, you may need to run one or more management tasks. 本教程介绍如何执行下列操作:In this tutorial you learn how to:

  • 创建和连接虚拟机规模集Create and connect to a virtual machine scale set
  • 选择并使用 VM 映像Select and use VM images
  • 查看和使用特定 VM 实例大小View and use specific VM instance sizes
  • 手动缩放规模集Manually scale a scale set
  • 执行常见的规模集管理任务Perform common scale set management tasks

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a Trial before you begin.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

创建资源组Create a resource group

Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 必须在创建虚拟机规模集前创建资源组。A resource group must be created before a virtual machine scale set. 使用 New-AzResourceGroup 命令创建资源组。Create a resource group with the New-AzResourceGroup command. 本示例在 ChinaNorth 区域中创建名为 myResourceGroup 的资源组。In this example, a resource group named myResourceGroup is created in the ChinaNorth region.

New-AzResourceGroup -ResourceGroupName "myResourceGroup" -Location "ChinaNorth"

在本教程中,此资源组名称是在创建或修改规模集时指定的。The resource group name is specified when you create or modify a scale set throughout this tutorial.

创建规模集Create a scale set

首先,使用 Get-Credential 设置 VM 实例的管理员用户名和密码:First, set an administrator username and password for the VM instances with Get-Credential:

$cred = Get-Credential

现在,使用 New-AzVmss 创建虚拟机规模集。Now create a virtual machine scale set with New-AzVmss. 若要将流量分配到单独的 VM 实例,则还要创建负载均衡器。To distribute traffic to the individual VM instances, a load balancer is also created. 负载均衡器包含的规则可在 TCP 端口 80 上分配流量,并允许 TCP 端口 3389 上的远程桌面流量,以及 TCP 端口 5985 上的 PowerShell 远程流量:The load balancer includes rules to distribute traffic on TCP port 80, as well as allow remote desktop traffic on TCP port 3389 and PowerShell remoting on TCP port 5985:

New-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -VMScaleSetName "myScaleSet" `
  -Location "ChinaNorth" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -PublicIpAddressName "myPublicIPAddress" `
  -LoadBalancerName "myLoadBalancer" `
  -Credential $cred

创建和配置所有的规模集资源和 VM 实例需要几分钟时间。It takes a few minutes to create and configure all the scale set resources and VM instances.

重要

如果无法连接到规模集,则可能需要通过添加 -SecurityGroupName "mySecurityGroup" 参数来创建网络安全组。If you are unable to connect to your scale set, you may need to create a Network Security Group by adding the -SecurityGroupName "mySecurityGroup" parameter.

查看规模集中的 VM 实例View the VM instances in a scale set

若要在规模集中查看 VM 实例的列表,请使用 Get-AzVmssVM,如下所示:To view a list of VM instances in a scale set, use Get-AzVmssVM as follows:

Get-AzVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

以下示例输出显示了规模集中的两个 VM 实例:The following example output shows two VM instances in the scale set:

ResourceGroupName         Name Location             Sku InstanceID ProvisioningState
-----------------         ---- --------             --- ---------- -----------------
MYRESOURCEGROUP   myScaleSet_0   chinanorth Standard_DS1_v2          0         Succeeded
MYRESOURCEGROUP   myScaleSet_1   chinanorth Standard_DS1_v2          1         Succeeded

若要查看特定 VM 实例的其他信息,请将 -InstanceId 参数添加到 Get-AzVmssVMTo view additional information about a specific VM instance, add the -InstanceId parameter to Get-AzVmssVM. 以下示例查看 VM 实例 1 的相关信息:The following example views information about VM instance 1:

Get-AzVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "1"

列出连接信息List connection information

系统将公共 IP 地址分配给负载均衡器,由后者将流量路由到各个 VM 实例。A public IP address is assigned to the load balancer that routes traffic to the individual VM instances. 默认情况下,会将网络地址转换 (NAT) 规则添加到 Azure 负载均衡器,由后者将远程连接流量转发给给定端口上的每个 VM。By default, Network Address Translation (NAT) rules are added to the Azure load balancer that forwards remote connection traffic to each VM on a given port. 若要连接到规模集中的 VM 实例,请创建一个可连接到已分配的公共 IP 地址和端口号的远程连接。To connect to the VM instances in a scale set, you create a remote connection to an assigned public IP address and port number.

若要列出连接到规模集中的 VM 实例所需的 NAT 端口,请先使用 Get-AzLoadBalancer 获取负载均衡器对象,To list the NAT ports to connect to VM instances in a scale set, first get the load balancer object with Get-AzLoadBalancer. 然后使用 Get-AzLoadBalancerInboundNatRuleConfig 查看入站 NAT 规则:Then, view the inbound NAT rules with Get-AzLoadBalancerInboundNatRuleConfig:

# Get the load balancer object
$lb = Get-AzLoadBalancer -ResourceGroupName "myResourceGroup" -Name "myLoadBalancer"

# View the list of inbound NAT rules
Get-AzLoadBalancerInboundNatRuleConfig -LoadBalancer $lb | Select-Object Name,Protocol,FrontEndPort,BackEndPort

以下示例输出显示了实例名称、负载均衡器的公共 IP 地址,以及可以通过 NAT 规则将流量转发到其中的端口号:The following example output shows the instance name, public IP address of the load balancer, and port number that the NAT rules forward traffic to:

Name             Protocol FrontendPort BackendPort
----             -------- ------------ -----------
myScaleSet3389.0 Tcp             50001        3389
myScaleSet5985.0 Tcp             51001        5985
myScaleSet3389.1 Tcp             50002        3389
myScaleSet5985.1 Tcp             51002        5985

此规则的 Name 与以前的 Get-AzVmssVM 命令中显示的 VM 实例的名称相符。The Name of the rule aligns with the name of the VM instance as shown in a previous Get-AzVmssVM command. 例如,若要连接到 VM 实例 0,请使用 myScaleSet3389.0 并连接到端口 50001For example, to connect to VM instance 0, you use myScaleSet3389.0 and connect to port 50001. 若要连接到 VM 实例 1,请使用 myScaleSet3389.1 中的值并连接到端口 50002To connect to VM instance 1, use the value from myScaleSet3389.1 and connect to port 50002. 若要使用 PowerShell 远程处理,请连接到 TCP 端口 5985 所对应的 VM 实例规则。To use PowerShell remoting, you connect to the appropriate VM instance rule for TCP port 5985.

使用 Get-AzPublicIpAddress 查看负载均衡器的公共 IP 地址:View the public IP address of the load balancer with Get-AzPublicIpAddress:

Get-AzPublicIpAddress -ResourceGroupName "myResourceGroup" -Name "myPublicIPAddress" | Select IpAddress

示例输出:Example output:

IpAddress
---------
52.168.121.216

创建连接到第一个 VM 实例所需的远程连接。Create a remote connection to your first VM instance. 指定所需 VM 实例的公共 IP 地址和端口号,如前述命令所示。Specify your public IP address and port number of the required VM instance, as shown from the preceding commands. 出现提示时,输入创建规模集时使用的凭据(在示例命令中,默认为 azureuser 和 P@ssw0rd!) 。When prompted, enter the credentials used when you created the scale set (by default in the sample commands, azureuser and P@ssw0rd!). 以下示例连接到 VM 实例 1The following example connects to VM instance 1:

mstsc /v 52.168.121.216:50001

登录到 VM 实例以后,可以根据需要执行一些手动配置更改。Once logged in to the VM instance, you could perform some manual configuration changes as needed. 现在,请关闭远程连接。For now, close the remote connection.

了解 VM 实例映像Understand VM instance images

Azure 市场包括许多可用于创建 VM 实例的映像。The Azure marketplace includes many images that can be used to create VM instances. 若要查看可用发布者的列表,请使用 Get-AzVMImagePublisher 命令。To see a list of available publishers, use the Get-AzVMImagePublisher command.

Get-AzVMImagePublisher -Location "ChinaNorth"

若要查看给定发布者的映像的列表,请使用 Get-AzVMImageSkuTo view a list of images for a given publisher, use Get-AzVMImageSku. 还可以按 -PublisherName-Offer 筛选映像列表。The image list can also be filtered by -PublisherName or -Offer. 以下示例从列表中筛选出了发布者名称为 MicrosoftWindowsServer 且产品/服务与 WindowsServer 相匹配的所有映像:In the following example, the list is filtered for all images with publisher name of MicrosoftWindowsServer and an offer that matches WindowsServer:

Get-AzVMImageSku -Location "ChinaNorth" -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer"

以下示例输出演示所有可用的 Windows Server 映像:The following example output shows all of the available Windows Server images:

Skus                                  Offer         PublisherName          Location
----                                  -----         -------------          --------
2008-R2-SP1                           WindowsServer MicrosoftWindowsServer chinanorth
2008-R2-SP1-smalldisk                 WindowsServer MicrosoftWindowsServer chinanorth
2012-Datacenter                       WindowsServer MicrosoftWindowsServer chinanorth
2012-Datacenter-smalldisk             WindowsServer MicrosoftWindowsServer chinanorth
2012-R2-Datacenter                    WindowsServer MicrosoftWindowsServer chinanorth
2012-R2-Datacenter-smalldisk          WindowsServer MicrosoftWindowsServer chinanorth
2016-Datacenter                       WindowsServer MicrosoftWindowsServer chinanorth
2016-Datacenter-Server-Core           WindowsServer MicrosoftWindowsServer chinanorth
2016-Datacenter-Server-Core-smalldisk WindowsServer MicrosoftWindowsServer chinanorth
2016-Datacenter-smalldisk             WindowsServer MicrosoftWindowsServer chinanorth
2016-Datacenter-with-Containers       WindowsServer MicrosoftWindowsServer chinanorth
2016-Datacenter-with-RDSH             WindowsServer MicrosoftWindowsServer chinanorth
2016-Nano-Server                      WindowsServer MicrosoftWindowsServer chinanorth

在教程开头创建规模集时,为 VM 实例提供了默认 VM 映像 Windows Server 2016 DataCenterWhen you created a scale set at the start of the tutorial, a default VM image of Windows Server 2016 DataCenter was provided for the VM instances. 可以根据 Get-AzVMImageSku 的输出指定其他 VM 映像。You can specify a different VM image based on the output from Get-AzVMImageSku. 以下示例会使用 -ImageName 参数创建一个规模集,以便指定 VM 映像 MicrosoftWindowsServer:WindowsServer:2016-Datacenter-with-Containers:latestThe following example would create a scale set with the -ImageName parameter to specify a VM image of MicrosoftWindowsServer:WindowsServer:2016-Datacenter-with-Containers:latest. 由于只需数分钟即可创建和配置所有的规模集资源和 VM 实例,因此不需部署以下规模集:As it takes a few minutes to create and configure all the scale set resources and VM instances, you don't have to deploy the following scale set:

New-AzVmss `
  -ResourceGroupName "myResourceGroup2" `
  -Location "ChinaNorth" `
  -VMScaleSetName "myScaleSet2" `
  -VirtualNetworkName "myVnet2" `
  -SubnetName "mySubnet2" `
  -PublicIpAddressName "myPublicIPAddress2" `
  -LoadBalancerName "myLoadBalancer2" `
  -UpgradePolicyMode "Automatic" `
  -ImageName "MicrosoftWindowsServer:WindowsServer:2016-Datacenter-with-Containers:latest" `
  -Credential $cred

重要

建议使用最新版本的映像。We recommend using the latest image version. 指定“latest”以使用部署时可用的最新版本的映像。Specify 'latest' to use the latest version of an image available at deploy time. 请注意,即使使用的是“latest”,VM 映像部署后也不会自动更新,即使新版本可用也是如此。Note, even if you use 'latest', the VM image will not automatically update after deploy time even if a new version becomes available.

了解 VM 实例大小Understand VM instance sizes

VM 实例大小或 SKU 决定了可供 VM 实例使用的计算资源(如 CPU、GPU 和内存)的量。A VM instance size, or SKU, determines the amount of compute resources such as CPU, GPU, and memory that are made available to the VM instance. 需要根据预期的工作负荷适当调整规模集中 VM 实例的大小。VM instances in a scale set need to be sized appropriately for the expected work load.

VM 实例大小VM instance sizes

下表将常用 VM 大小按类别分成了多个用例。The following table categorizes common VM sizes into use cases.

类型Type 常见大小Common sizes 说明Description
常规用途General purpose Dsv3、Dv3、DSv2、Dv2、DS、D、Av2、A0-7Dsv3, Dv3, DSv2, Dv2, DS, D, Av2, A0-7 CPU 与内存之比均衡。Balanced CPU-to-memory. 适用于开发/测试、小到中型应用程序和数据解决方案。Ideal for dev / test and small to medium applications and data solutions.
计算优化Compute optimized Fs, FFs, F 高 CPU 与内存之比。High CPU-to-memory. 适用于中等流量的应用程序、网络设备和批处理。Good for medium traffic applications, network appliances, and batch processes.
内存优化Memory optimized Esv3、Ev3、M、DSv2、DS、Dv2、DEsv3, Ev3, M, DSv2, DS, Dv2, D 较高的内存核心比。High memory-to-core. 适用于关系数据库、中到大型缓存和内存中分析。Great for relational databases, medium to large caches, and in-memory analytics.
GPUGPU NV, NCNV, NC 专门针对大量图形绘制和视频编辑的 VM。Specialized VMs targeted for heavy graphic rendering and video editing.

查找可用的 VM 实例大小Find available VM instance sizes

若要查看在特定区域可用的 VM 实例大小的列表,请使用 Get-AzVMSize 命令。To see a list of VM instance sizes available in a particular region, use the Get-AzVMSize command.

Get-AzVMSize -Location "ChinaNorth"

输出类似于以下浓缩版示例,其中显示了分配给每个 VM 大小的资源:The output is similar to the following condensed example, which shows the resources assigned to each VM size:

Name                   NumberOfCores MemoryInMB MaxDataDiskCount OSDiskSizeInMB ResourceDiskSizeInMB
----                   ------------- ---------- ---------------- -------------- --------------------
Standard_DS1_v2                    1       3584                4        1047552                 7168
Standard_DS2_v2                    2       7168                8        1047552                14336
[...]
Standard_A0                        1        768                1        1047552                20480
Standard_A1                        1       1792                2        1047552                71680
[...]
Standard_F1                        1       2048                4        1047552                16384
Standard_F2                        2       4096                8        1047552                32768
[...]
Standard_NV6                       6      57344               24        1047552               389120
Standard_NV12                     12     114688               48        1047552               696320

在教程开头创建规模集时,为 VM 实例提供了默认 VM SKU Standard_DS1_v2When you created a scale set at the start of the tutorial, a default VM SKU of Standard_DS1_v2 was provided for the VM instances. 可以根据 Get-AzVMSize 的输出指定其他 VM 实例大小。You can specify a different VM instance size based on the output from Get-AzVMSize. 以下示例会使用 -VmSize 参数创建一个规模集,以便指定 VM 实例大小 Standard_F1The following example would create a scale set with the -VmSize parameter to specify a VM instance size of Standard_F1. 由于只需数分钟即可创建和配置所有的规模集资源和 VM 实例,因此不需部署以下规模集:As it takes a few minutes to create and configure all the scale set resources and VM instances, you don't have to deploy the following scale set:

New-AzVmss `
  -ResourceGroupName "myResourceGroup3" `
  -Location "ChinaNorth" `
  -VMScaleSetName "myScaleSet3" `
  -VirtualNetworkName "myVnet3" `
  -SubnetName "mySubnet3" `
  -PublicIpAddressName "myPublicIPAddress3" `
  -LoadBalancerName "myLoadBalancer3" `
  -UpgradePolicyMode "Automatic" `
  -VmSize "Standard_F1" `
  -Credential $cred

更改规模集的容量Change the capacity of a scale set

你在创建规模集时,请求了两个 VM 实例。When you created a scale set, you requested two VM instances. 若要增加或减少规模集中的 VM 实例数,可以手动更改容量。To increase or decrease the number of VM instances in the scale set, you can manually change the capacity. 规模集会创建或删除所需数量的 VM 实例,然后配置分发流量所需的负载均衡器。The scale set creates or removes the required number of VM instances, then configures the load balancer to distribute traffic.

首先,使用 Get-AzVmss 创建的规模集对象,然后为 sku.capacity 指定新的值。First, create a scale set object with Get-AzVmss, then specify a new value for sku.capacity. 若要应用容量更改,请使用 Update-AzVmssTo apply the capacity change, use Update-AzVmss. 以下示例将规模集中 VM 实例的数目设置为 3The following example sets the number of VM instances in your scale set to 3:

# Get current scale set
$vmss = Get-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

# Set and update the capacity of your scale set
$vmss.sku.capacity = 3
Update-AzVmss -ResourceGroupName "myResourceGroup" -Name "myScaleSet" -VirtualMachineScaleSet $vmss 

更新规模集容量需要花费数分钟。If takes a few minutes to update the capacity of your scale set. 若要查看规模集中当前包含的实例数,请使用 Get-AzVmssTo see the number of instances you now have in the scale set, use Get-AzVmss:

Get-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

以下示例输出显示规模集的容量现在为 3The following example output shows that the capacity of the scale set is now 3:

Sku        :
  Name     : Standard_DS2
  Tier     : Standard
  Capacity : 3

常见管理任务Common management tasks

现在可以创建规模集、列出连接信息以及连接到 VM 实例。You can now create a scale set, list connection information, and connect to VM instances. 你已经了解如何对 VM 实例使用其他 OS 映像、如何选择其他 VM 大小,或者如何手动缩放实例数。You learned how you could use a different OS image for your VM instances, select a different VM size, or manually scale the number of instances. 在日常管理过程中,可能需要停止、启动或重启规模集中的 VM 实例。As part of day to day management, you may need to stop, start, or restart the VM instances in your scale set.

停止和解除分配规模集中的 VM 实例Stop and deallocate VM instances in a scale set

若要停止规模集中的一个或多个 VM,请使用 Stop-AzVmssTo stop one or more VMs in a scale set, use Stop-AzVmss. 通过 -InstanceId 参数,可指定要停止的一个或多个 VM。The -InstanceId parameter allows you to specify one or more VMs to stop. 若不指定实例 ID,则停止规模集中的所有 VM。If you do not specify an instance ID, all VMs in the scale set are stopped. 以下示例停止实例 1The following example stops instance 1:

Stop-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "1"

默认情况下,将取消分配已停止的 VM,这些 VM 不会产生计算费用。By default, stopped VMs are deallocated and do not incur compute charges. 若要在停止 VM 后保持预配状态,请将 -StayProvisioned 参数添加到上面的命令中。If you wish the VM to remain in a provisioned state when stopped, add the -StayProvisioned parameter to the preceding command. 保持预配状态的已停止 VM 会产生常规计算费用。Stopped VMs that remain provisioned incur regular compute charges.

启动规模集中的 VM 实例Start VM instances in a scale set

若要启动规模集中的一个或多个 VM,请使用 Start-AzVmssTo start one or more VMs in a scale set, use Start-AzVmss. 通过 -InstanceId 参数,可指定要启动的一个或多个 VM。The -InstanceId parameter allows you to specify one or more VMs to start. 若不指定实例 ID,则启动规模集中的所有 VM。If you do not specify an instance ID, all VMs in the scale set are started. 以下示例启动实例 1The following example starts instance 1:

Start-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "1"

重启规模集中的 VM 实例Restart VM instances in a scale set

若要重启规模集中的一个或多个 VM,请使用 Retart-AzVmssTo restart one or more VMs in a scale set, use Retart-AzVmss. 通过 -InstanceId 参数,可指定要重启的一个或多个 VM。The -InstanceId parameter allows you to specify one or more VMs to restart. 若不指定实例 ID,则重启规模集中的所有 VM。If you do not specify an instance ID, all VMs in the scale set are restarted. 以下示例重启实例 1The following example restarts instance 1:

Restart-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "1"

清理资源Clean up resources

删除资源组时,也会删除其中包含的所有资源,例如 VM 实例、虚拟网络和磁盘。When you delete a resource group, all resources contained within, such as the VM instances, virtual network, and disks, are also deleted. -Force 参数将确认是否希望删除资源,不会显示询问是否删除的额外提示。The -Force parameter confirms that you wish to delete the resources without an additional prompt to do so. -AsJob 参数会使光标返回提示符处,不会等待操作完成。The -AsJob parameter returns control to the prompt without waiting for the operation to complete.

Remove-AzResourceGroup -Name "myResourceGroup" -Force -AsJob

后续步骤Next steps

本教程介绍了如何使用 Azure PowerShell 执行一些基本的规模集创建和管理任务:In this tutorial, you learned how to perform some basic scale set creation and management tasks with Azure PowerShell:

  • 创建和连接虚拟机规模集Create and connect to a virtual machine scale set
  • 选择并使用 VM 映像Select and use VM images
  • 查看和使用特定 VM 大小View and use specific VM sizes
  • 手动缩放规模集Manually scale a scale set
  • 执行常见的规模集管理任务Perform common scale set management tasks

请转到下一教程,了解规模集磁盘。Advance to the next tutorial to learn about scale set disks.