轻松自动调整azure VM的大小

时间:2016-11-15 23:21:32

标签: azure virtual-machine

我们在azure上有一个VM,这个VM是一个20核,非常昂贵。该vm由公司代理商使用,工作时间为上午8点至下午5点。在此之后,几乎没有用处。

我想尝试通过在下午5:10之后将此vm调整为2核心大小来降低成本,并在早上7:50将其重新调整为20核心。我已经手动做了一个多星期了,现在似乎节省了很多钱。

问题是我手动执行此操作并且我想自动执行此操作。

我一直在读,如果配置了可用性集,就可以完成此操作。但是当创建此vm时,可用性集不是出于某种原因而需要重新创建vm才能拥有一个,但这不是一个选项。

也许我可以使用powershell来做到这一点?我对powershell有一个非常基本的知识,所以我不知道如何做到这一点。

如果我可以创建一个powershell脚本并使用Windows的计划任务在特定时间运行它,那就太棒了。

2 个答案:

答案 0 :(得分:0)

要更改机器尺寸,您可以运行powershell

Get-AzureVM -ServiceName "MySvc1" -Name "MyVM3" | Set-AzureVMSize "Small" | Update-AzureVM

Here is example如何自动化。

但是如果你运行resize命令机器也会重新启动,这也是一个问题,所以如果你使用一台机器,你将不会在一段时间内处理任何事情。

我还建议您关注webjobs with queue。它将为您提供处理的灵活性

  • 你可以根据需求进行自动缩放(所以你会选择基本大小,一旦cpu使用量增加,你可以说再增加一个节点等等,如果cpu减少了azure就会击落节点)
  • 您可以使用队列来推送任务,而webjob会选择它。

答案 1 :(得分:0)

除非您先禁用加速网络,否则无法获得某些 VM 大小。

如果像我一样,您想尽可能启用加速网络,仅在不启用时将其禁用,我将以下 powershell 模块放在一起,用于自动处理 VM 的缩放并在必要时禁用加速网络。

param
(
    [Parameter (Mandatory = $true)]
    [object] $ResourceGroupName,
    
    [Parameter (Mandatory = $true)]
    [object] $VMName,

    [Parameter (Mandatory = $true)]
    [object] $NewVMSize
)


Function Get-TimeStamp {    
    return "[{0:yyyy-MM-dd} {0:HH:mm:ss}]" -f (Get-Date)    
}

Write-Output "$(Get-TimeStamp) Importing Modules"
Import-Module -Name Az.Automation
Import-Module -Name Az.Compute
Import-Module -Name Az.Network
Import-Module -Name Az.Resources

Write-Output "$(Get-TimeStamp) Validating Arguments"
if (-Not $ResourceGroupName) {
    Write-Error "$(Get-TimeStamp) Runbook was missing the expected ""ResourceGroupName"" property. Args were: $args"
    exit 0
}
if (-Not $VMName) {
    Write-Error "$(Get-TimeStamp) Runbook was missing the expected ""VMName"" property. Args were: $args"
    exit 0
}

# Set the current automation connection's authenticated account to use for future Azure Resource Manager cmdlet requests.
Write-Output "$(Get-TimeStamp) Obtaining Service Principle Connection..."
$ServicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection"
Connect-AzAccount -ServicePrincipal -ErrorAction Stop `
    -TenantId $ServicePrincipalConnection.TenantId `
    -ApplicationId $ServicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint | Write-Verbose
Set-AzContext -Subscription $ServicePrincipalConnection.SubscriptionID -ErrorAction Stop | Write-Verbose 
Write-Output "$(Get-TimeStamp) Logged in as AzureRunAsConnection Service Principle"

# Get the VM to be scaled up or down
Write-Output "$(Get-TimeStamp) Retrieving VM Information"
$vm = Get-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName -ErrorAction Stop
# Check if the VM already has the requested configuration
if ($vm.HardwareProfile.VmSize -eq $NewVMSize) {
    Write-Output "$(Get-TimeStamp) Nothing to do! The VM already has the configuration $NewVMSize"
    exit 1
}

# Collect information about the VM's network configuration
Write-Output "$(Get-TimeStamp) Retrieving Network Interface Information..."
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].id -ErrorAction Stop

