Cloud-Init & Cloudbase-Init

Last updated: 2019-10-30 15:29:36

PDF

Cloud-init

What is cloud-init?

Cloud-init is an open source tool that runs inside a CVM instance as a non-resident service. It is executed at startup and exits immediately after execution. It does not monitor any ports.
All the Linux public images of Tencent Cloud are pre-installed with the cloud-init service. You need to run the service as the root user because the service is mainly used for the initialization of CVM instances such as configuring DNS, hostname, and IP, and the execution of some custom scripts that users specify to be executed during the first boot when creating the CVM instances.

How to check whether the cloud-init service inside a Linux instance is working properly?

Checking the operation of cloud-init

First, log in to the instance and execute the following commands to see if any error is returned. If the execution result is displayed, it means that the service is running normally. Otherwise, an error will be prompted. Please troubleshoot according to the prompt.

  1. Delete the cloud-init cache directory.
    rm -rf /var/lib/cloud
  2. Perform complete cloud-init initialization.
    cloud-init init --local
  3. Pull data from the configured data source.
    cloud-init init
  4. Cloud-init initialization involves multiple stages. To ensure sufficient dependency between the stages, config stage is specified for the cloud-init modules.
    cloud-init modules --mode=config
  5. Specify final stage for the cloud-init modules.
    cloud-init modules --mode=final

What initialization operations does cloud-init perform on instances?

Tencent Cloud implements all instance initialization operations through cloud-init, ensuring the transparency of the operations inside an instance. The following briefly covers some initialization operations. For more details, see cloud-init documentation.

Initialization operationsDefault behaviorCustomizationNotes
hostname initialization During the first boot of an instance, cloud-init will set the hostname of the instance according to the hostname information in vendor_data.json. If you create or reinstall an instance with a custom image and you want to keep the custom hostname of the image, you can delete the configuration, - scripts-user, from /etc/cloud/cloud.cfg before creating the custom image. After you disable - scripts-user, the initialization script, /var/lib/cloud/instance/scripts/runcmd, inside the instance will not be run. Disabling the configuration will also affect the initialization of other sub-items such as the installation of cloud monitor and cloud security and software source settings. Also, the custom script will not be run when you create the CVM.
/etc/hosts initialization During the first boot of an instance, cloud-init initializes /etc/hosts to 127.0.0.1 $hostname by default. If you create or reinstall an instance with a custom image and you want to keep the custom /etc/hosts setting of the image, you can delete the configuration, - scripts-user, from /etc/cloud/cloud.cfg before creating the custom image.
  • After you disable - scripts-user, the initialization script, /var/lib/cloud/instance/scripts/runcmd, inside the instance will not be run. Disabling the configuration will also affect the initialization of other sub-items such as the installation of cloud monitor and cloud security and software source settings. Also, the custom script will not be run when you create the CVM.
  • Every time the CVM restarts,the /etc/hosts settings of some existing CVMs will be overwritten.
DNS Initialization (non-DHCP scenario) During the first boot of an instance, cloud-init will set the DNS of the instance according to the nameservers information in vendor_data.json. If you create or reinstall an instance with a custom image and you want to keep the custom DNS setting of the image, you can delete the configuration, - resolv_conf and unverified_modules: ['resolv_conf'], from /etc/cloud/cloud.cfg before creating the custom image. None.
Software source initialization During the first boot of an instance, cloud-init will set the software source of the instance according to the write_files information in vendor_data.json.If you create or reinstall an instance with a custom image and you want to keep the custom software source setting of the image, you can delete the configuration, - write-files, from /etc/cloud/cloud.cfg before creating the custom image. None.
NTP initialization During the first boot of an instance, cloud-init will set the NTP server configuration of the instance according to the NTP server information in vendor_data.json and start the NTP service. If you create or reinstall an instance with a custom image and you want to keep the custom NTP configuration of the image, you can delete the configuration, - ntp, from /etc/cloud/cloud.cfg before creating the custom image. None.
Password initialization During the first boot of an instance, cloud-init will set the default account password of the instance according to the chpasswd information in vendor_data.json. If you create or reload an instance with a custom image and you want to keep the custom default password of the image, you can delete the configuration, - set-passwords, from /etc/cloud/cloud.cfg before creating the custom image. None.
Key binding During the first boot of an instance, cloud-init will set the default account key of the instance according to the ssh_authorized_keys information in vendor_data.json. If you create or reinstall an instance with a custom image and you want to keep the custom default key of the image, you can delete the configuration, - users-groups, from /etc/cloud/cloud.cfg before creating the custom image. If you manually bind the instance with a key inside the instance, the previous key will be overwritten when the key binding operation is performed via the console.
Network initialization (non-DHCP scenario) During the first boot of an instance, cloud-init will set the IP, Gateway, Mask, etc., according to the information in network_data.json. If you create or reinstall an instance with a custom image and you want to keep the custom network information of the image, you can add network: {config: disabled} to /etc/cloud/cloud.cfg before creating the custom image. None.

