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

一,引言

网购优惠券 m.fenfaw.cn

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

img-5c21d534-2ad3-4d4d-9c18-aafbfc58d3f0.png

img-231ee4d0-1a5b-4572-983c-e87eefc5c739.png

至于他提到的利用 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“

img-8a29a7e3-d93c-4b77-b74c-f067767aa69c.png

输入创建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 就不用我多说了

img-e2f5bc9d-1c6e-44e9-acd0-eef04be0e4e6.png

OS disk type 选择:”Standard HDD“

点击 ”Next:Networking >“

img-8029545d-141b-49eb-a243-2053d4cc4bae.png

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

点击 ”Review + create“

img-64d4405e-8349-488d-880d-9e09626231c6.png

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

img-1abf771d-008b-43f5-8e36-d0f51c7e4877.png

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

选择 ”Operation=》Auto-shundown“

Enabled 选择:”On“

Scheduled shutdown :“7:15:00 PM”

Time zone 选择:“UTC+8”

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

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

img-bf8f4fee-3e7e-460e-91ae-2941d34916f8.png

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

2,Azure Function App 华丽登场

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

img-0ea976e2-e15d-407d-89a4-8a65cecb4e4c.png

输入相关参数

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 >“ 进入下一步操作

img-8f29c43a-dcf6-4a4b-8e65-839459988ca5.png

Storage Account 可以选择默认的名称

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

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

点击 ”Review + create“

img-0a4f8949-a7e5-4c88-b3e9-23dd4d74559c.png

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

img-4fa42862-5da8-4c0c-a077-260a4442f66f.png

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

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

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

img-35d20f60-19b6-4c4b-aaf6-8c2cef854c76.png

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

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

img-dff26757-2f36-4c33-ae4e-a01273900a02.png

输入相关参数

Role:“Owner”

Assign access to:“Function App”

Select:选择我们创建的 Azure Function

点击 “Save” 保存

img-58699958-5e21-42c8-9fee-0a743552312e.png

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

img-615a9568-0e0b-4cef-b87d-90f4e5ed71ce.png

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

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

img-b51b61b5-6852-4f6a-aba1-290d4b600476.png

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

img-0fa8942d-e238-4e51-a8f1-6d6e62e04dcd.png

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

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

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

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

点击 ”Save“ 保存

img-bb4e8b8b-ce97-45af-a474-3e7c2cb28465.png

接下来点击 ”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

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

img-2a15395a-521f-4097-981c-8d848a188be2.png

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

19:15停止VM

img-c4655601-ffd5-499b-b2c1-6c7c252fb004.png

23:20 手动触发 TimeTrigger 启动VM

img-d1facba0-41c6-46ba-b72c-8f4c1f9c4866.png

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

三,结尾

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

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

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

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

作者:Allen 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值