教程:通过 Azure CLI 创建和使用虚拟机规模集的自定义映像Tutorial: Create and use a custom image for virtual machine scale sets with the Azure CLI

创建规模集时,需指定部署 VM 实例时要使用的映像。When you create a scale set, you specify an image to be used when the VM instances are deployed. 若要在部署 VM 实例之后减少任务数目,可以使用自定义 VM 映像。To reduce the number of tasks after VM instances are deployed, you can use a custom VM image. 在此自定义 VM 映像中可以完成全部所需的应用程序安装或配置步骤。This custom VM image includes any required application installs or configurations. 在规模集中创建的任何 VM 实例使用自定义 VM 映像,并随时可为应用程序流量提供服务。Any VM instances created in the scale set use the custom VM image and are ready to serve your application traffic. 本教程介绍如何执行下列操作:In this tutorial you learn how to:

  • 创建共享映像库Create a Shared Image Gallery
  • 创建专用化映像定义Create a specialized image definition
  • 创建映像版本Create an image version
  • 从专用化映像创建规模集Create a scale set from a specialized image
  • 共享映像库Share an image gallery

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don’t have an Azure subscription, create a Trial before you begin.

如果选择在本地安装并使用 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.

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

创建并配置源 VMCreate and configure a source VM

首先使用 az group create 创建资源组,然后使用 az vm create 创建 VM。First, create a resource group with az group create, then create a VM with az vm create. 然后,此 VM 将用作映像的源。This VM is then used as the source for the image. 以下示例在名为 myResourceGroup 的资源组中创建名为 myVM 的 VM:The following example creates a VM named myVM in the resource group named myResourceGroup:

az group create --name myResourceGroup --location chinanorth

az vm create \
  --resource-group myResourceGroup \
  --name myVM \
  --image ubuntults \
  --admin-username azureuser \
  --generate-ssh-keys

重要

az vm create 命令的输出中会显示 VM 的 ID。The ID of your VM is shown in the output of the az vm create command. 请将此 ID 复制到安全的位置,以便稍后在本教程中使用。Copy this someplace safe so you can use it later in this tutorial.

az vm create 命令的输出中还会显示 VM 的公共 IP 地址。The public IP address of your VM is also shown in the output of the az vm create command. 通过 SSH 连接到 VM 的公共 IP 地址,如下所示:SSH to the public IP address of your VM as follows:

ssh azureuser@<publicIpAddress>

为了自定义 VM,让我们安装一个基本的 Web 服务器。To customize your VM, let's install a basic web server. 部署规模集中的 VM 实例时,该实例会包含运行 Web 应用程序的全部所需包。When the VM instance in the scale set would be deployed, it would then have all the required packages to run a web application. 按如下所示,使用 apt-get 安装 NGINXUse apt-get to install NGINX as follows:

sudo apt-get install -y nginx

完成后,键入 exit 以断开 SSH 连接。When you are done, type exit to disconnect the SSH connection.

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

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

请确保映像定义的类型正确。Make sure your image definition is the right type. 如果已通用化 VM(使用适用于 Windows 的 Sysprep,或适用于 Linux 的 waagent -deprovision),则应使用 --os-state generalized 创建通用化映像定义。If you have generalized the VM (using Sysprep for Windows, or waagent -deprovision for Linux) then you should create a generalized image definition using --os-state generalized. 若要在不删除现有用户帐户的情况下使用 VM,请使用 --os-state specialized 创建专用化映像定义。If you want to use the VM without removing existing user accounts, create a specialized image definition using --os-state specialized.

若要详细了解可为映像定义指定的值,请参阅映像定义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. 若要使用 Windows OS 创建映像的定义,请使用 --os-type WindowsTo create a definition for images using a Windows OS, use --os-type Windows.

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。The ID of your image definition is shown in the output of the command. 请将此 ID 复制到安全的位置,以便稍后在本教程中使用。Copy this someplace safe so you can use it later in this tutorial.

