共用方式為

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

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

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

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

在您开始之前

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

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

概述

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

使用 Azure 计算库可以与他人共享自定义 VM 映像。 选择要共享哪些映像、要在其中提供哪些区域以及要与之共享的映像。

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

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

启动 Azure PowerShell

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

获取 VM

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

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

创建资源组

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

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

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

库是用于启用映像共享的主要资源。 库名称的允许字符为大写或小写字母、数字、点和句点。 库名称不能包含短划线。 库名称在订阅中必须是唯一的。

使用 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 ,并复制到 中国东部 和中国 北部 数据中心。 为复制选择目标区域时,需要将 区域作为复制目标。

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

   $region1 = @{Name='China East';ReplicaCount=1}
   $region2 = @{Name='China North';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"
$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
  • 共享库

转到下一教程,了解虚拟机规模集。