【问题标题】:Deploy VM on Azure using created (generalized/sysprep'ed) snapshot stored on Blob使用存储在 Blob 上的已创建(通用/系统准备)快照在 Azure 上部署 VM
【发布时间】:2018-03-16 12:22:38
【问题描述】:

我需要一个对 Azure 上的 VM 更有经验的人。我已经用 Win2016 Server 和一些额外的配置准备了镜像。之后我完成了 sysprep 并创建了映像和快照。当我使用此映像创建新机器时,机器出现故障并且没有事件启动。所以我找到了另一个解决方案:从这台机器上拍摄快照并传输到 blob。以下代码执行后新机器启动,但我无法连接 RDP...

错误:VM 部署时出现错误:New-AzureRmVM:长时间运行的操作失败,状态为“失败”

$resourceGroupName="<resource-group>"
$subscriptionId = "<subs>"
$tenantId = "<tenant>"
$location = "West Europe"

# sign in
Write-Host "Logging in...";
Login-AzureRmAccount -TenantID $tenantID;

# select subscription
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzureRmSubscription -SubscriptionID $subscriptionId;


# Create a Resource Group 
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location


#Prepare the VM parameters 
$rgName = $resourceGroupName
# Get the Azure location and storage account names
$locName=(Get-AzureRmResourceGroup -Name $rgName).Location

$exSubnet=New-AzureRMVirtualNetworkSubnetConfig -Name EX2016Subnet -AddressPrefix 10.0.0.0/24
New-AzureRMVirtualNetwork -Name EX2016Vnet -ResourceGroupName $rgName -Location $locName -AddressPrefix 10.0.0.0/16 -Subnet $exSubnet -DNSServer 10.0.0.5
$rule1 = New-AzureRMNetworkSecurityRuleConfig -Name "RDPTraffic" -Description "Allow RDP to all VMs on the subnet" -Access Allow -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$rule2 = New-AzureRMNetworkSecurityRuleConfig -Name "ExchangeSecureWebTraffic" -Description "Allow HTTPS to the Exchange server" -Access Allow -Protocol Tcp -Direction Inbound -Priority 101 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix "10.0.0.5/32" -DestinationPortRange 443
$rule3 = New-AzureRMNetworkSecurityRuleConfig -Name "ExchangeSecurePortExternal" -Description "Allow access port 25 to the Exchange server" -Access Allow -Protocol Tcp -Direction Inbound -Priority 102 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix "10.0.0.5/32" -DestinationPortRange 25
New-AzureRMNetworkSecurityGroup -Name EX2016Subnet -ResourceGroupName $rgName -Location $locName -SecurityRules $rule1, $rule2,$rule3
$vnet=Get-AzureRMVirtualNetwork -ResourceGroupName $rgName -Name EX2016Vnet
$nsg=Get-AzureRMNetworkSecurityGroup -Name EX2016Subnet -ResourceGroupName $rgName
Set-AzureRMVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name EX2016Subnet -AddressPrefix "10.0.0.0/24" -NetworkSecurityGroup $nsg

# Specify the virtual machine name and size
$vmName="exVM"
$vmSize="Standard_D3_v2"
$vnet=Get-AzureRMVirtualNetwork -Name "EX2016Vnet" -ResourceGroupName $rgName
$vm=New-AzureRMVMConfig -VMName $vmName -VMSize $vmSize

# Create the NIC for the virtual machine
$nicName=$vmName + "-NIC"
$pipName=$vmName + "-PublicIP"
$pip=New-AzureRMPublicIpAddress -Name $pipName -ResourceGroupName $rgName -DomainNameLabel $vmDNSName -Location $locName -AllocationMethod Dynamic
$nic=New-AzureRMNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -PrivateIpAddress "10.0.0.5"

# Specify the image and local administrator account, and then add the NIC
$cred=Get-Credential -Message "Type the name and password of the local administrator account for exVM."


$osDiskName = "VM01-OSDisk"
$osDiskUri = "https://xxxxxx.blob.core.windows.net/images/Image.vhd"

$storageAccountType = "StandardLRS"
$vm=Add-AzureRMVMNetworkInterface -VM $vm -Id $nic.Id

$osDisk = New-AzureRmDisk -DiskName $osDiskName -Disk (New-AzureRmDiskConfig -AccountType $storageAccountType -Location $location -CreateOption Import -SourceUri $osDiskUri) -ResourceGroupName $rgName
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType $storageAccountType -DiskSizeInGB 128 -CreateOption Attach -Windows

$vm = Set-AzureRmVMBootDiagnostics -VM $vm -disable
#Create the new VM
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm

【问题讨论】:

    标签: azure azure-storage azure-disk


    【解决方案1】:

    您可以使用 blob 创建快照并使用快照创建新 VM,您可以查看 example。

    $snapshot = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName 
    
    $diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
    
    $disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $osDiskName
    
    #Initialize virtual machine configuration
    $VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize
    
    #Use the Managed Disk Resource Id to attach it to the virtual machine. Please change the OS type to linux if OS disk has linux OS
    $VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Linux
    
    #Create a public IP for the VM  
    $publicIp = New-AzureRmPublicIpAddress -Name ($VirtualMachineName.ToLower()+'_ip') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -AllocationMethod Dynamic
    
    #Get the virtual network where virtual machine will be hosted
    $vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName
    
    # Create NIC in the first subnet of the virtual network 
    $nic = New-AzureRmNetworkInterface -Name ($VirtualMachineName.ToLower()+'_nic') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id
    
    $VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $nic.Id
    
    #Create the virtual machine with Managed Disk
    New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $snapshot.Location
    

    您还可以检查此link 以从 blob 创建快照。

    【讨论】:

    • 嗨,谢谢你的帮助,我想我已经差不多完成了,我找到了很多资源并创建了组合解决方案:) 我现在遇到的一个问题是从快照创建 VM 后,机器开始时好像没有正确关闭。出现如下窗口:toastytech.com/guis/srv2k3login2.jpg 因此(可能)我无法从 Azure 运行自定义脚本...
    • 您在 Azure 上使用 Server 2003 吗?您是否在 VM 上获得自定义脚本扩展日志。
    • 如果你想使用自定义脚本扩展,你应该在虚拟机上安装 Azure VM 代理。
    • 如果我的回答有帮助,也许您可​​以接受它作为答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2020-01-03
    • 2019-03-10
    • 2018-01-31
    • 2011-02-24
    相关资源
    最近更新 更多