Cloud-init

Overview

Cloud-init is a software package which automates the initialization of servers during system boot. When you deploy a new Linux server from an image, cloud-init gives you the option to set default user data. User data must be written in 1) shell scripts or 2) cloud-config directives using YAML syntax. This method is highly compatible across platforms and fully secure.
Compatibility: This service is supported on all public IONOS Cloud Linux distributions (Debian, CentOS and Ubuntu). You may submit user data through the DCD or via Cloud API. Existing cloud-init configurations from other providers are compatible with IONOS Cloud.
Limitations: Cloud-init is available on all public images supplied by IONOS Cloud. If you wish to use your own Linux image, please make sure that it is cloud-init supported first. Otherwise, there is no guarantee that the package will function as intended. Windows images are currently out of scope; adding them may be considered at a later stage.
Provisioning: Cloud-init can only be set at initial provisioning. It cannot be applied to instances that have already been provisioned. Settings can't be changed once provisioned.
Laptops: When using a laptop, please scroll down the properties panel, as additional fields are not immediately visible on a small screen.

Supported user data formats

This tutorial demonstrates the use of cloud-config and user-data scripts. However, the cloud-init package supports a variety of formats.
Data Format
Description
Gzip
If user data is gzip compressed, cloud-init decompresses the data and handles it appropriately.
Base64
If user-data is base64-encoded, cloud-init determines if it can understand the decoded data as one of the supported types. If it understands the decoded data, it decodes the data and handles it appropriately. If not, it returns the base64 data intact.
User-Data Script
Begins with #!or Content-Type: text/x-shellscript.
The script is run by /etc/init.d/cloud-init-user-scripts during the first boot cycle. This occurs late in the boot process (after the initial configuration actions are performed).
Include File
Begins with #include or Content-Type: text/x-include-url.
The file contains a list of URLs, one per line. Each of the URLs is read, and their content passed through this same set of rules. The content read from the URL can be gzipped, MIME-multi-part, or plaintext.
Cloud Config data
Begins with #cloud-config or Content-Type: text/cloud-config.
For a commented example of supported configuration formats, see the examples.
Upstart Job
Begins with #upstart-job or Content-Type: text/upstart-job.
This content is stored in a file in /etc/init, and upstart consumes the content as per other upstart jobs.
Cloud Boothook
Begins with #cloud-boothook or Content-Type: text/cloud-boothook.
This content is boothook data. It is stored in a file under /var/lib/cloud and then runs immediately.
This is the earliest hook available. There is no mechanism provided for running it only one time. The boothook must take care of this itself. It is provided with the instance ID in the environment variable INSTANCE_ID. Use this variable to provide a once-per-instance set of boothook data

Configuring user data via DCD

1. In the DCD, create a new virtual instance and attach any storage device to it.
2. Ensure the storage device is selected. Its Inspector pane should be visible on the right.
3. When choosing the Image, you may either use your own or pick one that is supplied by IONOS.
For IONOS supplied images, select No image selected > IONOS Images.
Alternatively, for private images select No image selected > Own Images.
You may either use an IONOS image or upload your own via FTP and select it instead.
4. Once you choose an image, additional fields will appear in the Inspector pane.
5. A Root password is required for Remote Console access. You may change it later.
6. SSH keys are optional. You may upload a new key or use an existing file. SSH keys can also be injected as user data utilizing cloud-init.
7. You may add a specific key to the Ad-hoc SSH Key field.
8. Under Cloud-init user data, select No configuration and a window will appear.
9. Input your cloud-init data. Either use a bash script or a cloud-config file with YAML syntax. Sample scripts are provided below.
10. To complete setup, return to the Inspector and click Provision Changes. Cloud-init automatically runs at boot, applying the changes requested.
When the DCD returns the message that provisioning has been successfully completed this means the infrastructure is virtually set up. However, the bootstrapping, which includes the execution of cloud-init data, may take additional time. This execution time is not included in the success message. Please allow extra time for the tasks to complete before testing.

Using shell scripts

Using shell scripts is an easy way to bootstrap a server. In the example script below, the code creates and configures our CentOS web server.
1
#!/bin/bash
2
# Use this for your user data (script from top to bottom)
3
# install httpd (Linux 2 version)
4
yum update -y
5
yum install -y httpd
6
systemctl start httpd
7
systemctl enable httpd
8
echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html
Copied!
Allow enough time for the instance to launch and run the commands in your script, and then check to see that your script has completed the tasks that you intended.
The above example will install a web server and rewrite the default index.html file. To test if cloud-init bootstrapped your VM successfully, you can open the corresponding IP address in your browser. You should be greeted with a “Hello World” message from your web server.

Using cloud-config directives

