教程:使用 Azure PowerShell 创建 Windows VM 映像

适用于:✔️ Windows VM ✔️ 灵活规模集

映像可用于启动部署,并确保跨多个 VM 的一致性。 在本教程中,你将使用 PowerShell 创建自己的 Azure 虚拟机专用映像,并将其存储在 Azure 计算库(以前称为共享映像库)中。 您将了解如何执行以下操作:

  • 创建 Azure Compute Gallery
  • 创建映像定义
  • 创建映像版本
  • 从映像创建 VM
  • 共享库

在您开始之前

以下步骤详细介绍了如何获取现有 VM 并将其转换为可用于创建新 VM 的可重用自定义映像。

若要完成本教程中的示例,必须具有现有的虚拟机。 如果需要,可以查看 PowerShell 快速入门 ,以创建要用于本教程的 VM。 完成本教程时,请根据需要替换资源名称。

概述

Azure Compute Gallery 可简化在整个组织范围内共享自定义映像。 自定义映像类似于市场映像,但你自己创建它们。 自定义映像可用于启动配置,例如预加载应用程序、应用程序配置和其他 OS 配置。

Azure 计算库可让你与他人共享自定义虚拟机映像。 选择要共享哪些映像、要在哪些区域提供这些映像,以及要与哪些对象共享这些映像。

Azure 计算库功能具有多种资源类型:

Resource Description
图像源 这是一个可用于在映像库中创建映像版本的资源。 映像源可以是现有的 Azure VM(已通用化或专用化)、托管映像、快照,或者另一个映像库中的映像版本。
画廊 与 Azure 市场一样,是一个用于管理和共享映像及VM 应用程序的存储库,但你可以控制谁有权访问。
映像定义 映像定义是在库中创建的,包含有关该映像以及在内部使用该映像的要求的信息。 这包括映像是 Windows 还是 Linux、发行说明以及最小和最大内存要求。 它是图像类型的定义。
映像版本 映像版本是在使用映像库时用于创建 VM 的版本。 你可以根据环境需要使用同一映像的多个版本。 与托管映像一样,使用 映像版本 创建 VM 时,映像版本用于为 VM 创建新磁盘。 映像版本可以多次使用。

启动 Azure PowerShell

打开 Azure Powershell 控制台,并使用管理员特权运行以下脚本。

获取虚拟机

可以使用 Get-AzVM 查看资源组中可用的 VM 列表。 知道 VM 名称和资源组后,可以再次使用该 Get-AzVM VM 对象并将其存储在变量中以供以后使用。 此示例从 myResourceGroup 资源组获取名为 sourceVM 的 VM,并将其分配给变量$sourceVM

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

创建资源组

使用 New-AzResourceGroup 命令创建资源组。

Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。 在以下示例中,在 ChinaEast2 区域中创建了名为 myGalleryRG 的资源组:

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

库是用于实现映像共享的主要资源。 库名称允许使用的字符包括大写和小写字母、数字、点号和句点。 图库名称不能包含连字符。 在你的订阅范围内,库集名称必须唯一。

使用 New-AzGallery 创建图库。 以下示例在 myGalleryRG 资源组中创建名为 myGallery 的 库。

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

创建映像定义

映像定义为映像创建逻辑分组。 它们用于管理有关在其中创建的映像版本的信息。 图像定义名称可以由大写或小写字母、数字、点、短划线和句点组成。 有关可以为映像定义指定的值的详细信息,请参阅 映像定义

使用 New-AzGalleryImageDefinition 创建映像定义。 在此示例中,映像库映像名为myGalleryImage,并且是为专用映像创建的。

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

创建映像版本

使用 New-AzGalleryImageVersion 从 VM 创建映像版本。

映像版本的允许字符是数字和句点。 数字必须在 32 位整数范围内。 格式:MajorVersion.MinorVersion.Patch

在此示例中,映像版本为 1.0.0 ,并复制到 中国东部 2 和中国 北部 2 数据中心。 为复制选择目标区域时,需要将 区域作为复制目标。

若要从 VM 创建映像版本,请使用源 VM 的资源 ID 作为 -sourceImageVMId。

   $region1 = @{Name='China East 2';ReplicaCount=1}
   $region2 = @{Name='China North 2';ReplicaCount=2}
   $targetRegions = @($region1,$region2)
   $sourceImageVMId = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGalleryRG/providers/Microsoft.Compute/virtualMachines/sourceVM"

New-AzGalleryImageVersion `
   -GalleryImageDefinitionName $galleryImage.Name`
   -GalleryImageVersionName '1.0.0' `
   -GalleryName $gallery.Name `
   -ResourceGroupName $resourceGroup.ResourceGroupName `
   -Location $resourceGroup.Location `
   -TargetRegion $targetRegions  `
   -SourceImageVMId $sourceImageVMId `
   -PublishingProfileEndOfLifeDate '2030-12-01'

将映像复制到所有目标区域可能需要一段时间。

创建 VM

拥有专用映像后,可以创建一个或多个新 VM。 使用 New-AzVM cmdlet。 若要使用映像,请使用 Set-AzVMSourceImage 并设置 -Id 映像定义 ID(在本例中为 $galleryImage.Id),以始终使用最新的映像版本。

在此示例中根据需要替换资源名称。

# Create some variables for the new VM.
$resourceGroup = "myResourceGroup"
$location = "China North 2"
$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 Deny
$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

我们建议您在库级别共享访问权限。 使用电子邮件地址和 Get-AzADUser cmdlet 获取用户的对象 ID,然后使用 New-AzRoleAssignment 向其授予对库的访问权限。 将此示例中的示例电子邮件 alinne_montes@contoso.com 替换为你自己的信息。

# 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

清理资源

不再需要时,可以使用 Remove-AzResourceGroup cmdlet 删除资源组和所有相关资源:

# Delete the gallery 
Remove-AzResourceGroup -Name myGalleryRG

# Delete the VM
Remove-AzResourceGroup -Name myResoureceGroup

Azure 映像生成器

Azure 还提供基于 Packer、 Azure VM 映像生成器构建的服务。 只需在模板中描述您的自定义设置,它就会负责创建映像。

后续步骤

在本教程中,你创建了一个专用 VM 映像。 你已了解如何执行以下操作:

  • 创建 Azure Compute Gallery
  • 创建镜像定义
  • 创建映像版本
  • 从映像创建 VM
  • 共享库

继续学习下一个教程,了解虚拟机规模集。