更新时间:2025-09-10 GMT+08:00
分享

配置Cloud-Init

在安装完Cloud-Init后,需要配置cloud.cfg文件,用于定制Cloud-Init在iMetal服务器初始化时的功能配置。

  1. 使用vi编辑器修改“/etc/cloud/cloud.cfg”配置文件为以下内容。

    # 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:
     - name: root
       lock_passwd: False
    
    # If this is set, 'root' will not be able to ssh in and they
    # will get a message to login instead as the default $user
    disable_root: false
    ssh_pwauth: true
    
    # This will cause the set+update hostname module to not operate (if true)
    preserve_hostname: false
    
    #manage_etc_hosts: localhost
    
    network:
      config: disabled
    
    apt:
      preserve_sources_list: true
    
    # Example datasource config
    # datasource:
    #    Ec2:
    #      metadata_urls: [ 'blah.com' ]
    #      timeout: 5 # (defaults to 50 seconds)
    #      max_wait: 10 # (defaults to 120 seconds)
    datasource_list: [ OpenStack ]
    datasource:
      OpenStack:
        metadata_urls: ['http://169.254.169.254/clouddc']
        max_wait: 300
        timeout: 5
        apply_network_config: false
    
    # The modules that run in the 'init' stage
    cloud_init_modules:
     - ssh
     - migrator
     - seed_random
     - bootcmd
     - write-files
     - growpart
     - resizefs
     - disk_setup
     - mounts
     - set_hostname
    # - update_hostname
     - update_etc_hosts
     - ca-certs
     - rsyslog
     - users-groups
    
    # 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
     - snap
     - snap_config  # DEPRECATED- Drop in version 18.2
     - ssh-import-id
     - locale
     - set-passwords
     - grub-dpkg
     - apt-pipelining
     - apt-configure
     - ubuntu-advantage
     - ntp
     - timezone
     - disable-ec2-metadata
     - runcmd
     - byobu
    
    # The modules that run in the 'final' stage
    cloud_final_modules:
     - snappy  # DEPRECATED- Drop in version 18.2
     - package-update-upgrade-install
     - fan
     - landscape
     - lxd
     - ubuntu-drivers
     - puppet
     - chef
     - mcollective
     - salt-minion
     - 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
       paths:
          cloud_dir: /var/lib/cloud/
          templates_dir: /etc/cloud/templates/
          upstart_dir: /etc/init/
       ssh_svcname: ssh

  2. 配置自动扩容。

    需要确保OS已安装growpart工具,UEFI引导还需额外安装gdisk工具

    sudo tee /etc/cloud/cloud.cfg.d/99-force-growroot.cfg <<EOF 
    resize_rootfs: true 
    growpart: 
      mode: 
    auto EOF

  3. 执行vi /lib/systemd/system/cloud-init-local.service配置cloud-init-local.service自启动项。

    注意依赖顺序,依赖顺序错误会导致cloud-init不生效。

    # /lib/systemd/system/cloud-init-local.service
    [Unit]
    Description=Initial cloud-init job (pre-networking)
    DefaultDependencies=no
    Wants=network-pre.target
    After=systemd-remount-fs.service
    Before=NetworkManager.service
    Before=network-pre.target
    Before=shutdown.target
    Before=sysinit.target
    Conflicts=shutdown.target
    RequiresMountsFor=/var/lib/cloud
    
    [Service]
    Type=oneshot
    ExecStart=/usr/bin/cloud-init init --local
    ExecStart=/bin/touch /run/cloud-init/network-config-ready
    RemainAfterExit=yes
    TimeoutSec=0
    
    # Output needs to appear in instance console output
    StandardOutput=journal+console
    
    [Install]
    WantedBy=cloud-init.target

  4. 执行vi /lib/systemd/system/cloud-init.service配置cloud-init.service自启动项。

    注意依赖顺序,依赖顺序错误会导致cloud-init不生效。

    [Unit]
    Description=Initial cloud-init job (metadata service crawler)
    DefaultDependencies=no
    Wants=cloud-init-local.service
    Wants=sshd-keygen.service
    Wants=sshd.service
    Wants=jarvis-network-config.service
    After=cloud-init-local.service
    After=network-online.target
    Before=sshd-keygen.service
    Before=sshd.service
    Before=systemd-user-sessions.service
    Conflicts=shutdown.target
    
    [Service]
    Type=oneshot
    ExecStart=/usr/bin/cloud-init init
    RemainAfterExit=yes
    TimeoutSec=0
    
    # Output needs to appear in instance console output
    StandardOutput=journal+console
    
    [Install]
    WantedBy=cloud-init.target

相关文档