使用 Azure CLI 从托管映像迁移到映像版本Migrate from a managed image to an image version using the Azure CLI

如果打算将现有托管映像迁移到共享映像库,可以直接从托管映像创建共享映像库映像。If you have an existing managed image that you would like to migrate into a Shared Image Gallery, you can create a Shared Image Gallery image directly from the managed image. 测试新映像后,可以删除源托管映像。Once you have tested your new image, you can delete the source managed image. 还可以使用 PowerShell 从托管映像迁移到共享映像库。You can also migrate from a managed image to a Shared Image Gallery using PowerShell.

映像库中的映像具有两个组件,我们将在此示例中创建这两个组件:Images in an image gallery have two components, which we will create in this example:

  • “映像定义”包含有关映像及其使用要求的信息。An Image definition carries information about the image and requirements for using it. 这包括该映像是 Windows 映像还是 Linux 映像、是专用映像还是通用映像,此外还包括发行说明以及最低和最高内存要求。This includes whether the image is Windows or Linux, specialized or generalized, release notes, and minimum and maximum memory requirements. 它是某种映像类型的定义。It is a definition of a type of image.
  • 使用共享映像库时,将使用映像版本来创建 VM。An image version is what is used to create a VM when using a Shared Image Gallery. 可根据环境的需要创建多个映像版本。You can have multiple versions of an image as needed for your environment. 创建 VM 时,将使用该映像版本为 VM 创建新磁盘。When you create a VM, the image version is used to create new disks for the VM. 可以多次使用映像版本。Image versions can be used multiple times.

准备阶段Before you begin

若要完成本文,必须具有现有的共享映像库To complete this article, you must have an existing Shared Image Gallery.

若要完成本文中的示例,必须具有通用化 VM 的现有托管映像。To complete the example in this article, you must have an existing managed image of a generalized VM. 有关详细信息,请参阅捕获托管映像For more information, see Capture a managed image. 如果托管映像包含数据磁盘,则数据磁盘大小不能超过 1 TB。If the managed image contains a data disk, the data disk size cannot be more than 1 TB.

通过本文进行操作时,请根据需要替换资源组和 VM 名称。When working through this article, replace the resource group and VM names where needed.

创建映像定义Create an image definition

由于托管映像始终是通用化映像,因此你将使用通用化映像的 --os-state generalized 创建映像定义。Because managed images are always generalized images, you will create a an image definition using --os-state generalized for a generalized image.

映像定义名称可能包含大写或小写字母、数字、点、短划线和句点。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.

使用 az sig image-definition create 在库中创建一个映像定义。Create an image definition in the gallery using az sig image-definition create.

在此示例中,映像定义名为 myImageDefinition,适用于通用化 Linux OS 映像。In this example, the image definition is named myImageDefinition, and is for a generalized Linux OS image. 若要使用 Windows OS 创建映像的定义,请使用 --os-type WindowsTo create a definition for images using a Windows OS, use --os-type Windows.

resourceGroup=myGalleryRG
gallery=myGallery
imageDef=myImageDefinition
az sig image-definition create \
   --resource-group $resourceGroup \
   --gallery-name $gallery \
   --gallery-image-definition $imageDef \
   --publisher myPublisher \
   --offer myOffer \
   --sku mySKU \
   --os-type Linux \
   --os-state generalized

创建映像版本Create the image version

使用 az image gallery create-image-version 创建版本。Create versions using az image gallery create-image-version. 你需要传入托管映像的 ID 以作为创建映像版本时要使用的基线。You will need to pass in the ID of the managed image to use as a baseline for creating the image version. 可以使用 az image list 获取映像的 ID。You can use az image list to get the IDs for your images.

az image list --query "[].[name, id]" -o tsv

允许用于映像版本的字符为数字和句点。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,我们打算使用区域冗余存储在“中国东部”区域创建 1 个副本,在“中国东部 2”区域创建 1 个副本。In this example, the version of our image is 1.0.0 and we are going to create 1 replica in the China East region and 1 replica in the China East 2 region using zone-redundant storage. 选择复制的目标区域时,请记住,你还需包括源区域作为复制的目标。When choosing target regions for replication, remember that you also have to include the source region as a target for replication.

--managed-image 参数中传递托管映像的 ID。Pass the ID of the managed image in the --managed-image parameter.

imageID="/subscriptions/<subscription ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myImage"
az sig image-version create \
   --resource-group $resourceGroup \
   --gallery-name $gallery \
   --gallery-image-definition $imageDef \
   --gallery-image-version 1.0.0 \
   --target-regions "chinaeast=1" "chinaeast2=1=standard_lrs" \
   --replica-count 2 \
   --managed-image $imageID

备注

需等待映像版本彻底生成并复制完毕,然后才能使用同一托管映像来创建另一映像版本。You need to wait for the image version to completely finish being built and replicated before you can use the same managed image to create another image version.

也可在高级冗余存储中存储所有映像版本副本,只需在创建映像版本时添加 --storage-account-type premium_lrs 即可。You can also store all of your image version replicas in Premium Redundant Storage by adding --storage-account-type premium_lrs when you create the image version.

后续步骤Next steps

通用化映像版本创建 VM。Create a VM from a generalized image version.