你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

从通用化映像版本创建 VM

从 Azure Compute Gallery(以前称为“共享映像库”)中存储的通用化映像版本创建 VM。 若要使用专用化映像创建 VM,请参阅从专用化映像创建 VM

本文介绍如何从通用映像创建 VM:

使用 az sig image-definition list 列出库中的映像定义,以查看定义的名称和 ID。

resourceGroup=myGalleryRG
gallery=myGallery
az sig image-definition list --resource-group $resourceGroup --gallery-name $gallery --query "[].[name, id]" --output tsv

运行 az vm create 创建 VM。 若要使用最新版本的映像,请将 --image 设置为映像定义的 ID。

此示例演示如何创建一个使用 SSH 保护的 Linux VM。 对于 Windows 或要使用密码保护 Linux VM,请删除 --generate-ssh-keys,使系统提示输入密码。 如果要直接提供密码,请将 --generate-ssh-keys 替换为 --admin-password。 在此示例中,请根据需要替换资源名称。

imgDef="/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition"
vmResourceGroup=myResourceGroup
location=eastus
vmName=myVM
adminUsername=azureuser

az group create --name $vmResourceGroup --location $location

az vm create\
   --resource-group $vmResourceGroup \
   --name $vmName \
   --image $imgDef \
   --admin-username $adminUsername \
   --generate-ssh-keys

也可以通过使用 --image 参数的映像版本 ID 来使用特定版本。 例如,若要使用映像版本 1.0.0,请键入:--image "/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0"

RBAC - 在组织内共享

如果库所在的订阅位于同一租户中,则通过 RBAC 共享的映像可用于通过 CLI 和 PowerShell 创建 VM。

需要获取你要使用的映像的 imageID,并确保映像已复制到你要创建 VM 的区域。

请确保映像的状态为 Generalized。 如果要使用具有 Specialized 状态的映像,请参阅从专用映像版本创建 VM

imgDef="/SharedGalleries/1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f-MYDIRECTSHARED/Images/myDirectDefinition/Versions/latest"
vmResourceGroup=myResourceGroup
location=westus
vmName=myVM
adminUsername=azureuser

az group create --name $vmResourceGroup --location $location

az vm create\
   --resource-group $vmResourceGroup \
   --name $vmName \
   --image $imgDef \
   --admin-username $adminUsername \
   --generate-ssh-keys

RBAC - 从另一个租户共享

如果你要使用的映像存储在某个不在同一租户(目录)中的库中,则需登录到每个租户以验证你是否拥有访问权限。

还需要所要使用的映像的 imageID,并确保映像已复制到你要创建 VM 的区域。 你还需要用于源库的 tenantID,以及与用于创建 VM 的位置对应的 tenantID

此示例演示如何根据通用化映像创建 VM。 如果使用的是专用化映像,请参阅使用专用化映像版本创建 VM

你需要登录到存储映像的租户,获取访问令牌,然后登录到你要在其中创建 VM 的租户。 这是 Azure 验证你是否有权访问映像的方式。


tenant1='<ID for tenant 1>'
tenant2='<ID for tenant 2>'

az account clear
az login --tenant $tenant1
az account get-access-token 
az login --tenant $tenant2
az account get-access-token
az login --tenant $tenant1
az account get-access-token

创建 VM。 请将示例中的信息替换为你自己的。 在创建 VM 之前,请确保将映像复制到你要在其中创建 VM 的区域。

imageid="<ID of the image that you want to use>"
resourcegroup="<name for the resource group>"
location="<location where the image is replicated>"
user='<username for the VM>'
name='<name for the VM>'

az group create --location $location --resource-group $resourcegroup
az vm create \
  --resource-group $resourcegroup \
  --name $name \
  --image $imageid \
  --admin-username $user \
  --generate-ssh-keys

重要

Microsoft 不为社区库中的映像提供支持。

报告社区映像的问题

使用社区提交的虚拟机映像存在多种风险。 映像可能包含恶意软件、安全漏洞或侵犯某人的知识产权。 为了帮助为社区创建安全可靠的体验,可以在发现这些问题时报告映像。

