教程:使用 Azure CLI 创建 Azure VM 的自定义映像Tutorial: Create a custom image of an Azure VM with the Azure CLI

自定义映像类似于市场映像,不同的是自定义映像的创建者是自己。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. 在本教程中,你将创建自己的 Azure 虚拟机自定义映像。In this tutorial, you create your own custom image of an Azure virtual machine. 学习如何: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

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.4.0 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.4.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

概述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.

准备阶段Before you begin

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

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

映像库是用于启用映像共享的主要资源。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.

使用 az sig create 创建一个映像库。Create an image gallery using az sig create. 以下示例在“中国北部”创建一个名为“myGalleryRG”的资源组命名库,以及一个名为“myGallery”的库** ** **。The following example creates a resource group named gallery named myGalleryRG in China North, and a gallery named myGallery.

az group create --name myGalleryRG --location chinanorth
az sig create --resource-group myGalleryRG --gallery-name myGallery

获取有关 VM 的信息Get information about the VM

可以使用 az vm list 查看可用 VM 的列表。You can see a list of VMs that are available using az vm list.

az vm list --output table

知道 VM 的名称及其所在的资源组后,使用 az vm get-instance-view 获取 VM 的 ID。Once you know the VM name and what resource group it is in, get the ID of the VM using az vm get-instance-view.

az vm get-instance-view -g MyResourceGroup -n MyVm --query id

复制 VM 的 ID 供稍后使用。Copy the ID of your VM to use later.

创建映像定义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.

使用 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 specialized Linux OS image.

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

复制输出中的映像定义 ID 供稍后使用。Copy the ID of the image definition from the output to use later.

创建映像版本Create the image version

使用 az image gallery create-image-version 从 VM 创建映像版本。Create an image version from the VM using az image gallery create-image-version.

允许用于映像版本的字符为数字和句点。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,并且我们打算使用区域冗余存储在“中国北部”区域创建 2 个副本,在“中国东部区域”创建 1 个副本,在“中国东部 2”区域创建 1 个副本** ** ** **。In this example, the version of our image is 1.0.0 and we are going to create 2 replicas in the China North region, 1 replica in the China East region and 1 replica in the China East 2 region using zone-redundant storage. 复制区域必须包含源 VM 所在的区域。The replication regions must include the region the source VM is located.

请将此示例中的 --managed-image 值替换为上一步的 VM ID。Replace the value of --managed-image in this example with the ID of your VM from the previous step.

az sig image-version create \
   --resource-group myGalleryRG \
   --gallery-name myGallery \
   --gallery-image-definition myImageDefinition \
   --gallery-image-version 1.0.0 \
   --target-regions "chinanorth" "chinaeast=1" "chinaeast2=1=standard_zrs" \
   --replica-count 2 \
   --managed-image "/subscriptions/<Subscription ID>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM"

备注

需等待映像版本彻底生成并复制完毕,然后才能使用同一托管映像来创建另一映像版本。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 your image in Premiun storage by a adding --storage-account-type premium_lrs when you create the image version.

创建 VMCreate the VM

结合 --specialized 参数使用 az vm create 创建 VM 可以指明该映像是专用化映像。Create the VM using az vm create using the --specialized parameter to indicate the the image is a specialized image.

使用 --image 的映像定义 ID 从可用的最新映像版本创建 VM。Use the image definition ID for --image to create the VM from the latest version of the image that is available. 还可以通过为 --image 提供映像版本 ID 从特定版本创建 VM。You can also create the VM from a specific version by supplying the image version ID for --image.

在此示例中,我们将从 myImageDefinition 映像的最新版本创建 VM。In this example, we are creating a VM from the latest version of the myImageDefinition image.

az group create --name myResourceGroup --location chinanorth
az vm create --resource-group myResourceGroup \
    --name myVM \
    --image "/subscriptions/<Subscription ID>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition" \
    --specialized

可以使用基于角色的访问控制 (RBAC) 在订阅之间共享映像。You can share images across subscriptions using Role-Based Access Control (RBAC). 可以在库、映像定义或映像版本级别共享映像。You can share images at the gallery, image definition or image version leve. 任何对映像版本具有读取权限的用户,即使跨订阅,也能够使用映像版本部署 VM。Any user that has read permissions to an image version, even across subscriptions, will be able to deploy a VM using the image version.

建议在库级别与其他用户进行共享。We recommend that you share with other users at the gallery level. 若要获取库的对象 ID,请使用 az sig showTo get the object ID of your gallery, use az sig show.

az sig show \
   --resource-group myGalleryRG \
   --gallery-name myGallery \
   --query id

使用对象 ID 作为范围,并使用电子邮件地址和 az role assignment create 为用户授予对共享映像库的访问权限。Use the object ID as a scope, along with an email address and az role assignment create to give a user access to the shared image gallery. 请将 <email-address><gallery iD> 替换为自己的信息。Replace <email-address> and <gallery iD> with your own information.

az role assignment create \
   --role "Reader" \
   --assignee <email address> \
   --scope <gallery ID>

有关如何使用 RBAC 共享资源的详细信息,请参阅使用 RBAC 和 Azure CLI 管理访问权限For more information about how to share resources using RBAC, see Manage access using RBAC and Azure CLI.

后续步骤Next steps

在本教程中,已创建自定义 VM 映像。In this tutorial, you created a custom 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 highly available virtual machines.