cloud-init和rc.local

What is cloud-init?

Cloud-init is a service that customizes Linux-based cloud servers. It's supported by most Linux distributions and public cloud vendors, and it's the industry standard to initialize cloud virtual machines. Canonical created cloud-init more than a decade ago for Ubuntu, and since then, all major public clouds and most Linux distributions have added support for cloud-init. 

Advantages of cloud-init

You'll discover several advantages when you start using cloud-init. First, it's portable. If you developed scripts at another public cloud, you could deploy those at Vultr without changes. You can also do many things with cloud-init that aren't easy to do with imageboot scripts, like:

  • You can override Vultr's vendor-data with your user-data.
  • You can inject shell scripts that run once per boot or once per instance.
  • You have advanced logging and debugging tools with cloud-init analyze.
  • It's easy to reset an instance and run cloud-init again without a full reinstall.

What is cloud-init?

Cloud-init is an industry standard method for automating cloud instance initialization, with support across distributions and platforms. Cloud-init manages initialization using a combination of instance metadata and configuration scripts (user data) to automate the process of setting up a new server.

Akamai’s Metadata service provides an API for cloud-init to consume, offering the relevant instance and user data to initialize your server. When your new instance spins up, cloud-init starts running locally, accesses the metadata, and automatically configures your system accordingly.

Metadata Availability
Akamai’s Metadata service is now available in select data centers. Use Metadata to automate system configuration by adding directives or scripts when deploying Compute Instances. This user data can then be consumed by cloud-init, an industry standard system initialization tool, or accessed directly using the Metadata API. For instructions on using the Metadata service and for a list of supported regions and distributions, reference our documentation.

Differences:

cloud-init and rc.local are both methods for initializing and configuring a Linux system during the boot process. However, they serve different purposes and have distinct characteristics.

cloud-init:

cloud-init is a cloud-based initialization framework specifically designed for provisioning and configuring virtual machines (VMs) in cloud environments. It provides a standardized and automated approach to setting up cloud instances, including tasks like:

  1. Network Configuration: Setting up the network interface and assigning IP addresses based on cloud metadata.

  2. User Configuration: Creating user accounts and setting passwords based on cloud-provided credentials.

  3. Package Installation: Installing software packages and configuring services according to user-defined scripts or cloud metadata.

  4. Run Commands: Executing arbitrary commands during the boot process to perform specific system customizations.

cloud-init typically runs early in the boot process, before the system fully enters the multi-user runlevel. It utilizes cloud-provided metadata or user-supplied configuration files to determine the actions to be performed.

rc.local:

rc.local is a traditional script file used for executing custom commands or scripts during the boot process. It's located at /etc/rc.local and is typically executed at the end of the boot process, after the system has entered the multi-user runlevel.

rc.local is a more general-purpose tool compared to cloud-init. It can be used for various tasks, such as:

  1. Starting System Services: Launching specific services that should start after the system has initialized.

  2. Mounting Filesystems: Mounting additional filesystems that are not automatically mounted by the system.

  3. Running User Scripts: Executing user-defined scripts to perform specific configuration or customization tasks.

rc.local's execution order is determined by its filename and placement within the /etc/rc.d directory structure.

Key Differences:

Featurecloud-initrc.local
PurposeCloud-based VM provisioning and configurationGeneral-purpose boot-time script execution
Execution TimingEarly in the boot processLate in the boot process
Configuration SourceCloud-provided metadata or user-supplied configuration filesUser-defined scripts or commands
Typical Use CasesNetwork configuration, user configuration, package installation, command executionStarting services, mounting filesystems, running user scripts

Choosing Between cloud-init and rc.local:

  • Use cloud-init: If you're managing VMs in a cloud environment, cloud-init is the preferred choice for its automation, standardization, and cloud-integration capabilities.

  • Use rc.local: For specific tasks that require execution after the system has initialized and for more granular control over boot-time scripts, rc.local is a suitable option.

In summary, cloud-init is a powerful tool specifically designed for cloud-based VM provisioning and configuration, while rc.local is a more general-purpose script execution mechanism for boot-time tasks. The choice between the two depends on the specific needs and environment of your system.

[root@vultr ~]# cat /etc/cloud/cloud.cfg
cloud_init_modules:
  - ssh
  - yum-add-repo
