azure 转换/迁移现货示例虚拟机,以实现按需付费虚拟机

3zwtqj6y  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(84)

我需要转换/迁移现货示例VM,以按需付费VM。示例已停止,由于可用性原因,无法启动示例。
如何做到这一点?最好有剧本。

5jdjgkvh

5jdjgkvh1#

AFAIK,我不认为你可以将Spot VM转换为常规VM。您必须删除该虚拟机并将磁盘连接到新的即用即付虚拟机。
由于Azure Spot VM没有SLA,并且可以随时将其逐出,因此如果您需要立即使用容量,建议您使用标准VM而不是Azure Spot虚拟机。

fhg3lkii

fhg3lkii2#

我已经创建了PS脚本来将PYUG转换为SPOT VM。它不适用于具有扩展的VM,因为我们将从现有磁盘重新创建。验证转换成功后,删除备份快照。

Function Convert-PYUG2SPOT {
    Param (
        # Set variables to your specifics
        $resourceGroup,
        $vmName,
        $subscriptionName,
        [switch]$force
    )
    $WarningPreference = 'SilentlyContinue'

    $context = Set-AzContext $subscriptionName
    Write-Output "Setting context to $($subscriptionName)"
    # Get the details of the VM to be moved to the Availability Set
    $originalVM = Get-AzVM `
        -ResourceGroupName $resourceGroup `
        -Name $vmName

    try {
        # Check whether already logged-in
        $setAzContext = (az account set --name $subscriptionName)
        $updateresource = (az resource update --resource-group $resourceGroup --name $vmName --resource-type virtualMachines
--namespace Microsoft.Compute --set properties.storageProfile.osDisk.deleteOption=detach)
    }
    catch {
        Write-Host 'You need to login'
        if ($LASTEXITCODE -ne 0) { exit 1 }
    }
    # checking for dependencies
    $vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName
    $extensionExist = ($vm.Extensions | Select-Object Publisher, VirtualMachineExtensionType, TypeHandlerVersion)
    if ($extensionExist) {
        $extensionExist
        Write-Host 'Make sure you note the installed extensions, that you can configure later on the converted VM.'
        Write-Host 'Please re-run the automation with the -force switch to initiate the conversion.'
    }
    if ($extensionExist -and $force) {
        Write-Host 'Initiating the conversion.'
        # Create the basic configuration for the replacement VM.
        $newVM = New-AzVMConfig `
            -VMName $originalVM.Name `
            -VMSize $originalVM.HardwareProfile.VmSize `
            -Priority 'Spot' -MaxPrice -1

        # Confgure OS Disk
        Set-AzVMOSDisk `
            -VM $newVM -CreateOption Attach `
            -ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id `
            -Name $originalVM.StorageProfile.OsDisk.Name

        if ($originalVM.OSProfile.WindowsConfiguration) {
            $newVM.StorageProfile.OsDisk.OsType = 'Windows'
        }
        else {
            $newVM.StorageProfile.OsDisk.OsType = 'Linux'
        }

        # Taking backup of OSdisks
        $osDiskid = $originalVM.StorageProfile.OsDisk.ManagedDisk.Id
        $osDiskName = $originalVM.StorageProfile.OsDisk.Name
        # Create Disk Snapshot
        $snapshot = New-AzSnapshotConfig -SourceUri $osDiskid -Location $originalVM.Location -CreateOption copy
        $newsnap = New-AzSnapshot `
            -Snapshot $snapshot `
            -SnapshotName "$($osDiskName)_snapshot" `
            -ResourceGroupName $resourceGroup

        $randomNumber = Get-Random -Maximum 100
        foreach ($disk in $originalVM.StorageProfile.DataDisks) {
            # Taking backup of disks
            # Get Current VM Data Disk metadata.
            $dataDiskid = $disk.ManagedDisk.Id
            $dataDiskName = ($disk.Name).ToLower()

            # Create Disk Snapshot
            $snapshot = New-AzSnapshotConfig -SourceUri $dataDiskid -Location $originalVM.Location -CreateOption copy
            $newsnap = New-AzSnapshot `
                -Snapshot $snapshot `
                -SnapshotName "$($dataDiskName)_$($randomNumber)" `
                -ResourceGroupName $resourceGroup
        }

        # Add NIC(s) and keep the same NIC as primary
        foreach ($nic in $originalVM.NetworkProfile.NetworkInterfaces) {
            if ($nic.Primary -eq 'True') {
                Add-AzVMNetworkInterface `
                    -VM $newVM `
                    -Id $nic.Id -Primary
            }
            else {
                Add-AzVMNetworkInterface `
                    -VM $newVM `
                    -Id $nic.Id
            }
        }

        if ($originalVM.AvailabilitySetReference.Id) {
            Write-Output "Warning: VM $originalVM.Name is in an availability set. Spot VMs cannot run in availability sets."
        }

        # Remove the original VM
        Remove-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force

        New-AzVM `
            -ResourceGroupName $resourceGroup `
            -Location $originalVM.Location `
            -VM $newVM `
            -DisableBginfoExtension

        foreach ($attachDisk in $originalVM.StorageProfile.DataDisks) {
            $dataDiskid = ($attachDisk.ManagedDisk.Id).ToLower()
            $dataDiskName = ($attachDisk.Name).ToLower()
            Write-Output "Attaching $($dataDiskName)"
            Write-Output $dataDiskid
            Write-Output $dataDiskName
            $vm = Get-AzVM -Name $vmName -ResourceGroupName $resourceGroup
            $vm = Add-AzVMDataDisk -VM $vm `
                -Name $dataDiskName `
                -ManagedDiskId $dataDiskid `
                -Lun $attachDisk.Lun `
                -CreateOption Attach
            Write-Output " --- Adding data disk $($dataDiskName) to $($vmName)"
            Update-AzVM -ResourceGroupName $resourceGroup -VM $vm
        }
    } }

字符串

相关问题