通過(guò)使用 Powershell 將專(zhuān)用托管磁盤(pán)附加為 OS 磁盤(pán)來(lái)創(chuàng)建新 VM。 專(zhuān)用磁盤(pán)是保留原始 VM 中的用戶(hù)帳戶(hù)、應(yīng)用程序和其他狀態(tài)數(shù)據(jù)的現(xiàn)有 VM 中虛擬硬盤(pán) (VHD) 的副本。
使用專(zhuān)用 VHD 創(chuàng)建新 VM 時(shí),新 VM 將保留原始 VM 的計(jì)算機(jī)名。 還會(huì)保留其他計(jì)算機(jī)特定信息,在某些情況下,這種重復(fù)信息可能會(huì)導(dǎo)致問(wèn)題。 請(qǐng)注意,在復(fù)制 VM 時(shí),應(yīng)用程序依賴(lài)哪些類(lèi)型的計(jì)算機(jī)特定信息。
可以使用兩個(gè)選項(xiàng):
本主題演示如何使用托管磁盤(pán)。 如果有需要使用存儲(chǔ)帳戶(hù)的舊版部署,請(qǐng)參閱從存儲(chǔ)帳戶(hù)中的專(zhuān)用 VHD 創(chuàng)建 VM
開(kāi)始之前
如果使用 PowerShell,請(qǐng)確保使用的是最新版本的 AzureRM.Compute PowerShell 模塊。
PowerShell復(fù)制
Install-ModuleAzureRM.Compute-RequiredVersion2.6.0
有關(guān)詳細(xì)信息,請(qǐng)參閱Azure PowerShell 版本控制。
選項(xiàng) 1:上傳專(zhuān)用 VHD
可從使用本地虛擬化工具(如 Hyper-V)創(chuàng)建的專(zhuān)用 VM 或從另一個(gè)云導(dǎo)出的 VM 上傳 VHD。
準(zhǔn)備 VM
如果想要使用當(dāng)前 VHD 創(chuàng)建新 VM,請(qǐng)確保完成以下步驟。
準(zhǔn)備好要上傳到 Azure 的 Windows VHD。不要使用 Sysprep 通用化 VM。
刪除 VM 上安裝的所有來(lái)賓虛擬化工具和代理(例如 VMware 工具)。
確保 VM 配置為通過(guò) DHCP 來(lái)提取其 IP 地址和 DNS 設(shè)置。 這確保服務(wù)器在啟動(dòng)時(shí)在 VNet 中獲取 IP 地址。
獲取存儲(chǔ)帳戶(hù)
需要 Azure 中的存儲(chǔ)帳戶(hù)來(lái)存儲(chǔ)上傳的 VHD。 可以使用現(xiàn)有存儲(chǔ)帳戶(hù),也可以創(chuàng)建新存儲(chǔ)帳戶(hù)。
顯示可用的存儲(chǔ)帳戶(hù),請(qǐng)鍵入:
PowerShell復(fù)制
Get-AzureRmStorageAccount
如果要使用現(xiàn)有存儲(chǔ)帳戶(hù),請(qǐng)轉(zhuǎn)到上傳 VHD部分。
若要?jiǎng)?chuàng)建存儲(chǔ)帳戶(hù),請(qǐng)執(zhí)行以下步驟:
需要應(yīng)在其中創(chuàng)建存儲(chǔ)帳戶(hù)的資源組的名稱(chēng)。 若要查找訂閱中的所有資源組,請(qǐng)鍵入:
PowerShell復(fù)制
Get-AzureRmResourceGroup
若要在中國(guó)北部區(qū)域中創(chuàng)建名為myResourceGroup的資源組,請(qǐng)鍵入:
PowerShell復(fù)制
New-AzureRmResourceGroup-NamemyResourceGroup-Location"China North"
使用New-AzureRmStorageAccountcmdlet 在此資源組中創(chuàng)建名為mystorageaccount的存儲(chǔ)帳戶(hù):
PowerShell復(fù)制
New-AzureRmStorageAccount-ResourceGroupNamemyResourceGroup-Namemystorageaccount-Location"China North"`-SkuName"Standard_LRS"-Kind"Storage"
將 VHD 上傳到存儲(chǔ)帳戶(hù)
使用Add-AzureRmVhdcmdlet 將 VHD 上傳到存儲(chǔ)帳戶(hù)中的容器。 本示例將文件myVHD.vhd從"C:\Users\Public\Documents\Virtual hard disks\"上傳到myResourceGroup資源組中名為mystorageaccount的存儲(chǔ)帳戶(hù)。 該文件存儲(chǔ)在名為mycontainer的容器中,新文件名為myUploadedVHD.vhd。
PowerShell復(fù)制
$resourceGroupName="myResourceGroup"$urlOfUploadedVhd="https://mystorageaccount.blob.core.chinacloudapi.cn/mycontainer/myUploadedVHD.vhd"Add-AzureRmVhd-ResourceGroupName$resourceGroupName-Destination$urlOfUploadedVhd`-LocalFilePath"C:\Users\Public\Documents\Virtual hard disks\myVHD.vhd"
如果成功,會(huì)顯示類(lèi)似于下面的響應(yīng):
PowerShell復(fù)制
MD5 hash is being calculatedforthe file C:\Users\Public\Documents\Virtual hard disks\myVHD.vhd.MD5 hash calculation is completed.Elapsed timeforthe operation:00:03:35Creating new page blob of size53687091712...Elapsed timeforupload:01:12:49LocalFilePath? ? ? ? ? DestinationUri-------------? ? ? ? ? --------------C:\Users\Public\Doc...? https://mystorageaccount.blob.core.chinacloudapi.cn/mycontainer/myUploadedVHD.vhd
完成執(zhí)行此命令可能需要一段時(shí)間,具體取決于網(wǎng)絡(luò)連接速度和 VHD 文件的大小
從 VHD 創(chuàng)建托管磁盤(pán)
使用New-AzureRMDisk通過(guò)存儲(chǔ)帳戶(hù)中的專(zhuān)用 VHD 創(chuàng)建托管磁盤(pán)。 此示例使用myOSDisk1作為磁盤(pán)名稱(chēng),將磁盤(pán)置于StandardLRS存儲(chǔ)中并使用https://storageaccount.blob.core.chinacloudapi.cn/vhdcontainer/osdisk.vhd作為源 VHD 的 URI。
創(chuàng)建適用于新 VM 的新資源組。
PowerShell復(fù)制
$destinationResourceGroup='myDestinationResourceGroup'New-AzureRmResourceGroup-Location$location-Name$destinationResourceGroup
從上傳的 VHD 創(chuàng)建新 OS 磁盤(pán)。
PowerShell復(fù)制
$sourceUri= (https://storageaccount.blob.core.chinacloudapi.cn/vhdcontainer/osdisk.vhd)$osDiskName='myOsDisk'$osDisk=New-AzureRmDisk-DiskName$osDiskName-Disk`? ? (New-AzureRmDiskConfig-AccountTypeStandardLRS-Location$location-CreateOptionImport `-SourceUri$sourceUri) `-ResourceGroupName$destinationResourceGroup
選項(xiàng) 2:復(fù)制現(xiàn)有 Azure VM
通過(guò)拍攝 VM 快照來(lái)創(chuàng)建使用托管磁盤(pán)的 VM 副本,并使用該快照創(chuàng)建一個(gè)新的托管磁盤(pán)和一個(gè)新 VM。
拍攝 OS 磁盤(pán)快照
可拍攝整個(gè) VM(包括所有磁盤(pán))快照或僅拍攝單個(gè)磁盤(pán)快照。 以下步驟展示了如何使用New-AzureRmSnapshotcmdlet 拍攝僅 VM 的 OS 磁盤(pán)的快照。
設(shè)置一些參數(shù)。
PowerShell復(fù)制
$resourceGroupName='myResourceGroup'$vmName='myVM'$location='chinanorth'$snapshotName='mySnapshot'
獲取 VM 對(duì)象。
PowerShell復(fù)制
$vm=Get-AzureRmVM-Name$vmName-ResourceGroupName$resourceGroupName
獲取 OS 磁盤(pán)名稱(chēng)。
PowerShell復(fù)制
$disk=Get-AzureRmDisk-ResourceGroupName$resourceGroupName-DiskName$vm.StorageProfile.OsDisk.Name
創(chuàng)建快照配置。
PowerShell復(fù)制
$snapshotConfig=New-AzureRmSnapshotConfig-SourceUri$disk.Id-OsTypeWindows-CreateOptionCopy-Location$location
創(chuàng)建快照。
PowerShell復(fù)制
$snapShot=New-AzureRmSnapshot-Snapshot$snapshotConfig-SnapshotName$snapshotName-ResourceGroupName$resourceGroupName
如果計(jì)劃使用快照創(chuàng)建需要高性能的 VM,請(qǐng)結(jié)合使用-AccountType Premium_LRS參數(shù)和 New-AzureRmSnapshot 命令。 該參數(shù)創(chuàng)建快照,使其作為高級(jí)托管磁盤(pán)進(jìn)行存儲(chǔ)。 高級(jí)托管磁盤(pán)比標(biāo)準(zhǔn)托管磁盤(pán)開(kāi)銷(xiāo)大。 因此使用該參數(shù)前,請(qǐng)確保確實(shí)需要高級(jí)托管磁盤(pán)。
從快照創(chuàng)建新磁盤(pán)
使用New-AzureRMDisk從快照創(chuàng)建托管磁盤(pán)。 此示例使用“myOSDisk”作為磁盤(pán)名稱(chēng)。
創(chuàng)建適用于新 VM 的新資源組。
PowerShell復(fù)制
$destinationResourceGroup='myDestinationResourceGroup'New-AzureRmResourceGroup-Location$location-Name$destinationResourceGroup
設(shè)置 OS 磁盤(pán)名稱(chēng)。
PowerShell復(fù)制
$osDiskName='myOsDisk'
創(chuàng)建托管磁盤(pán)。
PowerShell復(fù)制
$osDisk=New-AzureRmDisk-DiskName$osDiskName-Disk`? ? (New-AzureRmDiskConfig-Location$location-CreateOptionCopy `-SourceResourceId$snapshot.Id) `-ResourceGroupName$destinationResourceGroup
創(chuàng)建新 VM
創(chuàng)建新 VM 使用的網(wǎng)絡(luò)和其他 VM 資源。
創(chuàng)建子網(wǎng)和 vNet
創(chuàng)建虛擬網(wǎng)絡(luò)的 vNet 和子網(wǎng)。
創(chuàng)建子網(wǎng)。 本示例在資源組“myDestinationResourceGroup”中創(chuàng)建名為“mySubNet”的子網(wǎng),并將子網(wǎng)地址前綴設(shè)置為 10.0.0.0/24。
PowerShell復(fù)制
$subnetName='mySubNet'$singleSubnet=New-AzureRmVirtualNetworkSubnetConfig-Name$subnetName-AddressPrefix10.0.0.0/24
創(chuàng)建 vNet。 本示例將虛擬網(wǎng)絡(luò)名稱(chēng)設(shè)置為myVnetName,將位置設(shè)置為“中國(guó)北部”,將虛擬網(wǎng)絡(luò)的地址前綴設(shè)置為10.0.0.0/16。
PowerShell復(fù)制
$vnetName="myVnetName"$vnet=New-AzureRmVirtualNetwork-Name$vnetName-ResourceGroupName$destinationResourceGroup-Location$location`-AddressPrefix10.0.0.0/16-Subnet$singleSubnet
創(chuàng)建網(wǎng)絡(luò)安全組和 RDP 規(guī)則
若要使用 RDP 登錄到 VM,需要?jiǎng)?chuàng)建一個(gè)允許在端口 3389 上進(jìn)行 RDP 訪(fǎng)問(wèn)的安全規(guī)則。 由于新 VM 的 VHD 是從現(xiàn)有專(zhuān)用 VM 創(chuàng)建的,因此可以將源虛擬機(jī)中的帳戶(hù)用于 RDP。
本示例將 NSG 名稱(chēng)設(shè)置為myNsg,將 RDP 規(guī)則名稱(chēng)設(shè)置為myRdpRule。
PowerShell復(fù)制
$nsgName="myNsg"$rdpRule=New-AzureRmNetworkSecurityRuleConfig-NamemyRdpRule-Description"Allow RDP"`-AccessAllow-ProtocolTcp-DirectionInbound-Priority110`-SourceAddressPrefixInternet-SourcePortRange* `-DestinationAddressPrefix*-DestinationPortRange3389$nsg=New-AzureRmNetworkSecurityGroup-ResourceGroupName$destinationResourceGroup-Location$location`-Name$nsgName-SecurityRules$rdpRule
有關(guān)終結(jié)點(diǎn)和 NSG 規(guī)則的詳細(xì)信息,請(qǐng)參閱Opening ports to a VM in Azure using PowerShell(使用 PowerShell 在 Azure 中打開(kāi) VM 端口)。
創(chuàng)建公共 IP 地址和 NIC
若要與虛擬網(wǎng)絡(luò)中的虛擬機(jī)通信,需要一個(gè)公共 IP 地址和網(wǎng)絡(luò)接口。
創(chuàng)建公共 IP。 在此示例中,公共 IP 地址名稱(chēng)設(shè)置為myIP。
PowerShell復(fù)制
$ipName="myIP"$pip=New-AzureRmPublicIpAddress-Name$ipName-ResourceGroupName$destinationResourceGroup-Location$location`-AllocationMethodDynamic
創(chuàng)建 NIC。 在此示例中,NIC 名稱(chēng)設(shè)置為myNicName。
PowerShell復(fù)制
$nicName="myNicName"$nic=New-AzureRmNetworkInterface-Name$nicName-ResourceGroupName$destinationResourceGroup`-Location$location-SubnetId$vnet.Subnets[0].Id-PublicIpAddressId$pip.Id-NetworkSecurityGroupId$nsg.Id
設(shè)置 VM 名稱(chēng)和大小
此示例將 VM 名稱(chēng)設(shè)置為“myVM”,將 VM 大小設(shè)置為“Standard_A2”。
PowerShell復(fù)制
$vmName="myVM"$vmConfig=New-AzureRmVMConfig-VMName$vmName-VMSize"Standard_A2"
添加 NIC
PowerShell復(fù)制
$vm=Add-AzureRmVMNetworkInterface-VM$vmConfig-Id$nic.Id
添加 OS 磁盤(pán)
使用Set-AzureRmVMOSDisk將 OS 磁盤(pán)添加到配置。 此示例將磁盤(pán)大小設(shè)置為128 GB并附加托管磁盤(pán)作為WindowsOS 磁盤(pán)。
PowerShell復(fù)制
$vm=Set-AzureRmVMOSDisk-VM$vm-ManagedDiskId$osDisk.Id-StorageAccountTypeStandardLRS `-DiskSizeInGB128-CreateOptionAttach-Windows
完成該 VM
使用剛剛創(chuàng)建的New-AzureRMVM配置創(chuàng)建 VM。
PowerShell復(fù)制
New-AzureRmVM-ResourceGroupName$destinationResourceGroup-Location$location-VM$vm
如果此命令成功,會(huì)看到類(lèi)似于下面的輸出:
PowerShell復(fù)制
RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
True? ? ? ? OK OK
驗(yàn)證是否已創(chuàng)建 VM
應(yīng)會(huì)在Azure 門(mén)戶(hù)的“瀏覽” > “虛擬機(jī)”下看到新建的 VM,也可以使用以下 PowerShell 命令查看該 VM:
PowerShell復(fù)制
$vmList=Get-AzureRmVM-ResourceGroupName$destinationResourceGroup$vmList.Name
后續(xù)步驟
若要登錄到新虛擬機(jī),請(qǐng)?jiān)?a target="_blank" rel="nofollow">門(mén)戶(hù)中瀏覽到該 VM,單擊“連接”,然后打開(kāi)遠(yuǎn)程桌面 RDP 文件。 使用原始虛擬機(jī)的帳戶(hù)憑據(jù)登錄到新虛擬機(jī)。 有關(guān)詳細(xì)信息,請(qǐng)參閱How to connect and log on to an Azure virtual machine running Windows(如何連接并登錄到運(yùn)行 Windows 的 Azure 虛擬機(jī))。立即訪(fǎng)問(wèn)http://market.azure.cn