cloud_config_modules:
  - ssh-import-id
  - keyboard
cloud_final_modules:
  - ansible
  - mcollective
  - salt-minion
system_info:
  distro: centos
  paths:
    cloud_dir: /var/lib/cloud/
    templates_dir: /etc/cloud/templates/
  ssh_svcname: sshd

Troubleshoot metadata and cloud-init

View cloud-init logs

If you encounter issues with cloud-init or your user data, you can review the logs and output logs by running one of the commands below:

cat /var/log/cloud-init.log
cat /var/log/cloud-init-output.log

If you are not able to access your system through SSH, you can use Lish or boot your instance into rescue mode and mount your disks.

Consider Using Akamai's New Metadata Service

Akamai's Metadata service is now available in select data centers and should be considered for new projects as an alternative to StackScripts. Use Metadata to automate system configuration by adding directives or scripts when deploying Compute Instances. This user data can then be consumed by cloud-init, an industry standard system initialization tool, or accessed directly using the Metadata API. For instructions on using the Metadata service and for a list of supported regions and distributions, reference our documentation.

User data formats

User data can be provided in many different formats, with the most common being cloud-config.

  • Cloud-config script: cloud-config is the default syntax for cloud-init and can be used on any Linux distribution. It contains a list of directives formatted using YAML. Review the Using cloud-config files to configure a server guide for more details.

    #cloud-config
    package_update: true
    package_upgrade: true
    packages:
    - nginx
    - mysql-server
    
  • Executable script: Cloud-init also accepts other scripts that can be executed by the target distribution. This includes bash and python. Since many commands (including those to create users and install packages) differ between distributions, providing these scripts may limit which distributions you can target.

    #!/bin/bash
    apt-get update -y && apt-get upgrade -y
    apt-get install nginx mysql-server -y
    
  • Other formats: Review the User data formats guide within the official documentation to learn more about other types of formats supported by cloud-init.

  • 测试:

[root@vultr ~]# cat test.yaml 
#cloud-config
timezone: Asia/Shanghai
[root@vultr ~]# cloud-init schema --config test.yaml
Valid schema test.yaml
[root@vultr ~]# 
[root@vultr ~]# cat test2.yaml 
#!/bin/sh
echo "Hello World" > /var/tmp/output.txt
[root@vultr ~]# cloud-init schema --config test2.yaml
User-data type 'text/x-shellscript' not currently evaluated by cloud-init schema
[root@172-236-13-22 ~]# cat test3.yaml 
#cloud-config
timezone: Asia/Shanghai
#!/bin/sh
echo "Hello World" > /var/tmp/output.txt
[root@172-236-13-22 ~]# cloud-init schema --config test3.yaml 
Invalid UNKNOWN_CONFIG_HEADER test3.yaml
Error: Cloud config schema errors: format-l4.c1: File test3.yaml is not valid YAML. while scanning a simple key
  in "<unicode string>", line 4, column 1:
    echo "Hello World" > /var/tmp/ou ... 
    ^
could not find expected ':'
  in "<unicode string>", line 5, column 1:
    
    ^

Error: Invalid schema: UNKNOWN_CONFIG_HEADER
使用MIME multi-part
vbird@DESKTOP-3NQUT0E:~$ cloud-init devel make-mime -a set-tz-cloud-config.txt:cloud-config -a install_ss.sh:x-shellscript > userdata3
vbird@DESKTOP-3NQUT0E:~$ cat userdata3
Content-Type: multipart/mixed; boundary="===============3740234081866345669=="
MIME-Version: 1.0

--===============3740234081866345669==
Content-Type: text/cloud-config; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="set-tz-cloud-config.txt"

I2Nsb3VkLWNvbmZpZwp0aW1lem9uZTogQXNpYS9TaGFuZ2hhaQo=

--===============3740234081866345669==
Content-Type: text/x-shellscript; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="install_ss.sh"

IyEvYmluL2Jhc2gKIyAxCnN5c3RlbWN0bCBzdG9wIGZpcmV3YWxsZAplY2hvICJcbiMjIyMjI1xu

评论

此博客中的热门博文

码率单位

Process vs. Thread

日志文件系统