1. Academy
  2. Compute & Instances

How to use Cloud-init

Introduction

Cloud-init is a cross platform initialisation tool that works on many cloud platforms. Its main purpose is to bootstrap new instances with a predefined configuration file. This will help administrators to automate and accelerate server installations while reducing human error.

 

Prerequisites

For this tutorial you need the following:

  1. Active fuga Cloud Account

Using the fuga dashboard to add Cloud-init scripts.

Go to the fuga.cloud dashboard, and Create Instance. Select below Boot Source, a distribution, in this tutorial Ubuntu 20.04 LTS is being used.

Select a Boot disk size, flavor, network, key pair, security group, and give your instance a name.

Unfold the Advanced Settings, here you can add a customization script. 

This is where you can add your custom shell script. What it does, is when adding your script, the instance will lookup the user-data format, and run the script during first boot. This allows administrators to automate their tasks for deploying instances.

Example 1 - pre update and upgrade

In this script, you want to pre update and upgrade our instance on its first boot, this makes the instance ready to use without using the $ sudo apt update and $ sudo apt upgrade commands.

#cloud-config
package_update: true
package_upgrade: true

Example 2 - pre install packages.

When using the packages string in cloud-init, you have the opportunity to add multiple packages.

#cloud-config
packages:
- nginx
- php-mysql
- bind9

Note - it's important to know that YAML is whitespace sensitive. Mixing spaces and tabs will prevent your YAML script from being parsed and will result in error. You can simply use YAMLlint to verify the validation for the YAML script

Example 3 - make a directory with a file.

In this example, the script will run a command in the CLI of the instance after de deployment. In the following YAML script you create a directory and write some content in it.

#cloud-config
runcmd:
- [ mkdir, /fuga ]
write_files:
- path: /fuga/cloud-init.txt
content: Created a cloud-init script

Using Cloud-init in OpenStack. 

You have learned how to add YAML scripts on the Fuga Dashboard. You can also deploy an instance with the OpenStack CLI.

For this example open the terminal, create a file named “user-data.yaml” to add the YAML script. You can add the scripts that were made in the previous exemples.

$ tee user-data.yaml <<EOF
#cloud-config
hostname: webserver
apt_update: true
packages:
- apache2
- php-mysql
- php-curl
- php-gd
runcmd
- [ sh, -c, 'echo ubuntu:secret | chpasswd' ]
- [ mkdir, /webserver ]
write_files
- path: /webserver/websites
owner: ubuntu:ubuntu
permissions: '0775'
content: Y'all got some more of those.. Exemples?
EOF

 now use this file to create a new instance.

$ openstack server create --user-data ./user-data.yaml --flavor <flavor> --image <image> --keypair <key_name> --network <network> <name_of_host>

Troubleshooting Cloud-init

To make sure the script worked, use the following command to see the status of cloud-init. 

You can have two outputs from this:

$ cloud-init status
status: done
$ cloud-init status
status: error

To troubleshoot the error you can locate the logs by the following commands: 

$ tail /var/log/cloud-init-output.log
$ tail /var/log/cloud-init.log

For analyze logs: 

$ cloud-init analyze show -i /var/log/cloud-init.log

The purpose of Cloud-init, is that it only runs during the first boot, but for testing purposes it's good to know how to rerun the cloud-init script.

$ cloud-init clean --reboot

Conclusion

In this tutorial you have learned what Cloud-init is, and how Cloud-init works. Using some examples in YAML format. You know how to deploy an instance with a cloud-init configuration file and how to troubleshoot cloud-init deployments.