如何在每次 EC2 实例启动时运行 cloud-init 启动脚本? [英] How do I make cloud-init startup scripts run every time my EC2 instance boots?

查看:73
本文介绍了如何在每次 EC2 实例启动时运行 cloud-init 启动脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行基于 Amazon Linux AMI 的 AMI 的 EC2 实例.与所有此类 AMI 一样,它支持 cloud-init 系统,用于根据用户数据运行启动脚本传入每个实例.在这种特殊情况下,我的用户数据输入恰好是一个包含其他几个启动脚本的包含文件:

I have an EC2 instance running an AMI based on the Amazon Linux AMI. Like all such AMIs, it supports the cloud-init system for running startup scripts based on the User Data passed into every instance. In this particular case, my User Data input happens to be an Include file that sources several other startup scripts:

#include
http://s3.amazonaws.com/path/to/script/1
http://s3.amazonaws.com/path/to/script/2

我第一次启动我的实例时,cloud-init 启动脚本运行正常.但是,如果我对实例进行软重启(例如,通过运行 sudo shutdown -r now),实例会重新启动, 第二次运行启动脚本时间.如果我进入系统日志,我可以看到:

The first time I boot my instance, the cloud-init startup script runs correctly. However, if I do a soft reboot of the instance (by running sudo shutdown -r now, for instance), the instance comes back up without running the startup script the second time around. If I go into the system logs, I can see:

Running cloud-init user-scripts
user-scripts already ran once-per-instance
[  OK  ]

这不是我想要的——我可以看到让启动脚本在每个实例生命周期只运行一次的效用,但在我的情况下,这些应该在每次实例启动时运行,就像普通的启动脚本一样.

This is not what I want -- I can see the utility of having startup scripts that only run once per instance lifetime, but in my case these should run every time the instance starts up, like normal startup scripts.

我意识到一个可能的解决方案是在第一次运行后手动让我的脚本将它们自己插入到 rc.local 中.然而,这似乎很麻烦,因为 cloud-init 和 rc.d 环境略有不同,我现在必须在首次启动和所有后续启动时分别调试脚本.

I realize that one possible solution is to manually have my scripts insert themselves into rc.local after running the first time. This seems burdensome, however, since the cloud-init and rc.d environments are subtly different and I would now have to debug scripts on first launch and all subsequent launches separately.

有谁知道我如何告诉 cloud-init 始终运行我的脚本?这听起来确实是 cloud-init 的设计者会考虑的.

Does anyone know how I can tell cloud-init to always run my scripts? This certainly sounds like something the designers of cloud-init would have considered.

推荐答案

在 11.10、12.04 及更高版本中,您可以通过使scripts-user"始终"运行来实现此目的.在/etc/cloud/cloud.cfg 中,您会看到如下内容:

In 11.10, 12.04 and later, you can achieve this by making the 'scripts-user' run 'always'. In /etc/cloud/cloud.cfg you'll see something like:

cloud_final_modules:
 - rightscale_userdata
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - keys-to-console
 - phone-home
 - final-message

这可以在启动后修改,或者可以通过用户数据插入覆盖此节的云配置数据.即,在用户数据中,您可以提供:

This can be modified after boot, or cloud-config data overriding this stanza can be inserted via user-data. Ie, in user-data you can provide:

#cloud-config
cloud_final_modules:
 - rightscale_userdata
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - [scripts-user, always]
 - keys-to-console
 - phone-home
 - final-message

这也可以是#included",正如您在描述中所做的那样.不幸的是,现在,您不能修改cloud_final_modules",而只能覆盖它.我希望在某个时候添加修改配置部分的能力.

That can also be '#included' as you've done in your description. Unfortunately, right now, you cannot modify the 'cloud_final_modules', but only override it. I hope to add the ability to modify config sections at some point.

在云配置文档中有更多关于此的信息https://github.com/canonical/cloud-init/tree/master/doc/examples

There is a bit more information on this in the cloud-config doc at https://github.com/canonical/cloud-init/tree/master/doc/examples

或者,您可以将文件放在/var/lib/cloud/scripts/per-boot 中,它们将通过scripts-per-boot"路径运行.

Alternatively, you can put files in /var/lib/cloud/scripts/per-boot , and they'll be run by the 'scripts-per-boot' path.

这篇关于如何在每次 EC2 实例启动时运行 cloud-init 启动脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