另类的曲线方式定时Start up/Shut down VM 的解决方案

一,引言

     最近看到一位小兄弟在为了做 Azure 云虚拟机的自动关机开启 在群里求助,最后也不知道结果咋样了。

至于他提到的利用 Automation Account 我是没有接触过,并且也没有看资料学习,所以不知道怎么使用 Automation Account 做到定时的开关机。但是值得庆幸是的,我会一些 PoerShell 脚本,知道 PowerShell 可以做到利用脚本开关机;并且在前一段时间稍微熟悉了 Azure Function App,知道 Azure Function App 的模板中是有 TimeTrigger。一个可做到开关机,一个可做到定时执行作业。奇怪的组合就这样诞生了。今天我们就额外为这位网友分享一篇另类的解决方案。Let's go

二,正文

1,不可缺少的主角----- Azure virtual machine

登陆到 Azure Portal 上之后,点击 “Create a resource“,选择 ”Compute“,点击创建 ”Virtual machine“

输入创建VM时,必要的参数

Resource group:”Web_Test_VM_RG“

Virtual machine name:‘cnbateblog-vm”

Region:“East Asia”

Availability options:’No infrastructure redundancy required“

Images:”Windows Server 2012 R2 Datacenter - Gen1“

Size:”根据自己的实际情况选择合适的类型“

Username:”cnbateblogwebvm01“

Password 和 Confirm password 就不用我多说了

OS disk type 选择:”Standard HDD“

点击 ”Next:Networking >“

修改Public IP :”Basic“ && "Static"(主要是为了不想虚拟机在重启之后,Public IP 老是在变化)

点击 ”Review + create“

等待验证完成后,点击 ”Create“

等待创建完成后,点击 ”go to resource“ 跳转到 ”cnbateblog-vm“ 这台机器。我们可以看到微软其实已经给提供了 ”Auto-shundown“(定时关机)的功能了,我们只需要简单的操作一番就可以实现了

选择 ”Operation=》Auto-shundown“

Enabled 选择:”On“

Scheduled shutdown :“7:15:00 PM”

Time zone 选择:“UTC+8”

邮件的发送,大家可以自行选择是否需要

输入完相关参数后,记得点击 “Save” 进行保存

做完这些操作后,我们的重头戏来了 Azure Function App

2,Azure Function App 华丽登场

继续回到 Azure Portal 首页,点击 “Create a resource”,创建 Function App

输入相关参数

Resource Group:”Web_Test_Function_RG“

Function App name:”cnbateblogvm-autostartup“

Publish:”Code“

Runtime stack:”PowerShell Code“

Version:”7.0“

Region:”East Asia“

点击 ”Next:Hosting >“ 进入下一步操作

Storage Account 可以选择默认的名称

这里我自己修改了这个 Storage account 为 ”vmautostartstorage“

关于 ”Monitoring“ 也可以根据实际需求设置是否开启

点击 ”Review + create“

检查参数没有问题后,点击 ”Create“ 创建 Azure Function App

创建完后,点击跳转到该资源,进行设置 Function App

首先我们需要设置开启 Function App 的  Identity(标识)

选择 ”Settings=》Identity“,点击 ”Status=》On“,完事记得点击 ”Save“ 保存操作

同时Azure Function App 需要访问控制 Azure VM 

回到刚刚创建好的VM,选择 “Access control(IAM)”,点击 “Add role to this resource” 添加角色指派

输入相关参数

Role:“Owner”

Assign access to:“Function App”

Select:选择我们创建的 Azure Function

点击 “Save” 保存

并且,我们可以在 Access control(IAM)中看到刚刚添加好的角色分配

又回到刚刚创建好的Azure Function App 中,接下来需要创建自动启动虚拟机的代码,

选择 ” Functions =》Function“,点击 ”+Add“

Function template 选择 ”Time trigger“(将按指定计划运行函数),点击 ”Add“

添加完成后,跳转到 Function 的开发这页面,需要我们编写具体的代码

选择 ” Developer =》Intergration“ ,点击 ”计时器(Timer)“

Schedule(计划):”0 0 10 * * *“(每天UTC 时间 上午10点执行,中国时区 也就是下午18点整)为了演示,等下会手动触发

记得时间戳参数名称 ”Time“ 等下会使用到

点击 ”Save“ 保存

接下来点击 ”Code + Test“ 编写 PowerShell 代码

需要注意的是,中间有两行 “Install-Module,Import-Module” 意思是 “安装、导入 PowerShell 库中需要的 PowerShellGet 模块”

如果在测试运行 Azure Function App 出现 “The term 'Get-AzSubscription' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again” 那么就得加上注册、导入 PowerShellGet 模块 的命令,当测试运行成功后,再注释掉就可以了。

# Input bindings are passed in via param block.
param($Timer)

$subscription_Id = "Your Subscription_Id "
$tenant_Id = "Your Tenant Id "
$Resource_Name = "Web_Test_VM_RG"
$VM_Name = "cnbateblog-vm"

#Install-Module
#Import-Module

Select-AzSubscription -SubscriptionID $subscription_Id -TenantID $tenant_Id
Start-AzVM -ResourceGroupName $Resource_Name -Name $VM_Name

保存后,我们可以尝试测试代码

我们可以从 VM 的活动日志中看到当前VM的状态的变化

19:15停止VM

23:20 手动触发 TimeTrigger 启动VM

Bingo!!!!!完成。大家可以自行尝试操作一下🤷‍♂️🤷‍♂️🤷‍♂️🤷‍♂️🤷‍♂️

三,结尾

     文中有用到 “安装、导入 PowerShell 库中需要的 PowerShellGet 模块”,接下来我也会再学习分享的,并且定时开/关 VM 还有其他方式,如文章开始这位小兄弟提到的 Automation Account。期待下一篇分享来自 Automation Account 的分享内容

参考链接:Working with the new Azure PowerShell Az moduleUse Azure Function to Schedule Auto Start for VMs

文章来自博主本人自己的博客:https://allenmasters.com/post/2021/4/29/start-upshut-down-vm-

欢迎大家关注博主的博客:https://allenmasters.com/

作者:Allen 

版权:转载请在文章明显位置注明作者及出处。如发现错误,欢迎批评指正。

posted @ 2021-04-30 07:40  Grant_Allen  阅读(598)  评论(0编辑  收藏  举报