Auto Start Stopped Spot Instance to Keep Your VM Running 24/7/365
Using Azure Spot Virtual Machines allows you to take advantage of our unused capacity at a significant cost savings. At any point in time when Azure needs the capacity back, the Azure infrastructure will evict Azure Spot Virtual Machines. Therefore, Azure Spot Virtual Machines are great for workloads that can handle interruptions like batch processing jobs, dev/test environments, large compute workloads, and more.
In this post I am gonna show you how to utilize unused Azure compute capacity at deep discounts—up to 90 percent compared to pay-as-you-go prices. If the machine has been evicted based on capacity, we will be able to start it in 5 minutes using automation account's runbook.
Limitation
VMs can be evicted based on capacity or the max price you set. When creating an Azure Spot Virtual Machine, you can set the eviction policy to Deallocate (default) or Delete.
The following VM sizes aren't supported for Azure Spot Virtual Machines:
- B-series
- Promo versions of any size (like Dv2, NV, NC, H promo sizes)
Azure Spot Virtual Machines can be deployed to any region, except Microsoft Azure operated by 21Vianet.
The biggest challenge is how to start the Spot Instance automatically. We will use alert rule to monitor system and use runbook to trigger a PowerShell script to start stopped VM.
Create VM with A Spot Discount Check
1 Enable Spot Discount
Make sure there is no infrastructure redundancy selected.
Make eviction policy is stop/deallocate.
Create An Automation Account and Add A Runbook
2 Search and Create An Automation Account
3 Import a runbook
param (
[Parameter(Mandatory=$true)]
[String] $Action,
[Parameter(Mandatory=$true)]
[String] $TagName,
[Parameter(Mandatory=$true)]
[String] $TagValue
)
Write-Output ""
Write-Output "------------------------ Authentication ------------------------"
Write-Output "Logging into Azure ..."
try {
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
}
$SubscriptionId = "<YOUR SUBSCRIPTION ID>"
Select-AzContext -name ((Get-AzContext -ListAvailable).Name -match $SubscriptionId)[0]
try {
if ($Action -eq "Start" -or $Action -eq "Stop") {
Write-Output "Action Selected"
}
else {
Write-Output "Wrong Action Selection. It should be Start or Stop"
Exit
}
} catch {
Write-Error -Message $_.Exception
}
try {
Write-Output ""
Write-Output ""
Write-Output "---------------------------- Status ----------------------------"
Write-Output "Getting all virtual machines with Tags ..."
$myvms = Get-AzVM | Where-Object { $_.Tags[$TagName] -eq $TagValue }
Write-Output "Total of VMs: $($myvms.Length)"
foreach ($vm in $myvms) {
Write-Output $vm.Name
$status = Get-AzVM -Name $vm.Name -Status
Write-Output "VM $($vm.Name) status: $($status.PowerState)"
if ($Action -eq "Start") {
if ($status.PowerState -eq "VM deallocated") {
Write-Output "Starting VM: $($vm.Name)"
try {
Start-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -NoWait
} catch {
Write-Error -Message $_.Exception
}
} else {
Write-Output "VM: $($vm.Name) is already started"
}
}
if ($Action -eq "Stop") {
if ($status.PowerState -eq "VM running") {
Write-Output "Stopping VM: $($vm.Name)"
try {
Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force -NoWait
} catch {
Write-Error -Message $_.Exception
}
} else {
Write-Output "VM: $($vm.Name) is already stopped"
}
}
}
}
catch {
Write-Error -Message $_.Exception
}
4 add tag to Spot VM
Create an Alert Rule
Go to Monitor - Alert page
Create
Create an alert rule condition, if threshold for VM Aaailabiity Metric is less than 1 (means stopped), the alert will be triggered. The alert will check the condition every minute.
Next step is to create action group. It has to be in same subscription. Action group has to be in same subscription as your monitoring resources.
You even can set up SMS notification to notify you the server is stopped.
Notitication setting does not matter. SMS is not free anyway. One SMS is about one dime.
Videos
References
- https://hostloc.com/thread-1182352-1-5.html
共有 0 条评论