try {
    # Change the configuration
    Write-Output "$(Get-TimeStamp) Changing VM Configuration..."
    $vm.HardwareProfile.VmSize = $NewVMSize    
    Update-AzVM -VM $vm -ResourceGroupName $ResourceGroupName -ErrorAction Stop
    Write-Output "$(Get-TimeStamp) Successfully updated the VM configuration to $NewVMSize"
} catch {
    Write-Output "$(Get-TimeStamp) Handling error while changing VM Configuration..."
    # Some VM configurations do not support accelerated networking. Try to correct that
    if ($_.Exception.Message.Contains("VMSizeIsNotPermittedToEnableAcceleratedNetworking")) {
        Write-Output "$(Get-TimeStamp) It appears as though the new VM Size was not accepted because it does not support Accelerated Networking."
        Write-Output "$(Get-TimeStamp) Attempting to disable Accelerated Networking on the VM's network interface: $($nic.Name)..."
        $nic.EnableAcceleratedNetworking = $False
        try {
            $nic | Set-AzNetworkInterface -ErrorAction Stop
        } catch {
            Write-Error "$(Get-TimeStamp) Error while disabling accelerated networking: $_"
            throw
        }
        Write-Output "$(Get-TimeStamp) Done. Now trying to change the VM Configuration again..."
        try {
            Update-AzVM -VM $vm -ResourceGroupName $ResourceGroupName -ErrorAction Stop
            Write-Output "$(Get-TimeStamp) Successfully updated the VM configuration to $NewVMSize"
        } catch {
            Write-Error "$(Get-TimeStamp) Unexpected error while updating the VM configuration after disabling accelerated networking: $_"
            throw
        }
    } else {
        Write-Error "$(Get-TimeStamp) Unexpected error while updating the VM configuration. Error code is: $($_.Exception.ErrorCode). Full Details: $_"
        throw
    }
} finally {
    # Try to restore Accelerated Networking regardless of what actions succeeded/failed above
    if (-Not $nic.EnableAcceleratedNetworking) {
        Write-Output "$(Get-TimeStamp) Attempting to restore accellerated networking on the VM..."
        try { 
            $nic.EnableAcceleratedNetworking = $True
            $nic | Set-AzNetworkInterface -ErrorAction Stop
            Write-Output "$(Get-TimeStamp) Restored Accelerated Networking on the network interface: $($nic.Name)"
        } catch {
            # This is expected to fail if we have configured the VM to a size that does not support it.    
            Write-Output "$(Get-TimeStamp) Retrieving VM Information..."        
            $vm = Get-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName -ErrorAction Stop
            Write-Output "$(Get-TimeStamp) Accelerated Networking not supported on the network interface $($nic.Name) with the current VM size $($vm.HardwareProfile.VmSize)"
        }
    }
}

根据您的用例,您可能不需要服务原则的东西,我的目的是将其托管在 Azure 自动化 Runbook 中。

以下是截至发布时可试用的一些 VM 大小:

# SCALE UP: Here are the best-value memory optimized VM configurations as of 2021-01-01
# VM                 vCPUs  RAM (GiB)
# Standard_E2as_v4   2      16
# Standard_E4as_v4   4      32
# Standard_E8as_v4   8      64
# Standard_E16as_v4  16     128
# Standard_E20as_v4  20     160
# Standard_E32as_v4  32     256
# Standard_E48as_v4  48     384
# Standard_E64as_v4  64     512
# Standard_E96as_v4  96     672   <- Note: sub-linear increase in RAM per cost makes this slightly worse value
#
# SCALE DOWN: Here are the lowest-cost VM configurations that meet the minimum-spec for windows 10
# VM        vCPUs   RAM (GiB)   Cost/month
# Standard_B1ms     1       2           US$15.11
# Standard_B2s      2       4           US$30.37
# Standard_F1s      1       2           US$36.28
# Standard_DS1_v2   1       3.5         US$53.29
# Standard_DS1      1       3.5         US$56.21
# Standard_B2ms     2       8           US$60.74
# Standard_F2s_v2   2       4           US$61.76
# Standard_D2as_v4  2       8           US$70.08
# Standard_F2s      2       4           US$72.27
相关问题