教程:使用 Azure PowerShell 创建 Windows VM 映像Tutorial: Create Windows VM images with Azure PowerShell

映像可用于启动部署并确保多个 VM 的一致性。Images can be used to bootstrap deployments and ensure consistency across multiple VMs. 在本教程中,我们使用 PowerShell 创建自己的 Azure 虚拟机专用化映像,并将其存储在共享映像库中。In this tutorial, you create your own specialized image of an Azure virtual machine using PowerShell and store it in a Shared Image Gallery. 学习如何:You learn how to:

  • 创建共享映像库Create a Shared Image Gallery
  • 创建映像定义Create an image definition
  • 创建映像版本Create an image version
  • 从映像创建 VMCreate a VM from an image
  • 共享映像库Share an image gallery

准备阶段Before you begin

下列步骤详细说明如何将现有 VM 转换为可重用自定义映像,以便将其用于创建新的 VM。The steps below detail how to take an existing VM and turn it into a re-usable custom image that you can use to create new VMs.

若要完成本教程中的示例,必须现有一个虚拟机。To complete the example in this tutorial, you must have an existing virtual machine. 如果需要,可以参阅 PowerShell 快速入门来创建本教程所用的 VM。If needed, you can see the PowerShell quickstart to create a VM to use for this tutorial. 在学习本教程期间,请根据需要替换资源名称。When working through the tutorial, replace the resource names where needed.

概述Overview

共享映像库大大简化了整个组织中的自定义映像共享。A Shared Image Gallery simplifies custom image sharing across your organization. 自定义映像类似于市场映像,不同的是自定义映像的创建者是自己。Custom images are like marketplace images, but you create them yourself. 自定义映像可用于启动配置,例如预加载应用程序、应用程序配置和其他 OS 配置。Custom images can be used to bootstrap configurations such as preloading applications, application configurations, and other OS configurations.

共享映像库可让你与他人共享自定义 VM 映像。The Shared Image Gallery lets you share your custom VM images with others. 选择要共享哪些映像,要在哪些区域中共享,以及希望与谁共享它们。Choose which images you want to share, which regions you want to make them available in, and who you want to share them with.

共享映像库功能具有多种资源类型:The Shared Image Gallery feature has multiple resource types:

资源Resource 说明Description
映像源Image source 这是可用于在映像库中创建“映像版本”的资源 。This is a resource that can be used to create an image version in an image gallery. 映像源可以是现有的 Azure VM(可以是通用或专用的)、托管映像、快照或其他映像库中的映像版本。An image source can be an existing Azure VM that is either generalized or specialized, a managed image, a snapshot, or an image version in another image gallery.
映像库Image gallery 与 Azure 市场一样,映像库是用于管理和共享映像的存储库,但你可以控制谁有权访问这些映像。Like the Azure Marketplace, an image gallery is a repository for managing and sharing images, but you control who has access.
映像定义Image definition 映像定义在库中创建,携带有关该映像以及在内部使用该映像的要求的信息。Image definitions are created within a gallery and carry information about the image and requirements for using it internally. 这包括了该映像是 Windows 还是 Linux 映像、发行说明以及最低和最高内存要求。This includes whether the image is Windows or Linux, release notes, and minimum and maximum memory requirements. 它是某种映像类型的定义。It is a definition of a type of image.
映像版本Image version 使用库时,将使用映像版本来创建 VM。An image version is what you use to create a VM when using a gallery. 可根据环境的需要创建多个映像版本。You can have multiple versions of an image as needed for your environment. 与托管映像一样,在使用映像版本创建 VM 时,将使用映像版本来创建 VM 的新磁盘。Like a managed image, when you use an image version to create a VM, the image version is used to create new disks for the VM. 可以多次使用映像版本。Image versions can be used multiple times.

启动 Azure 本地 ShellLaunch Azure local Shell

打开 Azure Powershell 控制台,以管理员权限运行下面列出的脚本。Open the Azure Powershell console and run the scripts listed below with Administrator privilege.

获取 VMGet the VM

