从通用化映像创建规模集Create a scale set from a generalized image

共享映像库中存储的通用化映像版本创建 VM。Create a VM from a generalized image version stored in a Shared Image Gallery. 若要使用专用化映像创建规模集,请参阅从专用化映像创建规模集实例If want to create a scale set using a specialized image, see Create scale set instances from a specialized image.

有通用化映像后,可以使用 New-AzVmss cmdlet 来创建虚拟机规模集。Once you have a generalized image, you can create a virtual machine scale set using the New-AzVmss cmdlet.

在此示例中,我们使用映像定义 ID 来确保新 VM 会使用最新版本的映像。In this example, we are using the image definition ID to ensure your new VM will use the most recent version of an image. 也可通过将映像版本 ID 用作 -ImageReferenceId 来使用特定版本。You can also use a specific version by using the image version ID for -ImageReferenceId. 例如,若要使用映像版本 1.0.0,请键入:-ImageReferenceId "/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0"For example, to use image version 1.0.0 type: -ImageReferenceId "/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0".

请注意,使用特定映像版本意味着:如果该特定映像版本由于已删除或已从区域中删除而无法使用,则自动化可能会失败。Be aware that using a specific image version means automation could fail if that specific image version isn't available because it was deleted or removed from the region. 建议使用映像定义 ID 来创建新的 VM(除非需要特定的映像版本)。We recommend using the image definition ID for creating your new VM, unless a specific image version is required.

以下示例在 chinanorth 位置的“myVMSSRG”资源组中创建名为“myScaleSet”的规模集。The following examples create a scale set named myScaleSet, in the myVMSSRG resource group, in the chinanorth location. 将基于“myGalleryRG”资源组的“myGallery”映像库中的“myImageDefinition”映像创建该规模集。The scale set will be created from the myImageDefinition image, in the myGallery image gallery in the myGalleryRG resource group. 出现提示时,请为该规模集中的 VM 实例设置自己的管理凭据。When prompted, set your own administrative credentials for the VM instances in the scale set.

简化参数集Simplified parameter set

若要在提供最少信息的同时快速创建规模集,请使用简化参数集从“共享映像库”映像创建规模集。To quickly create a scale set, while providing minimal information, use the simplified parameter set to create a scale set from a Share Image Gallery image.

$imageDefinition = Get-AzGalleryImageDefinition `
   -GalleryName myGallery `
   -ResourceGroupName myGalleryRG `
   -Name myImageDefinition

# Create user object

$cred = Get-Credential `
   -Message "Enter a username and password for the virtual machine."

# Create the resource group and scale set
New-AzResourceGroup -ResourceGroupName myVMSSRG -Location chinanorth
New-AzVmss `
   -Credential $cred `
   -VMScaleSetName myScaleSet `
   -ImageName $imageDefinition.Id `
   -UpgradePolicyMode Automatic `
   -ResourceGroupName myVMSSRG

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

扩展参数集Extended parameter set

若要完全控制所有资源(包括命名),请使用完整参数集基于“共享映像库”映像创建规模集。For full control over all of the resources, including naming, use the full parameter set to create a scale set using a Shared Image Gallery image.

# Get the image definition

$imageDefinition = Get-AzGalleryImageDefinition `
   -GalleryName myGallery `
   -ResourceGroupName myGalleryRG `
   -Name myImageDefinition

# Create user object

$cred = Get-Credential `
   -Message "Enter a username and password for the virtual machine."
   
# Define variables for the scale set
$resourceGroupName = "myVMSSRG"
$scaleSetName = "myScaleSet"
$location = "China North"

# Create a resource group
New-AzResourceGroup -ResourceGroupName $resourceGroupName -Location $location

# Create a networking pieces
$subnet = New-AzVirtualNetworkSubnetConfig `
  -Name "mySubnet" `
  -AddressPrefix 10.0.0.0/24
$vnet = New-AzVirtualNetwork `
  -ResourceGroupName $resourceGroupName `
  -Name "myVnet" `
  -Location $location `
  -AddressPrefix 10.0.0.0/16 `
  -Subnet $subnet
