适用于:✔️ 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 chinaeast
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 = "China East"
$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 通过验证后,选择页面底部的“创建”以开始部署。
后续步骤