可以使用 Get-AzVM 查看资源组中可用的 VM 列表。You can see a list of VMs that are available in a resource group using Get-AzVM. 了解 VM 名称和资源组后,可以再次使用 Get-AzVM 来获取 VM 对象并将其存储在变量中,供稍后使用。Once you know the VM name and what resource group, you can use Get-AzVM again to get the VM object and store it in a variable to use later. 此示例从“myResourceGroup”资源组获取名为 sourceVM 的 VM,并将其分配给变量 $sourceVM 。This example gets an VM named sourceVM from the "myResourceGroup" resource group and assigns it to the variable $sourceVM .

$sourceVM = Get-AzVM `
   -Name sourceVM `
   -ResourceGroupName myResourceGroup

创建资源组Create a resource group

使用 New-AzResourceGroup 命令创建资源组。Create a resource group with the New-AzResourceGroup command.

Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 以下示例在“ChinaEast”区域中创建名为“myGalleryRG”的资源组 :In the following example, a resource group named myGalleryRG is created in the ChinaEast region:

$resourceGroup = New-AzResourceGroup `
   -Name 'myGalleryRG' `
   -Location 'ChinaEast'

映像库是用于启用映像共享的主要资源。An image gallery is the primary resource used for enabling image sharing. 允许用于库名称的字符为大写或小写字母、数字、点和句点。Allowed characters for gallery name are uppercase or lowercase letters, digits, dots, and periods. 库名称不能包含短划线。The gallery name cannot contain dashes. 库名称在你的订阅中必须唯一。Gallery names must be unique within your subscription.

使用 New-AzGallery 创建映像库。Create an image gallery using New-AzGallery. 以下示例在“myGalleryRG”资源组中创建名为“myGallery”的库 。The following example creates a gallery named myGallery in the myGalleryRG resource group.

$gallery = New-AzGallery `
   -GalleryName 'myGallery' `
   -ResourceGroupName $resourceGroup.ResourceGroupName `
   -Location $resourceGroup.Location `
   -Description 'Shared Image Gallery for my organization'  

创建映像定义Create an image definition

映像定义为映像创建一个逻辑分组。Image definitions create a logical grouping for images. 映像定义用于管理在其中创建的映像版本的相关信息。They are used to manage information about the image versions that are created within them. 映像定义名称可能包含大写或小写字母、数字、点、短划线和句点。Image definition names can be made up of uppercase or lowercase letters, digits, dots, dashes and periods. 若要详细了解可以为映像定义指定的值,请参阅映像定义For more information about the values you can specify for an image definition, see Image definitions.

使用 New-AzGalleryImageDefinition 创建映像定义。Create the image definition using New-AzGalleryImageDefinition. 在此示例中,库映像名为 myGalleryImage,它是为专用化映像创建的。In this example, the gallery image is named myGalleryImage and is created for a specialized image.

