gpt4 book ai didi

timer - 如果在另一个计费小时之前空闲,则关闭 EC2 实例

转载 作者:行者123 更新时间:2023-12-01 01:09:57 25 4
gpt4 key购买 nike

在不可预测的时间(用户请求),我需要运行内存密集型作业。为此,我获得了一个现货或按需实例,并用标签将其标记为 non_idle .工作完成后(可能需要几个小时),我给它打上标签 idle .由于 AWS 的按小时计费模式,我希望该实例保持事件状态,直到另一个计费小时发生,以防万一另一个工作进来。如果有一个工作进来,则应重用该实例并将其标记为 non_idle .如果在此期间没有工作进入,则实例应终止。

AWS 是否为此提供了现成的解决方案?据我所知,CloudWatch 无法设置应在特定时间运行的警报,更不用说使用 CPUUtilization 或实例的标签了。否则,也许我可以简单地为每个创建的实例设置一个 java 计时器或在创建实例后每小时运行一次的 Scala Actor 并检查标签 idle .

最佳答案

对于这种细粒度优化,没有现成的 AWS 解决方案,但您可以使用现有的构建块根据当前实例的启动时间来构建您自己的构建块(请参阅 Dmitriy Samovskiy 的智能解决方案,用于推导 How Long Ago Was This EC2 Instance Started?)。

播放“鸡”

Shlomo Swidler 在他的文章 Play “Chicken” with Spot Instances 中探讨了这种优化。 ,尽管在 Amazon EC2 Spot Instances 的背景下动机略有不同:

AWS Spot Instances have an interesting economic characteristic that make it possible to game the system a little. Like all EC2 instances, when you initiate termination of a Spot Instance then you incur a charge for the entire hour, even if you’ve used less than a full hour. But, when AWS terminates the instance due to the spot price exceeding the bid price, you do not pay for the current hour.



机制当然是一样的,所以你可以简单地重用他组装的脚本,即执行这个脚本来代替或除了将实例标记为 idle :

#! /bin/bash
t=/tmp/ec2.running.seconds.$$
if wget -q -O $t http://169.254.169.254/latest/meta-data/local-ipv4 ; then
# add 60 seconds artificially as a safety margin
let runningSecs=$(( `date +%s` - `date -r $t +%s` ))+60
rm -f $t
let runningSecsThisHour=$runningSecs%3600
let runningMinsThisHour=$runningSecsThisHour/60
let leftMins=60-$runningMinsThisHour
# start shutdown one minute earlier than actually required
let shutdownDelayMins=$leftMins-1
if [[ $shutdownDelayMins > 1 && $shutdownDelayMins < 60 ]]; then
echo "Shutting down in $shutdownDelayMins mins."
# TODO: Notify off-instance listener that the game of chicken has begun
sudo shutdown -h +$shutdownDelayMins
else
echo "Shutting down now."
sudo shutdown -h now
fi
exit 0
fi
echo "Failed to determine remaining minutes in this billable hour. Terminating now."
sudo shutdown -h now
exit 1

一旦有作业进入,您就可以取消计划终止,而不是使用 non_idle 标记实例或除此之外如下:

sudo shutdown -c

这也是测试/操作期间的“红色按钮”紧急命令,参见例如Shlomo 的警告:

Make sure you really understand what this script does before you use it. If you mistakenly schedule an instance to be shut down you can cancel it with this command, run on the instance: sudo shutdown -c



将 CloudWatch 添加到游戏中

通过与 Amazon CloudWatch 集成,您可以进一步采用 Shlomo 的自包含方法。 ,最近为 Use Amazon CloudWatch to Detect and Shut Down Unused Amazon EC2 Instances 添加了一个选项,请参阅介绍性博文 Amazon CloudWatch - Alarm Actions详情:

Today we are giving you the ability to stop or terminate your EC2 instances when a CloudWatch alarm is triggered. You can use this as a failsafe (detect an abnormal condition and then act) or as part of your application's processing logic (await an expected condition and then act). [emphasis mine]



您的用例在应用程序集成部分中具体列出:

You can also create CloudWatch alarms based on Custom Metrics that you observe on an instance-by-instance basis. You could, for example, measure calls to your own web service APIs, page requests, or message postings per minute, and respond as desired.



所以你可以通过 Publishing Custom Metrics 来利用这个新功能到 CloudWatch 以根据 Dmitriy 的启动时间检测指示实例是否应终止(为 idle),并在作业进入且实例应继续运行时再次重置指标(为 non_idle) - 就像 EC2 将采取在终止的情况下,3 个自动化步骤中的 2 个将从实例转移到操作环境中,自动化过程的管理和可见性也相应改进。

关于timer - 如果在另一个计费小时之前空闲,则关闭 EC2 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15899544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com