创建和部署虚拟机规模集Create and deploy a virtual machine scale set

使用虚拟机规模集可以轻松地将相同的虚拟机作为集来进行部署和管理。Virtual machine scale sets make it easy for you to deploy and manage identical virtual machines as a set. 规模集为超大规模应用程序提供高度可缩放且可自定义的计算层,并且它们支持 Windows 平台映像、Linux 平台映像、自定义映像和扩展。Scale sets provide a highly scalable and customizable compute layer for hyperscale applications, and they support Windows platform images, Linux platform images, custom images, and extensions. 有关规模集的详细信息,请参阅虚拟机规模集For more information about scale sets, see Virtual machine scale sets.

本教程介绍如何使用 Azure 门户就创建虚拟机规模集。This tutorial shows you how to create a virtual machine scale set without using the Azure portal. 有关如何使用 Azure 门户的信息,请参阅如何使用 Azure 门户创建虚拟机规模集For information about how to use the Azure portal, see How to create a virtual machine scale set with the Azure portal.

备注

有关 Azure 资源管理器资源的详细信息,请参阅 Azure 资源管理器与经典部署For more information about Azure Resource Manager resources, see Azure Resource Manager vs. classic deployment.

登录 AzureSign in to Azure

若要使用 Azure CLI 2.0 或 Azure PowerShell 创建规模集,首先需要登录到订阅。If you're using Azure CLI 2.0 or Azure PowerShell to create a scale set, you first need to sign in to your subscription.

有关如何使用 Azure CLI 或 PowerShell 安装、设置和登录到 Azure 的详细信息,请参阅 Azure CLI 2.0 入门Azure PowerShell cmdlet 入门For more information about how to install, set up, and sign in to Azure with Azure CLI or PowerShell, see Getting Started with Azure CLI 2.0 or Get started with Azure PowerShell cmdlets.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

az login
Login-AzureRmAccount -EnvironmentName AzureChinaCloud

创建资源组Create a resource group

首先需要创建虚拟机规模集所关联的资源组。You first need to create a resource group that the virtual machine scale set is associated with.

az group create --location chinanorth --name MyResourceGroup1
New-AzureRmResourceGroup -Location chinanorth -Name MyResourceGroup1

从 Azure CLI 创建Create from Azure CLI

使用 Azure CLI,只需最少的工作量就可创建虚拟机规模集。With Azure CLI, you can create a virtual machine scale set with minimal effort. 如果省略默认值,则将为你提供它们。If you omit default values, they are provided for you. 例如,如果未指定任何虚拟网络信息,系统会自动创建一个虚拟网络。For example, if you don't specify any virtual network information, a virtual network is created for you. 如果省略以下组成部分,系统会自动予以创建:If you omit the following parts, they are created for you:

  • 负载均衡器A load balancer
  • 虚拟网络A virtual network
  • 公共 IP 地址A public IP address

选择要用于虚拟机规模集的虚拟机映像时,可以使用几个选项:When choosing the virtual machine image that you want to use on the virtual machine scale set, you have a few choices:

  • URNURN
    资源的标识符:The identifier of a resource:
    Win2012R2DatacenterWin2012R2Datacenter

  • URN 别名URN alias
    URN 的友好名称:The friendly name of a URN:
    MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latestMicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest

  • 自定义资源 IDCustom resource id
    Azure 资源的路径:The path to an Azure resource:
    /subscriptions/subscription-guid/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/images/MyImage/subscriptions/subscription-guid/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/images/MyImage

  • Web 资源Web resource
    HTTP URI 的路径:The path to an HTTP URI:
    http://contoso.blob.core.chinacloudapi.cn/vhds/osdiskimage.vhd

提示

使用 az vm image list 可获取可用映像的列表。You can get a list of available images with az vm image list.

若要创建虚拟机规模集,必须指定以下各项:To create a virtual machine scale set, you must specify the following:

  • 资源组Resource group
  • NameName
  • 操作系统映像Operating system image
  • 身份验证信息Authentication information

以下示例创建基本虚拟机规模集(此步骤可能需要几分钟)。The following example creates a basic virtual machine scale set (this step might take a few minutes).

az vmss create --resource-group MyResourceGroup1 --name MyScaleSet --image UbuntuLTS --authentication-type password --admin-username azureuser --admin-password P@ssw0rd! --vm-sku Standard_DS1