报告社区图库问题的最简单方法是使用门户,该门户将预先填写报告信息:

  • 对于映像定义字段中的链接或其他信息的问题,请选择“报告社区映像”。
  • 如果映像版本包含恶意代码或特定版本的映像存在其他问题,请选择映像版本表中“报告版本”列下的“报告”。

还可以使用以下链接来报告问题,但系统不会预先填充表单:

若要使用共享到社区库的映像创建 VM,请使用 --image 的映像的唯一 ID,其格式如下:

/CommunityGalleries/<community gallery name, like: ContosoImages-1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f>/Images/<image name>/Versions/latest

按照以下说明使用 CLI 获取社区映像列表:

Step 1:  Show all 'Community images' in a specific location
az sig list-community --location westus2

Step 2: Once you have the public gallery name from Step 1, Get the Image definition (Name) of the image by running the following command
az sig image-definition list-community --public-gallery-name <<public gallery name>> --location westus2

Step 3: Finally, run the following command to list different image versions available for the specific image
az sig image-version list-community --public-gallery-name <<galleryname>> --gallery-image-definition <<image name>> --location westus2

从门户获取社区库的公共名称。 转到“虚拟机”>“创建”>“Azure 虚拟机”>“映像”>“查看所有映像”>“社区映像”>“公共库名称”。

在此示例中,我们将使用 Linux 映像创建 VM,并创建 SSH 密钥进行身份验证。

imgDef="/CommunityGalleries/ContosoImages-1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f>/Images/myLinuxImage/Versions/latest"
vmResourceGroup=myResourceGroup
location=eastus
vmName=myVM
adminUsername=azureuser

az group create --name $vmResourceGroup --location $location

az vm create\
   --resource-group $vmResourceGroup \
   --name $vmName \
   --image $imgDef \
   --admin-username $adminUsername \
   --generate-ssh-keys

使用社区映像时,系统会提示你接受法律条款。 消息如下所示:

To create the VM from community gallery image, you must accept the license agreement and privacy statement: http://contoso.com. (If you want to accept the legal terms by default, please use the option '--accept-term' when creating VM/VMSS) (Y/n): 

重要

Azure Compute Gallery - 直接共享库目前处于预览阶段,并受 Azure Compute Gallery 预览条款的约束。

要在预览期间将映像发布到直接共享库,需要在 https://aka.ms/directsharedgallery-preview 注册。 从直接共享库创建 VM 的功能将对所有 Azure 用户开放。

在预览期间,需要新建一个库,并将属性 sharingProfile.permissions 设置为 Groups。 使用 CLI 创建库时,请使用 --permissions groups 参数。 不能使用现有库,当前无法更新该属性。

要使用共享到订阅或租户的映像创建 VM,需要采用以下格式的映像的唯一 ID:

/SharedGalleries/<uniqueID>/Images/<image name>/Versions/latest

要查找与你共享的库的 uniqueID,请使用 az sig list-shared。 在本例中,我们探讨的是美国西部区域的库。

region=westus
az sig list-shared --location $region --query "[].name" -o tsv

使用库名称查找可用的映像。 在此示例中,我们列出美国西部的所有映像,以及创建 VM 所需的唯一 ID(按名称)、OS 和 OS 状态。

galleryName="1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f-myDirectShared"
 az sig image-definition list-shared \
   --gallery-unique-name $galleryName \
   --location $region \
   --query [*]."{Name:name,ID:uniqueId,OS:osType,State:osState}" -o table

请确保映像的状态为 Generalized。 如果要使用具有 Specialized 状态的映像,请参阅从专用映像版本创建 VM

使用输出中的 Id(追加有 /Versions/latest 以使用最新版本)作为 --image 的值来创建 VM。 在此示例中,我们从直接共享给我们的 Linux 映像创建 VM,并创建用于身份验证的 SSH 密钥。

imgDef="/SharedGalleries/1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f-MYDIRECTSHARED/Images/myDirectDefinition/Versions/latest"
vmResourceGroup=myResourceGroup
location=westus
vmName=myVM
adminUsername=azureuser

az group create --name $vmResourceGroup --location $location

az vm create\
   --resource-group $vmResourceGroup \
   --name $vmName \
   --image $imgDef \
   --admin-username $adminUsername \
   --generate-ssh-keys

后续步骤