我如何在AWS成立云初始化自定义的AMI? (CentOS的) [英] How do I set up cloud-init on custom AMIs in AWS? (CentOS)

查看:249
本文介绍了我如何在AWS成立云初始化自定义的AMI? (CentOS的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义用户数据在AWS实例似乎做各种引导式操作非常有用。不幸的是,我必须使用自定义的CentOS AMI这并没有从所提供的AMI的PCI原因之一,起源,所以云的init尚未安装和配置。我只是真的想设置一个主机名和运行一个小的bash脚本。我如何得到它的工作?

Defining userdata for instances in AWS seems really useful for doing all kinds of bootstrap-type actions. Unfortunately, I have to use a custom CentOS AMI that didn't originate from one of the provided AMIs for PCI reasons, so cloud-init is not already installed and configured. I only really want it to set a hostname and run a small bash script. How do I get it working?

推荐答案

云初始化是一个非常强大,但很无证工具。即使安装完毕后,有很多的覆盖您可以在您的AMI已经确定的事情模块默认激活的。下面是一个最小的设置从头说明:

cloud-init is a very powerful, but very undocumented tool. Even once it's installed, there are lot of modules active by default that overwrite things you may have already defined on your AMI. Here are instructions for a minimal setup from scratch:

  1. 这是一个标准的软件库安装云初始化。如果你担心PCI,你可能不希望使用AWS的定制库。

  1. Install cloud-init from a standard repository. If you're worried about PCI, you probably don't want to use AWS's custom repositories.

# 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文件,以反映您所需的配置。下面是为每个模块文档最小配置。

  • Edit /etc/cloud/cloud.cfg, a yaml file, to reflect your desired configuration. Below is a minimal configuration with documentation for each module.

    #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 / ,将其删除。

  • If there is a defaults.cfg in /etc/cloud/cloud.cfg.d/, delete it.

    要采取这种结构的优势,定义了以下用户数据的新实例:

    To take advantage of this configuration, define the following userdata for new instances:

    #cloud-config
    hostname: myhostname
    fqdn: myhostname.mydomain.com
    runcmd:
     - echo "I did this thing post-boot"
     - echo "I did this too"
    

    您也可以简单地用替换#云配置运行bash脚本#!/斌/ bash的并把体内的bash脚本,但如果这样做,你应该从 cloud_init_modules 删除所有主机相关的模块。

    You can also simply run a bash script by replacing #cloud-config with #!/bin/bash and putting the bash script in the body, but if you do, you should remove all of the hostname-related modules from cloud_init_modules.


    请注意,这是一个最小配置,和云初始化能够管理用户,SSH密钥,安装点,等看看下面的参考有关这些特定功能的详细资料。对

    Note that this is a minimal configuration, and cloud-init is capable of managing users, ssh keys, mount points, etc. Look at the references below for more documentation on those specific features.

    在一般情况下,似乎云初始化确实根据指定的模块的东西。一些模块,如禁止-EC2元数据,做的东西简单地通过被指定。其他的,像RUNCMD,只有做的东西如果指定的参数,无论是在cloud.cfg,或云配置用户数据。大部分文档的下方只能告诉你哪些参数是可能的每个模块,不是什么模块调用,但是默认cloud.cfg应该有一个完整的模块列表开始。我发现禁用某个模块的最好方法是简单地从列表中删除。

    In general, it seems that cloud-init does stuff based on the modules specified. Some modules, like "disable-ec2-metadata", do stuff simply by being specified. Others, like "runcmd", only do stuff if their parameters are specified, either in cloud.cfg, or in cloud-config userdata. Most of the documentation below only tell you what parameters are possible for each module, not what the module is called, but the default cloud.cfg should have a complete module list to begin with. The best way I've found to disable a module is simply to remove it from the list.

    在某些情况下,红帽可以工作,为发行的标签比亚马逊更好。我还没有真正想通了的时候。

    In some cases, "rhel" may work better for the "distro" tag than "amazon". I haven't really figured out when.


    • 如何安装云初始化:<一href="http://web.archive.org/web/20140925130743/http://docs.openstack.org/grizzly/openstack-image/content/centos-image.html" rel="nofollow">http://web.archive.org/web/20140925130743/http://docs.openstack.org/grizzly/openstack-image/content/centos-image.html
    • 在模块参考(不完全): http://cloudinit.readthedocs.org/ EN /最新/主题/ examples.html
    • 在模块参考(不完全):的https://github.com/number5/cloud-init/blob/master/doc/examples/cloud-config.txt
    • 常规设置说明:<一href="http://web.archive.org/web/20150110200930/http://www.scalehorizontally.com/2013/02/24/introduction-to-cloud-init" rel="nofollow">http://web.archive.org/web/20150110200930/http://www.scalehorizontally.com/2013/02/24/introduction-to-cloud-init
    • 主机名管理:<一href="http://web.archive.org/web/20140805225413/http://docs.openstack.org/user-guide/content/user-data.html" rel="nofollow">http://web.archive.org/web/20140805225413/http://docs.openstack.org/user-guide/content/user-data.html
    • How to install cloud-init: http://web.archive.org/web/20140925130743/http://docs.openstack.org/grizzly/openstack-image/content/centos-image.html
    • Modules reference (incomplete): http://cloudinit.readthedocs.org/en/latest/topics/examples.html
    • Modules reference (incomplete): https://github.com/number5/cloud-init/blob/master/doc/examples/cloud-config.txt
    • General setup instructions: http://web.archive.org/web/20150110200930/http://www.scalehorizontally.com/2013/02/24/introduction-to-cloud-init
    • Hostname management: http://web.archive.org/web/20140805225413/http://docs.openstack.org/user-guide/content/user-data.html

    这篇关于我如何在AWS成立云初始化自定义的AMI? (CentOS的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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