cloud-init の設定を上書き、拡張する方法

cloudDevOps

cloud-initについて調べたので、簡単な設定のやりかたの備忘録です。

スポンサーリンク

cloud-init とは?

cloud-init は AWS EC2 などの cloud instance に対して、設定や拡張性を提供するためのパッケージです。
そのため AWS EC2 の Amazon Linux や Ubuntu などのインスタンスには最初から組みこまれています。
インスタンス起動時などに動作して、OSの各種設定をしてくれています。
yaml形式の設定ファイルになっているので、独自の設定を記述することにより、プラットホーム特有の設定にしたり、ユーザーが任意の設定にすることができるようになります。
例えば AWS EC2 リージョンごとにapt packegeを提供するサーバの指定を cloud-init によって、最寄りのリージョンのホストへ向けるようになっています。
より詳しい内容については、公式ドキュメントを参照してください。
ディストリビューションによっては、独自の cloud-init を構築している場合がありますので、ディストリビューションの情報も参考にしてください。

設定ファイルや動作などについて

Amazon Linuxの情報は比較的目につきましたが、Ubuntuの情報が少なかったので、AWS EC2 での Ubuntu を元にして解説します。
以下が/etc/cloud/cloud.cfgというcloud-initの設定ファイルです。

# The top level settings are used as module
# and system configuration.
# A set of users which may be applied and/or used by various modules
# when a 'default' entry is found it will reference the 'default_user'
# from the distro configuration specified below
users:
   - default
# If this is set, 'root' will not be able to ssh in and they
# will get a message to login instead as the above $user (ubuntu)
disable_root: true
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false
# Example datasource config
# datasource:
#    Ec2:
#      metadata_urls: [ 'blah.com' ]
#      timeout: 5 # (defaults to 50 seconds)
#      max_wait: 10 # (defaults to 120 seconds)
# The modules that run in the 'init' stage
cloud_init_modules:
 - migrator
 - ubuntu-init-switch
 - seed_random
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - ca-certs
 - rsyslog
 - users-groups
 - ssh
# The modules that run in the 'config' stage
cloud_config_modules:
# Emit the cloud config ready event
# this can be used by upstart jobs for 'start on cloud-config'.
 - emit_upstart
 - disk_setup
 - mounts
 - ntp
 - ssh-import-id
 - locale
 - set-passwords
 - snappy
 - grub-dpkg
 - apt-pipelining
 - apt-configure
 - package-update-upgrade-install
 - fan
 - landscape
 - timezone
 - lxd
 - puppet
 - chef
 - salt-minion
 - mcollective
 - disable-ec2-metadata
 - runcmd
 - byobu
# The modules that run in the 'final' stage
cloud_final_modules:
 - rightscale_userdata
 - scripts-vendor
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change
# System and/or distro specific settings
# (not accessible to handlers/transforms)
system_info:
   # This will affect which distro class gets used
   distro: ubuntu
   # Default user name + that default users groups (if added/used)
   default_user:
     name: ubuntu
     lock_passwd: True
     gecos: Ubuntu
     groups: [adm, audio, cdrom, dialout, dip, floppy, lxd, netdev, plugdev, sudo, video]
     sudo: ["ALL=(ALL) NOPASSWD:ALL"]
     shell: /bin/bash
   # Other config here will be given to the distro class and/or path classes
   paths:
      cloud_dir: /var/lib/cloud/
      templates_dir: /etc/cloud/templates/
      upstart_dir: /etc/init/
      dhclient_lease:
   package_mirrors:
     - arches: [i386, amd64]
       failsafe:
         primary: http://archive.ubuntu.com/ubuntu
         security: http://security.ubuntu.com/ubuntu
       search:
         primary:
           - http://%(ec2_region)s.ec2.archive.ubuntu.com/ubuntu/
           - http://%(availability_zone)s.clouds.archive.ubuntu.com/ubuntu/
           - http://%(region)s.clouds.archive.ubuntu.com/ubuntu/
         security: []
     - arches: [armhf, armel, default]
       failsafe:
         primary: http://ports.ubuntu.com/ubuntu-ports
         security: http://ports.ubuntu.com/ubuntu-ports
   ssh_svcname: ssh
datasource:
  Azure:
    set_hostname: False
    agent_command: __builtin__

cloud-initの動作は

  • cloud_init_modules
  • cloud_config_modules
  • cloud_final_modules

というフェーズにわかれており、それぞれ動作するモジュールが記述されてい
ます。
system_info 以下にはyaml形式で、モジュール側で利用するための設定が
yaml形式で記述されています。この部分を上書きしたければ/etc/cloud/cloud.cfg.d以下にファイルを置けば、値を上書きしてくれます。
各種モジュールについてはドキュメントが用意されていますが、基本的にはそれっぽいモジュール名のものを見つけ、コードを読むのが確実です。

cloud-init 設定上書きの例

たとえば、aptのmirrorサイトを指定するのであれば /etc/cloud/cloud.cfg.d/99<sub>mirror.cfg</sub> というファイルを作成し、

system_info:
   package_mirrors:
     - arches: [i386, amd64]
       failsafe:
         primary: http://archive.ubuntu.com/ubuntu
         security: http://security.ubuntu.com/ubuntu
       search:
         primary:
           - http://mirror.example.com/ubuntu/

などと、独自のURLをvalueとして設定し、cloud-initを実行すれば`/etc/apt/souces.list’に設定されるホストが指定したものになります。

cloud-init の実行方法

cloud_config_modulesなどは再起動せずとも、以下のようにすれば実行されます。

sudo rm -rf /var/lib/cloud/*
sudo cloud-init init --local
sudo cloud-init init
sudo cloud-init modules --mode config # config の実行
sudo cloud-init modules --mode final  # final の実行

user-data としてインスタンス起動時に設定をわたす方法

設定ファイルをあらかじめ置いておかなくても良い、もうひとつの方法を紹介します。
AWS EC2などであれば、インスタンス起動時にuser-dataとしてcloud-initの設定ファイルをわたせば、それを利用してcloud-initが実行された状態でインスタンスが起動してきます。
その際には1行目に#cloud-configというのを挿入するのを忘れずに。

cloud-initのログについて

/var/log/cloud-init.log に出力されています。デバッグはここを見ながら。

AWS EC2を利用する上での注意

packerを利用してchefやpuppetによってOSの設定をしていると、ものによってはAMIから起動する際にcloud-initによって設定が戻されることになります。
cloud-initで設定されているものを把握して、cloud-initに寄せるものは寄せないと設定したはずなのに戻ってる状態になるので注意です。

まとめ

OSの基本的な設定などにcloud-initが利用されているので、cloud-initで設定されているものについてはcloud-initで設定してしまおうと思います。
あまり複雑なことをcloud-initでやるとメンテが大変なことになりそうなので、その他の設定についてはchef/puppet/ansibleなどを利用している感じです。

タイトルとURLをコピーしました