你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure PowerShell 创建虚拟机规模集

适用于:✔️ Linux VM ✔️ Windows VM ✔️ 统一规模集

注意

下文介绍了统一虚拟机规模集。 建议对新的工作负载使用灵活虚拟机规模集。 如需详细了解此业务流程模式,请参阅灵活虚拟机规模集概述

利用虚拟机规模集,可以部署和管理一组自动缩放的虚拟机。 可以手动缩放规模集中的 VM 数,也可以定义规则,以便根据资源使用情况(如 CPU 使用率、内存需求或网络流量)进行自动缩放。 然后,Azure 负载均衡器会将流量分配到规模集中的 VM 实例。 在本快速入门中,我们将使用 Azure PowerShell 创建虚拟机规模集并部署一个示例应用程序。

如果没有 Azure 订阅,请在开始之前创建一个免费帐户

Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。 可以使用 Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。

若要启动 Azure Cloud Shell,请执行以下操作:

选项 示例/链接
选择代码或命令块右上角的“试用”。 选择“试用”不会自动将代码或命令复制到 Cloud Shell。 Screenshot that shows an example of Try It for Azure Cloud Shell.
转到 https://shell.azure.com 或选择启动 Cloud Shell 按钮可在浏览器中打开 Cloud Shell。 Button to launch Azure Cloud Shell.
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。 Screenshot that shows the Cloud Shell button in the Azure portal

若要使用 Azure Cloud Shell,请执行以下操作:

  1. 启动 Cloud Shell。

  2. 选择代码块(或命令块)上的“复制”按钮以复制代码或命令。

  3. 在 Windows 和 Linux 上选择 Ctrl+Shift+V,或在 macOS 上选择 Cmd+Shift+V 将代码或命令粘贴到 Cloud Shell 会话中。

  4. 选择“Enter”运行代码或命令。

创建规模集

创建规模集之前,需使用 New-AzResourceGroup 创建资源组。 以下示例在 eastus 位置创建名为 myResourceGroup 的资源组:

New-AzResourceGroup -ResourceGroupName "myResourceGroup" -Location "EastUS"

现在,使用 New-AzVmss 创建虚拟机规模集。 以下示例创建名为 myScaleSet 且使用 Windows Server 2016 Datacenter 平台映像的规模集。 虚拟网络、公共 IP 地址和负载均衡器的 Azure 网络资源均会自动创建。 出现提示时,可以针对规模集中的 VM 实例设置自己的管理凭据:

重要

从 2023 年 11 月开始,使用 PowerShell 和 Azure CLI 创建的 VM 规模集将默认为灵活业务流程模式(如果未指定业务流程模式)。 若要详细了解此更改以及你应采取哪些操作,请访问针对 VMSS PowerShell/CLI 客户的中断性变更 - Microsoft 社区中心

New-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -Location "EastUS" `
  -VMScaleSetName "myScaleSet" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -PublicIpAddressName "myPublicIPAddress" `
  -LoadBalancerName "myLoadBalancer" `
  -OrchestrationMode 'Uniform' `
  -UpgradePolicyMode "Automatic"

创建和配置所有的规模集资源和 VM 需要几分钟时间。

部署示例应用程序

若要测试规模集,请安装一个基本的 Web 应用程序。 使用 Azure 自定义脚本扩展下载并运行一个脚本,以便在 VM 实例上安装 IIS。 此扩展适用于部署后配置、软件安装或其他任何配置/管理任务。 有关详细信息,请参阅自定义脚本扩展概述

使用自定义脚本扩展安装基本的 IIS Web 服务器。 应用可安装 IIS 的自定义脚本扩展,如下所示:

# Define the script for your Custom Script Extension to run
$publicSettings = @{
    "fileUris" = (,"https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate-iis.ps1");
    "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File automate-iis.ps1"
}

# Get information about the scale set
$vmss = Get-AzVmss `
            -ResourceGroupName "myResourceGroup" `
            -VMScaleSetName "myScaleSet"

# Use Custom Script Extension to install IIS and configure basic website
Add-AzVmssExtension -VirtualMachineScaleSet $vmss `
    -Name "customScript" `
    -Publisher "Microsoft.Compute" `
    -Type "CustomScriptExtension" `
    -TypeHandlerVersion 1.8 `
    -Setting $publicSettings

# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
    -ResourceGroupName "myResourceGroup" `
    -Name "myScaleSet" `
    -VirtualMachineScaleSet $vmss

允许流量发往应用程序

若要允许访问基本的 Web 应用程序,请使用 New-AzNetworkSecurityRuleConfigNew-AzNetworkSecurityGroup 创建网络安全组。 有关详细信息,请参阅 Azure 虚拟机规模集的网络

# Get information about the scale set
$vmss = Get-AzVmss `
            -ResourceGroupName "myResourceGroup" `
            -VMScaleSetName "myScaleSet"

#Create a rule to allow traffic over port 80
$nsgFrontendRule = New-AzNetworkSecurityRuleConfig `
  -Name myFrontendNSGRule `
  -Protocol Tcp `
  -Direction Inbound `
  -Priority 200 `
  -SourceAddressPrefix * `
  -SourcePortRange * `
  -DestinationAddressPrefix * `
  -DestinationPortRange 80 `
  -Access Allow

#Create a network security group and associate it with the rule
$nsgFrontend = New-AzNetworkSecurityGroup `
  -ResourceGroupName  "myResourceGroup" `
  -Location EastUS `
  -Name myFrontendNSG `
  -SecurityRules $nsgFrontendRule

$vnet = Get-AzVirtualNetwork `
  -ResourceGroupName  "myResourceGroup" `
  -Name myVnet

$frontendSubnet = $vnet.Subnets[0]

$frontendSubnetConfig = Set-AzVirtualNetworkSubnetConfig `
  -VirtualNetwork $vnet `
  -Name mySubnet `
  -AddressPrefix $frontendSubnet.AddressPrefix `
  -NetworkSecurityGroup $nsgFrontend

Set-AzVirtualNetwork -VirtualNetwork $vnet

# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
    -ResourceGroupName "myResourceGroup" `
    -Name "myScaleSet" `
    -VirtualMachineScaleSet $vmss

测试规模集

若要查看正在运行的规模集,请在 Web 浏览器中访问示例 Web 应用程序。 使用 Get-AzPublicIpAddress 获取负载均衡器的公共 IP 地址。 以下示例显示在 myResourceGroup 资源组中创建的 IP 地址:

Get-AzPublicIpAddress -ResourceGroupName "myResourceGroup" | Select IpAddress

将负载均衡器的公共 IP 地址输入到 Web 浏览器中。 负载均衡器将流量分发到某个 VM 实例,如以下示例所示:

Running IIS site

清理资源

如果不再需要资源组、规模集和所有相关的资源,可以使用 Remove-AzResourceGroup 命令将其删除,如下所示。 -Force 参数将确认是否希望删除资源,不会显示询问是否删除的额外提示。 -AsJob 参数会使光标返回提示符处,无需等待操作完成。

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

后续步骤

在本快速入门中,我们已创建一个基本的规模集,并使用自定义脚本扩展在 VM 实例上安装了一个基本的 IIS Web 服务器。 若要了解详细信息,请继续学习有关如何创建和管理 Azure 虚拟机规模集的教程。