$publicIP = New-AzPublicIpAddress `
  -ResourceGroupName $resourceGroupName `
  -Location $location `
  -AllocationMethod Static `
  -Name "myPublicIP"
$frontendIP = New-AzLoadBalancerFrontendIpConfig `
  -Name "myFrontEndPool" `
  -PublicIpAddress $publicIP
$backendPool = New-AzLoadBalancerBackendAddressPoolConfig -Name "myBackEndPool"
$inboundNATPool = New-AzLoadBalancerInboundNatPoolConfig `
  -Name "myRDPRule" `
  -FrontendIpConfigurationId $frontendIP.Id `
  -Protocol TCP `
  -FrontendPortRangeStart 50001 `
  -FrontendPortRangeEnd 50010 `
  -BackendPort 3389
# Create the load balancer and health probe
$lb = New-AzLoadBalancer `
  -ResourceGroupName $resourceGroupName `
  -Name "myLoadBalancer" `
  -Location $location `
  -FrontendIpConfiguration $frontendIP `
  -BackendAddressPool $backendPool `
  -InboundNatPool $inboundNATPool
Add-AzLoadBalancerProbeConfig -Name "myHealthProbe" `
  -LoadBalancer $lb `
  -Protocol TCP `
  -Port 80 `
  -IntervalInSeconds 15 `
  -ProbeCount 2
Add-AzLoadBalancerRuleConfig `
  -Name "myLoadBalancerRule" `
  -LoadBalancer $lb `
  -FrontendIpConfiguration $lb.FrontendIpConfigurations[0] `
  -BackendAddressPool $lb.BackendAddressPools[0] `
  -Protocol TCP `
  -FrontendPort 80 `
  -BackendPort 80 `
  -Probe (Get-AzLoadBalancerProbeConfig -Name "myHealthProbe" -LoadBalancer $lb)
Set-AzLoadBalancer -LoadBalancer $lb

# Create IP address configurations
$ipConfig = New-AzVmssIpConfig `
  -Name "myIPConfig" `
  -LoadBalancerBackendAddressPoolsId $lb.BackendAddressPools[0].Id `
  -LoadBalancerInboundNatPoolsId $inboundNATPool.Id `
  -SubnetId $vnet.Subnets[0].Id

# Create a configuration 
$vmssConfig = New-AzVmssConfig `
    -Location $location `
    -SkuCapacity 2 `
    -SkuName "Standard_DS2" `
    -UpgradePolicyMode "Automatic"

# Reference the image version
Set-AzVmssStorageProfile $vmssConfig `
  -OsDiskCreateOption "FromImage" `
  -ImageReferenceId $imageDefinition.Id

# Complete the configuration
Set-AzVmssOsProfile $vmssConfig `
  -AdminUsername $cred.UserName `
  -AdminPassword $cred.Password `
  -ComputerNamePrefix "myVM"
Add-AzVmssNetworkInterfaceConfiguration `
  -VirtualMachineScaleSet $vmssConfig `
  -Name "network-config" `
  -Primary $true `
  -IPConfiguration $ipConfig

# Create the scale set 
New-AzVmss `
  -ResourceGroupName $resourceGroupName `
  -Name $scaleSetName `
  -VirtualMachineScaleSet $vmssConfig

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

后续步骤Next steps

此外可以使用模板创建共享映像库资源。You can also Shared Image Gallery resource using templates. 提供多个 Azure 快速入门模板:There are several Azure Quickstart Templates available:

有关共享映像库的详细信息,请参阅概述For more information about Shared Image Galleries, see the Overview. 如果遇到问题,请参阅排查共享映像库问题If you run into issues, see Troubleshooting shared image galleries.