1. Error due to the uninstallation of the cloud-init dependencies

  • Problem description:
    When commands are used to check whether the cloud-init service is working properly, the following error is returned:
    Traceback (most recent call last):
    File "/usr/bin/cloud-init", line 5, in 
      ********
      raise DistributionNotFound(req)
    pkg_resources.DistributionNotFound: pyyaml
  • Problem analysis:
    “pkg_resources.DistributionNotFound: xxxxx” indicates that the cloud-init dependencies have been uninstalled.
  • Solution:
    1. Reinstall the dependencies.
    2. Follow Checking the operation of cloud-init to see if the error is returned again.

2. Error due to the modification of the default Python interpreter

  • Problem description:
    An error is returned when cloud-init is run on startup.
  • Problem analysis:
    When cloud-init is installed, Python 2 is used as the default Python interpreter, which means that symbolic links, /usr/bin/python and /bin/python, are linked to Python 2. Based on their needs, users may change the default Python interpreter to Python 3 inside the instance by directing the symbolic links, /usr/bin/python and /bin/python, to Python 3. Due to compatibility issues, an error will be returned when cloud-init is run on startup.
  • Solution:
    1. Modify the Python interpreter specified in the /usr/bin/cloud-init file by changing #/usr/bin/python or #/bin/python to #! user/bin/python.

      Do not use symbolic links. Point directly to a specific interpreter.

    2. Follow Checking the operation of cloud-init to see if the error is returned again.

Cloudbase-Init

What is Cloudbase-Init?

Like cloud-init, Cloudbase-Init is a bridge by which you can communicate with Windows CVM instances. The Cloudbase-Init service is run when an instance boots up for the first time. The service will read the initialization configuration information of the instance and initialize it. Following operations such as resetting password and modifying IP addresses are also done via Cloudbase-Init.

How to check whether the Cloudbase-Init service inside a Windows instance is working properly?

Checking the operation of the Cloudbase-Init service:

  1. Log in to the instance.

    If you forget your password or fail to reset your password because of Cloudbase-Init service exceptions, you can reset your password by following step 2.

  2. Open Control panel > Administrative tools > Services.

  3. Find the Cloudbase-Init service, right-click it, and go to Properties.

    • Check “Startup type” and make sure it is set to “Automatic” as shown below:

    • View “Logon identity” and ensure that “Local System account” is selected as shown below:

    • Manually start the Cloudbase-Init service and see if any error is returned.
      If any error is returned, you need to fix the issue first and check whether you have installed any security software which may stop Cloudbase-Init from performing related operations.

    • Open the registry, locate all “LocalScriptsPlugin”, and make sure its value is 2 as shown below:

    • Check whether CD-ROM loading is disabled. If there is an optical disc drive as shown in the figure below, it means that the loading has not been disabled; otherwise, it means that it has been disabled and needs to be enabled.

Failed to reset password during initialization

  • Possible reasons:
    • The Cloudbase-Init account password is manually changed, which results in the failure to start the Cloudbase-Init service, which further led to the failure of operations such as resetting password during initialization.
    • The Cloudbase-Init service is disabled, which led to the failure of operations such as resetting password during initialization.
    • The security software installed stops the Cloudbase-Init service from resetting password so that the operation returns a successful result but actually failed.
  • Solution:
    Follow the corresponding solution to each possible reason to fix the issue.
    1. Change the Cloudbase-Init service to LocalSystem service. For details, see step 2 in Checking the operation of the Cloudbase-Init service.
    2. Change the startup type of the Cloudbase-Init service to automatic. For details, see step 2 in Checking the operation of the Cloudbase-Init service.
    3. Uninstall the security software involved or add the relevant operations of the Cloudbase-Init service to the white list of the security software.