适用于:✔️ Linux VM ✔️ Windows VM
从 Azure Compute Gallery(以前称为共享映像库)中存储的专用化映像版本创建 VM。 若要使用通用映像版本创建 VM,请参阅从通用映像版本创建 VM。
本文介绍如何从专用映像创建 VM:
重要
从专用化映像创建新 VM 时,新 VM 会保留原始 VM 的计算机名。 其他特定于计算机的信息(如 CMID)也会保留。 此重复信息可能会导致问题。 复制 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
结合 --specialized
参数使用 az vm create 创建 VM 可以指明该映像是专用映像。
使用 --image
的映像定义 ID 从可用的最新映像版本创建 VM。 还可以通过为 --image
提供映像版本 ID 从特定版本创建 VM。
在此示例中,我们将从 myImageDefinition 映像的最新版本创建 VM。
az group create --name myResourceGroup --___location eastus
az vm create --resource-group myResourceGroup \
--name myVM \
--image "/subscriptions/<Subscription ID>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition" \
--specialized
获得专用化映像版本后,可以使用 New-AzVM cmdlet 创建一个或多个新 VM。
在此示例中,我们使用映像定义 ID 来确保新 VM 会使用最新版本的映像。 也可通过将映像版本 ID 用作 Set-AzVMSourceImage -Id
来使用特定版本。 例如,若要使用映像版本 1.0.0,请键入:Set-AzVMSourceImage -Id "/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0"
。
使用特定映像版本意味着:如果该特定映像版本由于已从区域中删除或移除而无法使用,则自动化可能会失败。 建议使用映像定义 ID 来创建新的 VM(除非需要特定的映像版本)。
在此示例中,请根据需要替换资源名称。
# Create some variables for the new VM.
$resourceGroup = "mySIGSpecializedRG"
$___location = "South Central US"
$vmName = "mySpecializedVM"
# Get the image. Replace the name of your resource group, gallery, and image definition. This will create the VM from the latest image version available.
$imageDefinition = Get-AzGalleryImageDefinition `
-GalleryName myGallery `
-ResourceGroupName myResourceGroup `
-Name myImageDefinition
# Create a resource group
New-AzResourceGroup -Name $resourceGroup -Location $___location
# Create the network resources.
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name mySubnet `
-AddressPrefix 192.168.1.0/24
$vnet = New-AzVirtualNetwork `
-ResourceGroupName $resourceGroup `
-Location $___location `
-Name MYvNET `
-AddressPrefix 192.168.0.0/16 `
-Subnet $subnetConfig
$pip = New-AzPublicIpAddress `
-ResourceGroupName $resourceGroup `
-Location $___location `
-Name "mypublicdns$(Get-Random)" `
-AllocationMethod Static `
-IdleTimeoutInMinutes 4
$nsgRuleRDP = New-AzNetworkSecurityRuleConfig `
-Name myNetworkSecurityGroupRuleRDP `
-Protocol Tcp `
-Direction Inbound `
-Priority 1000 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Deny
$nsg = New-AzNetworkSecurityGroup `
-ResourceGroupName $resourceGroup `
-Location $___location `
-Name myNetworkSecurityGroup `
-SecurityRules $nsgRuleRDP
$nic = New-AzNetworkInterface `
-Name $vmName `
-ResourceGroupName $resourceGroup `
-Location $___location `
-SubnetId $vnet.Subnets[0].Id `
-PublicIpAddressId $pip.Id `
-NetworkSecurityGroupId $nsg.Id
# Create a virtual machine configuration using Set-AzVMSourceImage -Id $imageDefinition.Id to use the latest available image version.
$vmConfig = New-AzVMConfig `
-VMName $vmName `
-VMSize Standard_D1_v2 | `
Set-AzVMSourceImage -Id $imageDefinition.Id | `
Add-AzVMNetworkInterface -Id $nic.Id
# Create a virtual machine
New-AzVM `
-ResourceGroupName $resourceGroup `
-Location $___location `
-VM $vmConfig
现在,可以创建一个或多个新的 VM。 本示例在美国东部数据中心的“myResourceGroup”中创建名为“myVM”的 VM 。
- 转到映像定义。 可以使用资源筛选器显示所有可用的映像定义。
- 在映像定义的页面顶部,从菜单中选择“创建 VM”。
- 对于“资源组”,请选择“新建”并键入 myResourceGroup 作为名称。
- 在“虚拟机名称”中键入 myVM。
- 对于“区域”,请选择“美国东部”。
- 对于“可用性选项”,请保留默认设置“无需基础结构冗余”。
- 如果你是从映像定义的页面开始操作的,系统会自动使用
latest
映像版本填充“映像”的值。
- 对于“大小”,请从可用大小列表中选择一种 VM 大小,然后选择“选择”。
- 由于使用的是源 VM 提供的用户名和凭据,因此“管理员帐户”下的用户名会灰显。
- 如果要允许远程访问 VM,请在“公共入站端口”下选择“允许所选端口”,然后从下拉列表中选择“SSH (22)”或“RDP (3389)” 。 如果你不希望允许远程访问 VM,请为“公共入站端口”保留选择“无”。
- 完成后,选择页面底部的“查看 + 创建”按钮。
- VM 通过验证后,选择页面底部的“创建”以开始部署。
RBAC - 在组织内部
如果库所在的订阅位于同一租户中,则通过 RBAC 共享的映像可用于通过 CLI 和 PowerShell 创建 VM。
你需要所要使用的映像的 imageID
,并确保映像已复制到你要创建 VM 的区域。
image="/subscriptions/<Subscription ID>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition"
vmResourceGroup='myResourceGroup'
___location='westus'
vmName='myVM'
az group create --name $vmResourceGroup --___location $___location
az vm create\
--resource-group $vmResourceGroup \
--name $vmName \
--image $image \
--specialized
# Create some variables for the new VM.
$resourceGroup = "myResourceGroup"
$___location = "South Central US"
$vmName = "myVM"
$image = "/subscriptions/<Subscription ID>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition"
# Create a resource group
New-AzResourceGroup -Name $resourceGroup -Location $___location
# Create the network resources.
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name mySubnet `
-AddressPrefix 192.168.1.0/24
$vnet = New-AzVirtualNetwork `
-ResourceGroupName $resourceGroup `
-Location $___location `
-Name MYvNET `
-AddressPrefix 192.168.0.0/16 `
-Subnet $subnetConfig
$pip = New-AzPublicIpAddress `
-ResourceGroupName $resourceGroup `
-Location $___location `
-Name "mypublicdns$(Get-Random)" `
-AllocationMethod Static `
-IdleTimeoutInMinutes 4
$nsgRuleRDP = New-AzNetworkSecurityRuleConfig `
-Name myNetworkSecurityGroupRuleRDP `
-Protocol Tcp `
-Direction Inbound `
-Priority 1000 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Deny
$nsg = New-AzNetworkSecurityGroup `
-ResourceGroupName $resourceGroup `
-Location $___location `
-Name myNetworkSecurityGroup `
-SecurityRules $nsgRuleRDP
$nic = New-AzNetworkInterface `
-Name $vmName `
-ResourceGroupName $resourceGroup `
-Location $___location `
-SubnetId $vnet.Subnets[0].Id `
-PublicIpAddressId $pip.Id `
-NetworkSecurityGroupId $nsg.Id
# Create a virtual machine configuration using Set-AzVMSourceImage -Id $imageDefinition.Id to use the latest available image version.
$vmConfig = New-AzVMConfig `
-VMName $vmName `
-VMSize Standard_D1_v2 | `
Set-AzVMSourceImage $image | `
Add-AzVMNetworkInterface -Id $nic.Id
# Create a virtual machine
New-AzVM `
-ResourceGroupName $resourceGroup `
-Location $___location `
-VM $vmConfig
RBAC - 来自其他租户或组织
如果你要使用的映像存储在某个不在同一租户(目录)中的库中,则需登录到每个租户以验证你是否拥有访问权限。
你需要所要使用的映像的 imageID
,并确保映像已复制到你要创建 VM 的区域。 你还需要用于源库的 tenantID
,以及与用于创建 VM 的位置对应的 tenantID
。
你需要登录到存储映像的租户,获取访问令牌,然后登录到你要在其中创建 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
结合 --specialized
参数使用 az vm create 创建 VM 可以指明该映像是专用映像。
imageid=""/subscriptions/<Subscription ID>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition""
resourcegroup="myResourceGroup"
___location="West US 3"
name='myVM'
az group create --name $resourcegroup --___location $___location
az vm create --resource-group $resourcegroup \
--name $name \
--image $image \
--specialized
你需要登录到存储映像的租户,获取访问令牌,然后登录到你要在其中创建 VM 的租户。 这是 Azure 验证你是否有权访问映像的方式。
$tenant1 = "<Tenant 1 ID>"
$tenant2 = "<Tenant 2 ID>"
Connect-AzAccount -Tenant "<Tenant 1 ID>" -UseDeviceAuthentication
Connect-AzAccount -Tenant "<Tenant 2 ID>" -UseDeviceAuthentication
创建 VM。 请将示例中的信息替换为你自己的。 在创建 VM 之前,请确保将映像复制到你要在其中创建 VM 的区域。
# Create some variables for the new VM.
$resourceGroup = "myResourceGroup"
$___location = "South Central US"
$vmName = "myVM"
# Set a variable for the image version in Tenant 1 using the full image ID of the image version
$image = "/subscriptions/<Tenant 1 subscription>/resourceGroups/<Resource group>/providers/Microsoft.Compute/galleries/<Gallery>/images/<Image definition>/versions/<version>"
# Create a resource group
New-AzResourceGroup -Name $resourceGroup -Location $___location
# Create the network resources.
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name mySubnet `
-AddressPrefix 192.168.1.0/24
$vnet = New-AzVirtualNetwork `
-ResourceGroupName $resourceGroup `
-Location $___location `
-Name MYvNET `
-AddressPrefix 192.168.0.0/16 `
-Subnet $subnetConfig
$pip = New-AzPublicIpAddress `
-ResourceGroupName $resourceGroup `
-Location $___location `
-Name "mypublicdns$(Get-Random)" `
-AllocationMethod Static `
-IdleTimeoutInMinutes 4
$nsgRuleRDP = New-AzNetworkSecurityRuleConfig `
-Name myNetworkSecurityGroupRuleRDP `
-Protocol Tcp `
-Direction Inbound `
-Priority 1000 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Deny
$nsg = New-AzNetworkSecurityGroup `
-ResourceGroupName $resourceGroup `
-Location $___location `
-Name myNetworkSecurityGroup `
-SecurityRules $nsgRuleRDP
$nic = New-AzNetworkInterface `
-Name $vmName `
-ResourceGroupName $resourceGroup `
-Location $___location `
-SubnetId $vnet.Subnets[0].Id `
-PublicIpAddressId $pip.Id `
-NetworkSecurityGroupId $nsg.Id
# Create a virtual machine configuration using Set-AzVMSourceImage -Id $imageDefinition.Id to use the latest available image version.
$vmConfig = New-AzVMConfig `
-VMName $vmName `
-VMSize Standard_D1_v2 | `
Set-AzVMSourceImage -Id $image | `
Add-AzVMNetworkInterface -Id $nic.Id
# Create a virtual machine
New-AzVM `
-ResourceGroupName $resourceGroup `
-Location $___location `
-VM $vmConfig
重要
Microsoft 不为社区库中的映像提供支持。
使用社区提交的虚拟机映像存在多种风险。 映像可能包含恶意软件、安全漏洞或侵犯某人的知识产权。 为了帮助为社区创建安全可靠的体验,可以在发现这些问题时报告映像。
报告社区图库问题的最简单方法是使用门户,该门户将预先填写报告信息:
- 对于映像定义字段中的链接或其他信息的问题,请选择“报告社区映像”。
- 如果映像版本包含恶意代码或特定版本的映像存在其他问题,请选择映像版本表中“报告版本”列下的“报告”。
还可以使用以下链接来报告问题,但系统不会预先填充表单:
若要使用共享到社区库的映像创建 VM,请使用 --image
的映像的唯一 ID,其格式如下:
/CommunityGalleries/<community gallery name, like: ContosoImages-1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f>/Images/<image name>/Versions/latest
作为最终用户,若要获取社区库的公共名称,需要使用门户。 转到“虚拟机”>“创建”>“Azure 虚拟机”>“映像”>“查看所有映像”>“社区映像”>“公共库名称”。
使用 az sig image-definition list-community 列出社区库中可用的所有映像定义。 在此示例中,我们列出美国西部的 ContosoImage 库中的所有映像,以及创建 VM 所需的唯一 ID(按名称)、OS 和 OS 状态。
az sig image-definition list-community \
--public-gallery-name "ContosoImages-1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f" \
--___location westus \
--query [*]."{Name:name,ID:uniqueId,OS:osType,State:osState}" -o table
若要使用社区库中的通用映像创建 VM,请参阅从通用映像版本创建 VM。
结合 --specialized
参数使用 az vm create 创建 VM 可以指明该映像是专用映像。
在此示例中,我们将从 myImageDefinition 映像的最新版本创建 VM。
az group create --name myResourceGroup --___location eastus
az vm create --resource-group myResourceGroup \
--name myVM \
--image "/CommunityGalleries/ContosoImages-f61bb1d9-3c5a-4ad2-99b5-744030225de6/Images/LinuxSpecializedVersions/latest" \
--specialized
使用社区映像时,系统会提示你接受法律条款。 消息将如下所示:
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):
- 在搜索中键入“虚拟机”。
- 在“服务”下,选择“虚拟机” 。
- 在“虚拟机”页面中,选择“创建”,然后选择“虚拟机” 。 此时将打开“创建虚拟机”页。
- 在“基本信息”选项卡中的“项目详细信息”下,确保选择了正确的订阅,然后选择“新建资源组”或从下拉列表中选择一个。
- 在“实例详细信息”下,为“虚拟机名称”键入一个名称。
- 对于“安全类型”,请确保选择了“标准”。
- 对于“映像”,请选择“查看所有映像”。 “选择映像”页将打开。
- 在左侧菜单的“其他项”下,选择“社区映像”。 “其他项 | 社区映像”页将打开。
- 从列表中删除映像。 确保 OS 状态为“专用”。 若要使用专用映像,请参阅使用通用映像创建创建 VM。 根据选择的映像,将更改将在其中创建 VM 的区域以匹配映像。
- 完成其余选项,然后选择页面底部的“查看 + 创建”按钮。
- 在“创建虚拟机”页上,可以查看要创建的 VM 的详细信息。 准备就绪后,选择“创建”。
直接共享库
要使用共享到订阅或租户的映像的最新版本创建 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
请确保映像的状态为 Specialized
。 如果要使用 Generalized
状态的映像,请参阅从通用映像版本创建 VM。
结合 --specialized
参数使用 az vm create 创建 VM 可以指明该映像是专用映像。
使用 Id
(追加有 /Versions/latest
以使用最新版本)作为 `--image`` 的值来创建 VM。
在此示例中,我们将从 myImageDefinition 映像的最新版本创建 VM。
imgDef="/SharedGalleries/1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f-MYDIRECTSHARED/Images/myDirectDefinition/Versions/latest"
vmResourceGroup=myResourceGroup
___location=westus
vmName=myVM
az group create --name $vmResourceGroup --___location $___location
az vm create\
--resource-group $vmResourceGroup \
--name $vmName \
--image $imgDef \
--specialized
注意
已知问题:在 Azure 门户中,如果选择某个区域,选择某个映像,然后更改该区域,你将收到一条错误消息:“只能在此映像的复制区域中创建 VM”,即使该映像已复制到该区域。 要消除该错误,请选择其他区域,然后切换回所需的区域。 如果该映像可用,应会清除错误消息。
还可使用 Azure CLI 检查与你共享的映像。 例如,可使用 `az sig list-shared --___location westus`` 查看在美国西部区域与你共享的映像。
- 在搜索中键入“虚拟机”。
- 在“服务”下,选择“虚拟机” 。
- 在“虚拟机”页面中,选择“创建”,然后选择“虚拟机” 。 此时将打开“创建虚拟机”页。
- 在“基本信息”选项卡中的“项目详细信息”下,确保选择了正确的订阅,然后选择“新建资源组”或从下拉列表中选择一个。
- 在“实例详细信息”下,为“虚拟机名称”键入一个名称。
- 对于“安全类型”,请确保选择了“标准”。
- 对于“映像”,请选择“查看所有映像”。 “选择映像”页将打开。
- 在左侧菜单中的“其他项”下,选择“直接共享映像(预览版)”。 “其他项 | 直接共享映像(预览版)”页将打开。
- 从列表中删除映像。 确保 OS 状态为“专用”。 要使用通用映像,请参阅使用通用映像版本创建 VM。 根据选择的映像,将在其中创建 VM 的区域将会更改以匹配映像。
- 完成其余选项,然后选择页面底部的“查看 + 创建”按钮。
- 在“创建虚拟机”页上,可以查看要创建的 VM 的详细信息。 准备就绪后,选择“创建”。
后续步骤