完成该命令后,即已创建虚拟机规模集。Once the command finishes you will now have your virtual machine scale set created. 可能需要获取虚拟机的 IP 地址,以便能够与它建立连接。You may need to get the IP address of the virtual machine so that you can connect to it. 可以使用以下命令获取有关虚拟机的各种不同信息(包括 IP 地址)。You can get a lot of different information about the virtual machine (including the IP address) with the following command.

az vmss list-instance-connection-info --resource-group MyResourceGroup1 --name MyScaleSet

从 PowerShell 创建Create from PowerShell

PowerShell 的用法比 Azure CLI 更复杂。PowerShell is more complicated to use than Azure CLI. Azure CLI 为网络相关的资源(例如负载均衡器、IP 地址和虚拟网络)提供默认值,而 PowerShell 则不提供。While Azure CLI provides defaults for networking-related resources (such as load balancers, IP addresses, and virtual networks), PowerShell does not. 使用 PowerShell 引用映像也略微复杂一些。Referencing an image with PowerShell is a slightly more complicated too. 可以使用以下 cmdlet 获取映像:You can get images with the following cmdlets:

  1. Get-AzureRMVMImagePublisherGet-AzureRMVMImagePublisher
  2. Get-AzureRMVMImageOfferGet-AzureRMVMImageOffer
  3. Get-AzureRmVMImageSkuGet-AzureRmVMImageSku

cmdlet 的运行结果可以按顺序通过管道传送。The cmdlets work can be piped in sequence. 以下示例演示如何获取其发布服务器包含名称 microsoft 的“中国北部” 区域的所有映像。Here is an example of how to get all images for the China North region with a publisher that has the name microsoft in it.

Get-AzureRMVMImagePublisher -Location ChinaNorth | Where-Object PublisherName -Like *microsoft* | Get-AzureRMVMImageOffer | Get-AzureRmVMImageSku | Select-Object PublisherName, Offer, Skus
PublisherName              Offer          Skus
-------------              -----          ----
MicrosoftAzureSiteRecovery Process-Server Windows-2012-R2-Datacenter
MicrosoftOSTC              FreeBSD        10.3
MicrosoftOSTC              FreeBSD        11.0
MicrosoftRServer           RServer-CentOS Enterprise
MicrosoftRServer           RServer-RedHat Enterprise
MicrosoftRServer           RServer-Ubuntu Enterprise
MicrosoftRServer           RServer-WS2016 Enterprise
...

用于创建虚拟机规模集的工作流如下:The workflow for creating a virtual machine scale set is as follows:

  1. 创建包含规模集相关信息的配置对象。Create a config object that holds information about the scale set.
  2. 引用基本 OS 映像。Reference the base OS image.
  3. 配置操作系统设置:身份验证、VM 名称前缀和用户/密码。Configure the operating system settings: authentication, VM name prefix, and user/pass.
  4. 配置网络。Configure networking.
  5. 创建规模集。Create the scale set.

本示例为装有 Windows Server 2016 的计算机创建一个基本的双实例规模集。This example creates a basic two-instance scale set for a computer that has Windows Server 2016 installed.

# Resource group name from above
$rg = "MyResourceGroup1"
$location = "ChinaNorth"

# Create a config object
$vmssConfig = New-AzureRmVmssConfig -Location $location -SkuCapacity 2 -SkuName Standard_A0  -UpgradePolicyMode Automatic

# Reference a virtual machine image from the gallery
Set-AzureRmVmssStorageProfile $vmssConfig -ImageReferencePublisher MicrosoftWindowsServer -ImageReferenceOffer WindowsServer -ImageReferenceSku 2016-Datacenter -ImageReferenceVersion latest

# Set up information for authenticating with the virtual machine
Set-AzureRmVmssOsProfile $vmssConfig -AdminUsername azureuser -AdminPassword P@ssw0rd! -ComputerNamePrefix myvmssvm

# Create the virtual network resources

## Basics
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name "my-subnet" -AddressPrefix 10.0.0.0/24
$vnet = New-AzureRmVirtualNetwork -Name "my-network" -ResourceGroupName $rg -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet

## Load balancer
$publicIP = New-AzureRmPublicIpAddress -Name "PublicIP" -ResourceGroupName $rg -Location $location -AllocationMethod Static -DomainNameLabel "myuniquedomain"
$frontendIP = New-AzureRmLoadBalancerFrontendIpConfig -Name "LB-Frontend" -PublicIpAddress $publicIP
$backendPool = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name "LB-backend"
$probe = New-AzureRmLoadBalancerProbeConfig -Name "HealthProbe" -Protocol Tcp -Port 80 -IntervalInSeconds 15 -ProbeCount 2
$inboundNATRule1= New-AzureRmLoadBalancerRuleConfig -Name "webserver" -FrontendIpConfiguration $frontendIP -Protocol Tcp -FrontendPort 80 -BackendPort 80 -IdleTimeoutInMinutes 15 -Probe $probe -BackendAddressPool $backendPool
$inboundNATPool1 = New-AzureRmLoadBalancerInboundNatPoolConfig -Name "RDP" -FrontendIpConfigurationId $frontendIP.Id -Protocol TCP -FrontendPortRangeStart 53380 -FrontendPortRangeEnd 53390 -BackendPort 3389

New-AzureRmLoadBalancer -ResourceGroupName $rg -Name "LB1" -Location $location -FrontendIpConfiguration $frontendIP -LoadBalancingRule $inboundNATRule1 -InboundNatPool $inboundNATPool1 -BackendAddressPool $backendPool -Probe $probe

## IP address config
$ipConfig = New-AzureRmVmssIpConfig -Name "my-ipaddress" -LoadBalancerBackendAddressPoolsId $backendPool.Id -SubnetId $vnet.Subnets[0].Id -LoadBalancerInboundNatPoolsId $inboundNATPool1.Id

# Attach the virtual network to the IP object
Add-AzureRmVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmssConfig -Name "network-config" -Primary $true -IPConfiguration $ipConfig

# Create the scale set with the config object (this step might take a few minutes)
New-AzureRmVmss -ResourceGroupName $rg -Name "MyScaleSet1" -VirtualMachineScaleSet $vmssConfig

使用自定义虚拟机映像Using a custom virtual machine image

如果要从自己的自定义映像创建规模集,而不是从库中引用虚拟机映像,Set-AzureRmVmssStorageProfile 命令将如下所示: If you are creating a scale set from your own custom image, instead of referencing a virtual machine image from the gallery, the Set-AzureRmVmssStorageProfile command would look like this:

Set-AzureRmVmssStorageProfile -OsDiskCreateOption FromImage -ManagedDisk PremiumLRS -OsDiskCaching "None" -OsDiskOsType Linux -ImageReferenceId (Get-AzureRmImage -ImageName $VMImage -ResourceGroupName $rg).id

从模板创建Create from a template

可以使用 Azure Resource Manager 模板部署虚拟机规模集。You can deploy a virtual machine scale set by using an Azure Resource Manager template. 可以创建自己的模板,也可以使用模板存储库中的模板。You can create your own template or use one from the template repository. 可以直接将这些模板部署到 Azure 订阅。These templates can be deployed directly to your Azure subscription.

备注

若要创建自己的模板,请创建一个 JSON 文本文件。To create your own template, you create a JSON text file. 有关如何创建和自定义模板的常规信息,请参阅 Azure Resource Manager 模板For general information about how to create and customize a template, see Azure Resource Manager templates.

GitHub 上提供了一个示例模板。A sample template is available on GitHub. 有关如何创建和使用该示例的详细信息,请参阅最小的可行规模集For more information about how to create and use that sample, see Minimum viable scale set.

从 Visual Studio 创建Create from Visual Studio

使用 Visual Studio 可以创建 Azure 资源组项目,并在其中添加虚拟机规模集模板。With Visual Studio, you can create an Azure resource group project and add a virtual machine scale set template to it. 可以选择是要从 GitHub 还是 Azure Web 应用程序库导入该模板。You can choose whether you want to import it from GitHub or the Azure Web Application Gallery. 还会生成部署 PowerShell 脚本。A deployment PowerShell script is also generated for you. 有关详细信息,请参阅如何使用 Visual Studio 创建虚拟机规模集For more information, see How to create a virtual machine scale set with Visual Studio.

从 Azure 门户中创建Create from the Azure portal

Azure 门户提供了快速创建规模集的简便方式。The Azure portal provides a convenient way to quickly create a scale set. 有关详细信息,请参阅如何使用 Azure 门户创建虚拟机规模集For more information, see How to create a virtual machine scale set with the Azure portal.

后续步骤Next steps

了解有关数据磁盘的详细信息。Learn more about data disks.

了解如何管理应用Learn how to manage your apps.