首页 > 解决方案 > 如何从快照创建 VM 规模集

问题描述

我想创建一个 VM 规模集并使用快照作为我的 Windows VM 的基础。由于 Set-AzureRmVmssStorageProfile 仅接受图像,我的第一次尝试是通过使用将快照转换为图像:

$rgName = #...
$location = #...
$snapshotName = "mySnapshot"
$imageName = "myImage"

$snapshot = Get-AzureRmSnapshot -ResourceGroupName $rgName -SnapshotName $snapshotName

$imageConfig = New-AzureRmImageConfig -Location $location
$imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -SnapshotId $snapshot.Id

New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig

但在这种情况下,创建的图像没有源 Blob URI:

在此处输入图像描述

是什么给了我错误:

New-AzureRmVmss :URI Microsoft.Azure.Commands.Compute.Automation.Models.PSImage 看起来不是正确的 blob URI。

在我的天蓝色部署命令中:

$vmss = New-AzureRmVmssConfig -Location $loc -SkuCapacity 2 -SkuName "Standard_DS1_v2" -UpgradePolicyMode "manual" -ErrorAction Stop

Add-AzureRmVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmss -Name "vmssNetwork" -Primary $true -IPConfiguration $ipConfig 
Set-AzureRmVmssStorageProfile -VirtualMachineScaleSet $vmss -OsDiskCreateOption "FromImage" -OsDiskCaching "None" `
            -Image $ImgRef -OsDiskOsType Windows -OsDiskName "C"
Set-AzureRmVmssOSProfile -ComputerNamePrefix $vmNamePrefix -AdminUsername $adminUsername -AdminPassword $adminPassword -VirtualMachineScaleSet $vmss

New-AzureRmVmss -ResourceGroupName $currentrg -Name $vmssName -VirtualMachineScaleSet $vmss -Verbose -ErrorAction Stop;

是否有其他方法来创建图像或设置源 blob uri?或者是否可以使用快照来创建 VM 规模集?

-- 编辑 1 --

根据Charles Xu的提示,我将图像创建更改为首先创建一个dik,但我仍然得到同样的错误。新代码是:

$rgName = #...
$location = #...
$snapshotName = "mySnapshot"
$imageName = "myImage"
$storageType = 'Standard_LRS'
$diskName = "myDisk"

$snapshot = Get-AzureRmSnapshot -ResourceGroupName $rgName -SnapshotName $snapshotName

$diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $rgName -DiskName $diskName

$imageConfig = New-AzureRmImageConfig -Location $location
$imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -ManagedDiskId $disk.Id     

New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig

标签: azureazure-devopsazure-powershellazure-vm-scale-set

解决方案


可以从 Azure VM 创建映像。例如,您可以通过 PowerShell 从 Windows VM 创建 Windows 映像,请参阅使用 Azure PowerShell 创建和使用虚拟机规模集的自定义映像。当映像正常时,只需像这样创建 VMSS:

New-AzureRmVmss `
  -ResourceGroupName "myResourceGroup" `
  -Location "EastUS" `
  -VMScaleSetName "myScaleSet" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -PublicIpAddressName "myPublicIPAddress" `
  -LoadBalancerName "myLoadBalancer" `
  -UpgradePolicyMode "Automatic" `
  -ImageName "yourImage"

此外,快照没问题,但您应该先从快照创建映像。然后从映像创建 VMSS。使用 command New-AzureRmImage,图像应该是托管图像,因此您看不到 URI。只需在命令中使用托管映像 ID,如下所示:

Set-AzureRmVmssStorageProfile -VirtualMachineScaleSet $vmss -OsDiskCreateOption "FromImage" -OsDiskCaching "None" -ImageReferenceId yourImageId -OsDiskOsType Windows -OsDiskName "C"

只是谈谈如何创建图像,我建议你使用Packer ,这里有一个例子。

更新

我假设您的自定义图像已准备好。像这样的 PowerShell 脚本:

#Get the custom image 
$image = Get-AzureRmImage -ResourceGroupName charlesTerraform -ImageName myPackerImage

# Get the existing Vnet
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName charlesTerraform -Name pakcerVnet

#IP configuration
$ipName = "ipConfig"

#create the IP configuration
$ipConfig = New-AzureRmVmssIpConfig -Name $ipName -LoadBalancerBackendAddressPoolsId $null -SubnetId $vnet.Subnets[0].Id

#create vmss configuration
$vmss = New-AzureRmVmssConfig -Location "East US" -SkuCapacity 2 -SkuName "Standard_DS1_v2" -UpgradePolicyMode "manual" -ErrorAction Stop

##Add the network interface configuration to the scale set configuration
Add-AzureRmVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmss -Name "vmssNetwork" -Primary $true -IPConfiguration $ipConfig 

# set the stroage profile 
Set-AzureRmVmssStorageProfile -VirtualMachineScaleSet $vmss -OsDiskCreateOption "FromImage" -OsDiskCaching "None" -ImageReferenceId $image.Id -OsDiskOsType Linux 

#set the os profile
Set-AzureRmVmssOSProfile -ComputerNamePrefix "Test" -AdminUsername "azureuser" -AdminPassword "azureuser@2018" -VirtualMachineScaleSet $vmss

#create the vmss
New-AzureRmVmss -ResourceGroupName charlesTerraform -Name TestVmss -VirtualMachineScaleSet $vmss

推荐阅读