$galleryImage = New-AzGalleryImageDefinition `
   -GalleryName $gallery.Name `
   -ResourceGroupName $resourceGroup.ResourceGroupName `
   -Location $gallery.Location `
   -Name 'myImageDefinition' `
   -OsState specialized `
   -OsType Windows `
   -Publisher 'myPublisher' `
   -Offer 'myOffer' `
   -Sku 'mySKU'

创建映像版本Create an image version

使用 New-AzGalleryImageVersion 从 VM 创建映像版本。Create an image version from a VM using New-AzGalleryImageVersion.

允许用于映像版本的字符为数字和句点。Allowed characters for image version are numbers and periods. 数字必须在 32 位整数范围内。Numbers must be within the range of a 32-bit integer. 格式: MajorVersion . MinorVersion . PatchFormat: MajorVersion . MinorVersion . Patch .

在此示例中,映像版本为 1.0.0,该版本将复制到“中国东部”和“中国北部”数据中心 。In this example, the image version is 1.0.0 and it's replicated to both China East and China North datacenters. 选择复制的目标区域时,需要将源区域包含为复制目标。When choosing target regions for replication, you need to include the source region as a target for replication.

若要从 VM 创建映像版本,请对 -Source 使用 $vm.Id.ToString()To create an image version from the VM, use $vm.Id.ToString() for the -Source.

   $region1 = @{Name='China East';ReplicaCount=1}
   $region2 = @{Name='China North';ReplicaCount=2}
   $targetRegions = @($region1,$region2)

New-AzGalleryImageVersion `
   -GalleryImageDefinitionName $galleryImage.Name`
   -GalleryImageVersionName '1.0.0' `
   -GalleryName $gallery.Name `
   -ResourceGroupName $resourceGroup.ResourceGroupName `
   -Location $resourceGroup.Location `
   -TargetRegion $targetRegions  `
   -Source $sourceVM.Id.ToString() `
   -PublishingProfileEndOfLifeDate '2020-12-01'

可能需要一段时间才能将映像复制到所有目标区域。It can take a while to replicate the image to all of the target regions.

创建 VMCreate a VM

获得专用化映像后,可以创建一个或多个新 VM。Once you have a specialized image, you can create one or more new VMs. 使用 New-AzVM cmdlet。Using the New-AzVM cmdlet. 要使用映像,请使用 Set-AzVMSourceImage 并将 -Id 设置为映像定义 ID(在本例中为 $galleryImage.Id),以始终使用最新的映像版本。To use the image, use Set-AzVMSourceImage and set the -Id to the image definition ID ($galleryImage.Id in this case) to always use the latest image version.

在此示例中,请根据需要替换资源名称。Replace resource names as needed in this example.

# Create some variables for the new VM.
$resourceGroup = "myResourceGroup"
$location = "China North"
$vmName = "mySpecializedVM"

# Create a resource group
New-AzResourceGroup -Name $resourceGroup -Location $location

# Create the network resources.
$subnetConfig = New-AzVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24
$vnet = New-AzVirtualNetwork -ResourceGroupName $resourceGroup -Location $location `
  -Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig
$pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
  -Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4
$nsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP  -Protocol Tcp `
  -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
  -DestinationPortRange 3389 -Access Allow
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
  -Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP
$nic = New-AzNetworkInterface -Name $vmName -ResourceGroupName $resourceGroup -Location $location `
  -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

# Create a virtual machine configuration using $imageVersion.Id to specify the image version.
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize Standard_D1_v2 | `
Set-AzVMSourceImage -Id $galleryImage.Id | `
Add-AzVMNetworkInterface -Id $nic.Id

# Create a virtual machine
New-AzVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig

建议在映像库级别共享访问权限。We recommend that you share access at the image gallery level. 使用电子邮件地址和 Get-AzADUser cmdlet 获取用户的对象 ID,然后使用 New-AzRoleAssignment 为用户授予对库的访问权限。Use an email address and the Get-AzADUser cmdlet to get the object ID for the user, then use New-AzRoleAssignment to give them access to the gallery. 请将此示例中的示例电子邮件地址 alinne_montes@contoso.com 替换为你自己的信息。Replace the example email, alinne_montes@contoso.com in this example, with your own information.

# Get the object ID for the user
$user = Get-AzADUser -StartsWith alinne_montes@contoso.com
# Grant access to the user for our gallery
New-AzRoleAssignment `
   -ObjectId $user.Id `
   -RoleDefinitionName Reader `
   -ResourceName $gallery.Name `
   -ResourceType Microsoft.Compute/galleries `
   -ResourceGroupName $resourceGroup.ResourceGroupName

清理资源Clean up resources

不再需要时,可以使用 Remove-AzResourceGroup cmdlet 删除资源组和所有相关资源:When no longer needed, you can use the Remove-AzResourceGroup cmdlet to remove the resource group, and all related resources:

# Delete the gallery 
Remove-AzResourceGroup -Name myGalleryRG

# Delete the VM
Remove-AzResourceGroup -Name myResoureceGroup

后续步骤Next steps

在本教程中,你已创建一个专用化 VM 映像。In this tutorial, you created a specialized VM image. 你已了解如何执行以下操作:You learned how to:

  • 创建共享映像库Create a Shared Image Gallery
  • 创建映像定义Create an image definition
  • 创建映像版本Create an image version
  • 从映像创建 VMCreate a VM from an image
  • 共享映像库Share an image gallery

请转到下一教程,了解如何创建高度可用的虚拟机。Advance to the next tutorial to learn about how to create highly available virtual machines.