简体   繁体   English

如何仅在 AWS AutoScaling 中的单个实例上运行 cron 作业?

[英]How to run cron job only on single instance in AWS AutoScaling?

I have scheduled 2 cronjobs for my application.我为我的应用程序安排了 2 个 cronjobs。

My Application server is in an autoscaling group and I kept a minimum of 2 instances because of High availability.我的应用程序服务器在一个自动缩放组中,由于高可用性,我至少保留了 2 个实例。 Everything working is fine but cron job is running multiple times because of 2 instances in autoscaling.一切正常,但由于自动缩放中有 2 个实例,cron 作业正在运行多次。

I could not limit the instance size to 1 because already my application in the production environment I prefer to have HA.我不能将实例大小限制为 1,因为我的应用程序已经在生产环境中,我更喜欢 HA。

How should I have to limit execute cron job on a single instance?我应该如何限制在单个实例上执行 cron 作业? or should i have to use other services like AWS Lamda or AWS ELasticBeanstalk还是我必须使用其他服务,例如 AWS Lamda 或 AWS ElasticBeanstalk

Firstly you should consider whether running the crons on these instances is suitable.首先,您应该考虑在这些实例上运行 crons 是否合适。 If you're trying to keep this highly available and it is directly interacted via customers what will the impact of the crons performance be?如果你试图保持这个高度可用并且它是通过客户直接交互的,那么 crons 性能的影响是什么?

Perhaps consider using a separate autoscaling group or instance with a total of 1 instances to run these crons?也许考虑使用一个单独的自动缩放组或实例,总共有 1 个实例来运行这些 crons? You could launch the instance or update the autoscaling group just before the cron needs to run and then automate the shutdown after it has completed.您可以在 cron 需要运行之前启动实例或更新自动缩放组,然后在完成后自动关闭。

Otherwise you would need to consider using a locking mechanism for your script.否则,您将需要考虑为您的脚本使用锁定机制。 By using this your script write a lock to confirm that it is in process, at the beginning of the script run it would check whether there was any script lock in progress.通过使用这个你的脚本写一个锁来确认它正在处理中,在脚本运行的开始它会检查是否有任何脚本锁在进行中。 To further prevent the chance of a collision between multiple servers consider adding jitter (random seconds of sleep) to the start of your script.为了进一步防止多个服务器之间发生冲突的可能性,请考虑在脚本的开头添加抖动(随机睡眠秒数)。

Suitable technologies for writing a lock are below:适合编写锁的技术如下:

you can enable termination protection on of the instance.您可以对实例启用终止保护。 Attach necessary role & permission for system manager.为系统管理员附加必要的角色和权限。 once the instance is available under managed instance under system manager you can create a schedule event in cloudwatch to run ssm documents.一旦实例在系统管理器下的托管实例下可用,您就可以在 cloudwatch 中创建计划事件来运行 ssm 文档。 if you are running a bash script convert that to ssm document and set this doc as targate.如果您正在运行 bash 脚本,请将其转换为 ssm 文档并将此文档设置为 targate。 or you can use shellscript document for running commands或者您可以使用 shellscript 文档来运行命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM