AWS でインスタンスのユーザーデータを定義すると、あらゆる種類のブートストラップ タイプのアクションを実行するのに非常に役立ちます。残念ながら、PCI の理由により、提供された AMI のいずれにも由来しないカスタム CentOS AMI を使用する必要があるため、cloud-init はまだインストールおよび構成されていません。ホスト名を設定して小さなbashスクリプトを実行したいだけです。どうすれば機能しますか?
4 に答える
cloud-init は非常に強力ですが、文書化されていないツールです。インストールした後でも、AMI で既に定義したものを上書きする多くのモジュールがデフォルトでアクティブになっています。最初から最小限の設定を行う手順は次のとおりです。
指示
標準リポジトリから cloud-init をインストールします。PCI が心配なら、AWS のカスタム リポジトリは使いたくないでしょう。
# rpm -Uvh https://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm # yum install cloud-init
/etc/cloud/cloud.cfg
必要な構成を反映するように yaml ファイルを編集します。以下は、各モジュールのドキュメントを含む最小構成です。#If this is not explicitly false, cloud-init will change things so that root #login via ssh is disabled. If you don't want it to do anything, set it false. disable_root: false #Set this if you want cloud-init to manage hostname. The current #/etc/hosts file will be replaced with the one in /etc/cloud/templates. manage_etc_hosts: true #Since cloud-init runs at multiple stages of boot, this needs to be set so #it can log in all of them to /var/log/cloud-init. syslog_fix_perms: null #This is the bit that makes userdata work. You need this to have userdata #scripts be run by cloud-init. datasource_list: [Ec2] datasource: Ec2: metadata_urls: ['http://169.254.169.254'] #modules that run early in boot cloud_init_modules: - bootcmd #for running commands in pre-boot. Commands can be defined in cloud-config userdata. - set-hostname #These 3 make hostname setting work - update-hostname - update-etc-hosts #modules that run after boot cloud_config_modules: - runcmd #like bootcmd, but runs after boot. Use this instead of bootcmd unless you have a good reason for doing so. #modules that run at some point after config is finished cloud_final_modules: - scripts-per-once #all of these run scripts at specific events. Like bootcmd, can be defined in cloud-config. - scripts-per-boot - scripts-per-instance - scripts-user - phone-home #if defined, can make a post request to a specified url when done booting - final-message #if defined, can write a specified message to the log - power-state-change #can trigger stuff based on power state changes system_info: #works because amazon's linux AMI is based on CentOS distro: amazon
に がある場合は
defaults.cfg
、/etc/cloud/cloud.cfg.d/
削除します。この構成を利用するには、新しいインスタンスに次のユーザーデータを定義します。
#cloud-config hostname: myhostname fqdn: myhostname.mydomain.com runcmd: - echo "I did this thing post-boot" - echo "I did this too"
本体に bash スクリプトを置き換え
#cloud-config
て配置することで、単純に bash スクリプトを実行することもでき#!/bin/bash
ますが、その場合は、ホスト名関連のすべてのモジュールを から削除する必要がありますcloud_init_modules
。
その他の注意事項
これは最小限の構成であり、cloud-init はユーザー、ssh キー、マウント ポイントなどを管理できることに注意してください。これらの特定の機能に関する詳細なドキュメントについては、以下の参照を参照してください。
一般に、cloud-init は指定されたモジュールに基づいて処理を行うようです。「disable-ec2-metadata」などの一部のモジュールは、指定するだけで機能します。「runcmd」などの他のものは、cloud.cfg または cloud-config userdata でパラメータが指定されている場合にのみ機能します。以下のドキュメントのほとんどは、モジュールの名前ではなく、各モジュールで使用できるパラメーターのみを示していますが、デフォルトの cloud.cfg には完全なモジュール リストが含まれている必要があります。モジュールを無効にする最善の方法は、単純にリストから削除することです。
場合によっては、「rhel」の方が「amazon」よりも「distro」タグに適している場合があります。いつだったかはよくわかりません。
参考文献
- cloud-init のインストール方法: http://web.archive.org/web/20140925130743/http://docs.openstack.org/grizzly/openstack-image/content/centos-image.html
- モジュール リファレンス (不完全): http://cloudinit.readthedocs.org/en/latest/topics/examples.html
- モジュール リファレンス (不完全): https://github.com/number5/cloud-init/blob/master/doc/examples/cloud-config.txt
- 一般的なセットアップ手順: http://web.archive.org/web/20150110200930/http://www.scalehorizontally.com/2013/02/24/introduction-to-cloud-init
- ホスト名の管理: http://web.archive.org/web/20140805225413/http://docs.openstack.org/user-guide/content/user-data.html
AWS EC2 (CentOS) で cloud-initを使用して、起動時にスクリプトを実行する方法に関する簡単なチュートリアルを次に示します。
このチュートリアルでは、次のことを説明します。
- 設定ファイルの設定方法
/etc/cloud/cloud.cfg
- 雲の道はどのように
/var/lib/cloud/scripts
見えるか- 例を使用したクラウド パスの下のスクリプト ファイル、および
- インスタンスの起動時にスクリプトファイルが実行されているかどうかを確認する方法
構成ファイル
以下の構成ファイルは AWS CentOS6 にあります。Amazon Linux については、こちらを参照してください。
# cat /etc/cloud/cloud.cfg
manage_etc_hosts: localhost
user: root
disable_root: false
ssh_genkeytypes: [ rsa, dsa ]
cloud_init_modules:
- resizefs
- update_etc_hosts
- ssh
cloud_final_modules:
- scripts-per-once
- scripts-per-boot
- scripts-per-instance
- scripts-user
ディレクトリ ツリー
クラウド パス/var/lib/cloud/scripts
は次のようになります。
# cd /var/lib/cloud/scripts
# tree `pwd`
/var/lib/cloud/scripts
├── per-boot
│ └── per-boot.sh
├── per-instance
│ └── per-instance.sh
└── per-once
└── per-once.sh
スクリプト ファイルの内容
サンプル スクリプト ファイルの内容は次のとおりです。
ファイルは user の下にある必要がありますroot
。ブート スクリプトの作成方法を参照してください。
# cat /var/lib/cloud/scripts/per-boot/per-boot.sh
#!/bin/sh
echo per-boot: `date` >> /tmp/per-xxx.txt
# cat /var/lib/cloud/scripts/per-instance/per-instance.sh
#!/bin/sh
echo per-instance: `date` >> /tmp/per-xxx.txt
# cat /var/lib/cloud/scripts/per-once/per-once.sh
#!/bin/sh
echo per-once: `date` >> /tmp/per-xxx.txt
実行結果
初回起動の場合
# cat /tmp/per-xxx.txt
per-once: 1 January 3, 2013 Thursday 17:30:16 JST
per-boot: 1 January 3, 2013 Thursday 17:30:16 JST
per-instance: 1 January 3, 2013 Thursday 17:30:16 JST
再起動の場合
# cat /tmp/per-xxx.txt
per-once: 1 January 3, 2013 Thursday 17:30:16 JST
per-boot: 1 January 3, 2013 Thursday 17:30:16 JST
per-instance: 1 January 3, 2013 Thursday 17:30:16 JST
per-boot: 1 January 3, 2013 Thursday 17:32:24 JST
AMI内から起動する場合
# cat /tmp/per-xxx.txt
per-once: 1 January 3, 2013 Thursday 17:30:16 JST
per-boot: 1 January 3, 2013 Thursday 17:30:16 JST
per-instance: 1 January 3, 2013 Thursday 17:30:16 JST
per-boot: 1 January 3, 2013 Thursday 17:32:24 JST
per-boot: 1 January 3, 2013 Thursday 17:44:08 JST