Cloud-init images can also be bootstrapped using cloud-config directives. The cloud-init website outlines all supported modules and gives examples of basic directives.

Modules

Modules — cloud-init 21.3 documentation

Examples

Cloud config examples — cloud-init 21.3 documentation

Example 1: Creating a swap partition

The following script is an example of how to create a swap partition with a second block storage, using a YAML script:
1
#cloud-config
2
fs_setup:
3
- label: mylabel
4
device: /dev/vda
5
filesystem: ext4
6
- label: swap
7
device: /dev/vdb
8
filesystem: swap
9
mounts:
10
- [ /dev/vda, /, ext4, defaults, 0, 0 ]
11
- [ /dev/vdb, none, swap, sw, 0, 0 ]
Copied!

Example 2: Resizing the file system

The following script is an example of how to resize your file system according to the chosen size of the block storage. It will also create a user with a SSH key, using a cloud-config YAML script:
1
#cloud-config
2
resize_rootfs: True
3
users:
4
- name: pb-user
5
gecos: Demo User
6
sudo: ALL=(ALL) NOPASSWD:ALL
7
groups: users, admin
8
ssh_import_id: None
9
lock_passwd: true
10
ssh_authorized_keys:
11
- ssh-rsa AAAA...
Copied!

Output log files

The cloud-init output log file (/var/log/cloud-init-output.log) captures console output. Depending on the default configuration for logging, a second log file exists under /var/log/cloud-init.log. This provides a comprehensive record based on user data.

Configuring user data via API

Cloud API provides enhanced convenience if you want to automate provisioning and configuration of cloud instances. Cloud-init is configured on the volume resource in Cloud API V6 (or later). Please find the link to the documentation below:
CLOUD API (6.0-SDK.2)
Name: userData
Type: string
Description: The cloud-init configuration for the volume as base64 encoded string. The property is immutable and is only allowed to be set on a new volume creation. It is mandatory to provide either public image or imageAliasthat has cloud-init compatibility in conjunction with this property.

Curl example

1
curl --include \
2
--request POST \
3
--user '<user:password>' \
4
--header "Content-Type: application/json" \
5
--data-binary '{
6
"properties": {
7
"name": "Server-01",
8
"ram": 2048,
9
"cores": 1,
10
"availabilityZone": "ZONE_1",
11
"cpuFamily": "INTEL_SKYLAKE"
12
},
13
"entities": {
14
"volumes": {
15
"items": [ {
16
"properties": {
17
"size": 10,
18
"type": "HDD",
19
"name": "Server-01_HDD",
20
"image": "bf4d1400-b48d-11eb-b9b3-d2869b2d44d9",
21
"imagePassword": "<pAsSW0rD>",
22
"sshKeys": ["<ssh_key>"],
23
"userData": "I2Nsb3VkLWNvbmZpZwoKcGFja2FnZXM6CiAgLSBodHRwZAogIC0gZmlyZXdhbGxkCgpydW5jbWQ6CiAgLSAvYmluL3N5c3RlbWN0bCBlbmFibGUgaHR0cGQKICAtIC9iaW4vc3lzdGVtY3RsIHN0YXJ0IGh0dHBkCiAgLSAvYmluL2ZpcmV3YWxsLW9mZmxpbmUtY21kIC0tYWRkLXBvcnQ9ODAvdGNwCiAgLSAvYmluL3N5c3RlbWN0bCBlbmFibGUgZmlyZXdhbGxkCiAgLSAvYmluL3N5c3RlbWN0bCBzdGFydCBmaXJld2FsbGQKICAtIGxvYWRrZXlzIGRlCgp3cml0ZV9maWxlczoKLSBjb250ZW50OiB8CiAgICA8IURPQ1RZUEUgaHRtbD4KICAgIDxodG1sPgogICAgICA8aGVhZD4KICAgICAgPC9oZWFkPgogICAgICA8Ym9keT4KICAgICAgICA8cD5XZWxjb21lIHRvIHlvdXIgbmV3IHdlYiBzZXJ2ZXIuPC9wPgogICAgICA8L2JvZHk+CiAgICA8L2h0bWw+CiAgcGF0aDogL3Zhci93d3cvaHRtbC9pbmRleC5odG1sCgpmaW5hbF9tZXNzYWdlOiAiVGhlIHN5c3RlbSBpcyBmaW5hbGx5IHVwLCBhZnRlciAkVVBUSU1FIHNlY29uZHMiCg=="
24
}
25
} ]
26
},
27
"nics": {
28
"items": [ {
29
"properties": {
30
"name": "NIC001",
31
"dhcp": true,
32
"lan": 1
33
}
34
} ]
35
}
36
}
37
}' \
38
https://api.ionos.com/cloudapi/v6/datacenters/<datacenter_id>/servers
Copied!
Last modified 14d ago