创建映像版本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,我们将在“中国北部”区域中创建 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 North region and 1 replica in the China North 2 region. 复制区域必须包含源 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 "chinanorth2=1" "chinanorth=1" \
   --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 在高级存储中存储映像,或者通过添加 --storage-account-type standard_zrs区域冗余存储中存储映像。You can also store your image in Premium storage by a adding --storage-account-type premium_lrs, or Zone Redundant Storage by adding --storage-account-type standard_zrs when you create the image version.

从映像创建规模集Create a scale set from the image

使用 az vmss create 从专用化映像创建规模集。Create a scale set from the specialized image using az vmss create.

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

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

从前面创建的最新 myImageDefinition 映像版本创建名为“myScaleSet”的规模集 。Create a scale set named myScaleSet the latest version of the myImageDefinition image we created earlier.

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

创建和配置所有的规模集资源和 VM 需要几分钟时间。It takes a few minutes to create and configure all the scale set resources and VMs.

测试规模集Test your scale set

若要允许流量抵达规模集并验证 Web 服务器是否正常工作,请使用 az network lb rule create 命令创建负载均衡器规则。To allow traffic to reach your scale set and that verify that the web server works correctly, create a load balancer rule with az network lb rule create. 以下示例创建名为 myLoadBalancerRuleWeb 的规则,该规则允许 TCP 端口 80 上的流量:The following example creates a rule named myLoadBalancerRuleWeb that allows traffic on TCP port 80:

az network lb rule create \
  --resource-group myResourceGroup \
  --name myLoadBalancerRuleWeb \
  --lb-name myScaleSetLB \
  --backend-pool-name myScaleSetLBBEPool \
  --backend-port 80 \
  --frontend-ip-name loadBalancerFrontEnd \
  --frontend-port 80 \
  --protocol tcp

若要查看规模集的运行方式,请使用 az network public-ip show 获取负载均衡器的公共 IP 地址。To see your scale set in action, get the public IP address of your load balancer with az network public-ip show. 以下示例获取创建为规模集一部分的 myScaleSetLBPublicIP 的 IP 地址:The following example gets the IP address for myScaleSetLBPublicIP created as part of the scale set:

az network public-ip show \
  --resource-group myResourceGroup \
  --name myScaleSetLBPublicIP \
  --query [ipAddress] \
  --output tsv

在 Web 浏览器中键入公共 IP 地址。Type the public IP address into your web browser. 随后会显示默认的 NGINX 网页,如以下示例所示:The default NGINX web page is displayed, as shown in the following example:

从自定义 VM 映像运行的 Nginx

可以使用基于角色的访问控制 (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. 任何对映像版本具有读取权限的用户,即使跨订阅,也能够使用映像版本部署 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.

清理资源Clean up resources

若要删除规模集和其他资源,请使用 az group delete 删除资源组及其所有资源。To remove your scale set and additional resources, delete the resource group and all its resources with az group delete. --no-wait 参数会使光标返回提示符处,无需等待操作完成。The --no-wait parameter returns control to the prompt without waiting for the operation to complete. --yes 参数将确认是否希望删除资源,而不会有额外提示。The --yes parameter confirms that you wish to delete the resources without an additional prompt to do so.

az group delete --name myResourceGroup --no-wait --yes

后续步骤Next steps

本教程已介绍如何通过 Azure CLI 创建和使用规模集的自定义 VM 映像:In this tutorial, you learned how to create and use a custom VM image for your scale sets with the Azure CLI:

  • 创建共享映像库Create a Shared Image Gallery
  • 创建专用化映像定义Create a specialized image definition
  • 创建映像版本Create an image version
  • 从专用化映像创建规模集Create a scale set from a specialized image
  • 共享映像库Share an image gallery

请继续学习下一教程,了解如何将应用程序部署到规模集。Advance to the next tutorial to learn how to deploy applications to your scale set.