8.3. 从通用虚拟机准备 Azure 镜像版本
要从通用虚拟机创建可重复使用的 Azure 镜像版本,您必须首先创建一个资源组来组织相关资源,如计算、网络和存储。在这个资源组中,设置一个 Azure Compute Gallery 来管理并跨机构共享镜像。在 gallery 中定义镜像定义,以对镜像进行逻辑分组并指定其属性和要求。基于这些镜像定义,您可以创建多个镜像版本以实现一致性和可扩展性。
使用镜像版本,您可以创建同一镜像的副本和多个版本。使用 Azure Compute Gallery,您可以创建一个市场兼容的自定义镜像来跨机构共享。您可以使用 Azure CLI 或 Azure Cloud Shell。有关 Azure Cloud Shell 的详情,请参阅开始使用 Azure Cloud Shell。
先决条件
- 已安装 Azure CLI。详情请参阅 安装 Azure CLI。
- 您已创建了通用虚拟机。详情请参阅 为创建镜像正式发布 Azure 虚拟机。
流程
创建用于托管 gallery 的资源组:
$ az group create --name <example_image_resource_group>在以上资源组中创建 gallery:
$ az sig create --resource-group <example_image_resource_group> \ --gallery-name <example_image_gallery_name>为订阅将安全类型设置为
Standard:$ az feature register --name UseStandardSecurityType \ --namespace Microsoft.Compute注册供应商:
$ az provider register --namespace Microsoft.Compute创建镜像定义来管理镜像版本:
$ az sig image-definition create --resource-group <example_image_resource_group> \ --gallery-name <example_image_gallery_name> \ --gallery-image-definition <example_image_definition> \ --publisher <example_publisher> \ --offer <example_offer> \ --sku <example_sky> \ --os-type Linux \ --os-state Generalized \ --hyper-v-generation V2 \ --features SecurityType=Standard- <publisher>
- 提供镜像的实体或机构。
- <offer>
- 来自发布者的相关镜像集合。
- <stock Keeping Unit (SKU)>
- 提供的版本,表示主版本。
- <VERSION>
- 给定 SKU 的版本号。
获取有关镜像的信息:
$ az vm list --output table使用输出中的常规镜像名称:
$ az vm get-instance-view --resource-group <example_vm_resource_group> \ --name <example_vm_name> \ --query id- 从输出中复制镜像定义的 ID。
使用上一步中获取的镜像 ID 创建镜像版本:
$ az sig image-version create \ --resource-group <example_image_resource_group> \ --gallery-name <example_image_gallery_name> \ --gallery-image-definition <example_image_definition> \ --gallery-image-version <example_version> \ --virtual-machine <example_id>可选:删除虚拟机和相关资源:
$ az vm delete --resource-group <example_image_resource_group> \ --name <example_vm_name>