Boot with Cloud-init
8 min
cloud init https //docs ionos com/cloud/support/general information/glossary of terms#cloud init is a software package that automates the initialization of servers https //docs ionos com/cloud/support/general information/glossary of terms#server during system boot when you deploy a new linux server from an image https //docs ionos com/cloud/support/general information/glossary of terms#image , cloud init gives you the option to set default user data user data must be written in shell scripts or cloud config directives using yaml syntax you can modify init's behavior via user data you can pass the user data in various formats to the init at launch time typically, this happens as a template, a parameter in the cli, etc this method is highly compatible across platforms and fully secure compatibility this service is supported on all public linux distributions you may submit user data through the dcd https //docs ionos com/cloud/support/general information/glossary of terms#dcd or via cloud api https //docs ionos com/cloud/support/general information/glossary of terms#application programming interface api existing cloud init configurations from other providers are compatible with limitations cloud init is available on all public linux images supplied by before using a custom linux image, it is important to ensure it supports cloud init otherwise, the package may not function as intended windows images and linux lvm 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 cannot be changed once provisioned laptops when using a laptop, scroll down the properties panel of the block storage volume that you want to create and configure, as additional fields are not immediately visible on a small screen clout init may only become visible when an supported image has been selected supported user data formats the following table demonstrates the use of cloud config and user data scripts however, the cloud init package supports a variety of formats data format description base64 if the user data is base64 encoded, cloud init verifies whether the decoded data is one of the supported types it decodes and handles the decoded data appropriately if it comprehends it if not, the base64 data is returned unaltered 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 include file is the content it contains a collection of urls, one in each line each url is read, and its content passes through the same set of rules the content read from the url can be 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 within a file in /etc/init , and upstart uses the content similar to other upstart jobs cloud boothook begins with #cloud boothook or content type text/cloud boothook the boothook data is the content, which is stored in a file within /var/lib/cloud and executed immediately this becomes the earliest hook and does not have any mechanism for executing it only one time the must be handled by the boothook 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 configure user data via the dcd in the workspace, create a new virtual instance and attach any storage device to it select the storage device and from the inspector pane associate an image with it to associate a private image, select own images from the drop down list to associate a public image, select ionos images from the drop down list once you choose an image, additional fields will appear in the inspector pane enter a password it is required for remote console access you may change it later (optional) upload a new ssh key or use an existing file ssh keys can also be injected as user data utilizing cloud init (optional) add a specific key to the ad hoc ssh key field select no configuration for cloud init user data and the cloud init user data window appears enter your user data either using a bash script or a cloud config file with a yaml syntax for sample scripts, see use shell scripts https //docs ionos com/cloud/compute services/cubes/dcd how tos/boot cloud init#use shell scripts , use cloud config directives https //docs ionos com/cloud/compute services/cubes/dcd how tos/boot cloud init#use cloud config directives , or configure user data via api https //docs ionos com/cloud/compute services/cubes/dcd how tos/boot cloud init#configure user data via api to complete setup, return to the inspector pane and click provision changes result at boot, cloud init executes automatically and applies the specified changes the dcd returns a message when provisioning https //docs ionos com/cloud/support/general information/glossary of terms#provisioning is complete, indicating that the infrastructure is virtually ready however, bootstrapping, which includes the execution of cloud init data, may require additional time the message that dcd returns does not mention the additional time required for execution we recommend allowing extra time for task completion before testing use shell scripts using shell scripts is an easy way to bootstrap a server the code creates, installs, and configures our centos web server in the following example it also rewrites the default index html file note allow enough time for the instance to launch and run the commands in your script, and later verify if your script has completed the tasks you intended \#!/bin/bash \# use this for your user data (script from top to bottom) \# install httpd (linux 2 version) yum update y yum install y httpd systemctl start httpd systemctl enable httpd echo "\<h1>hello world from $(hostname f)\</h1>" > /var/www/html/index html to test if the cloud init bootstrapped your vm https //docs ionos com/cloud/support/general information/glossary of terms#vm successfully, you can open the corresponding ip address https //docs ionos com/cloud/support/general information/glossary of terms#ip address in your browser you will be greeted with a “hello world” message from your web server use cloud config directives you can also bootstrap cloud init images using cloud config directives the cloud init website outlines all the supported modules https //cloudinit readthedocs io/en/latest/topics/modules html and provides examples https //cloudinit readthedocs io/en/latest/topics/examples html of basic directives example 1 create a swap partition the following script is an example of how to create a swap partition with second block storage using a yaml script #cloud config fs setup \ label mylabel device /dev/vda filesystem ext4 \ label swap device /dev/vdb filesystem swap mounts \ \[ /dev/vda, /, ext4, defaults, 0, 0 ] \ \[ /dev/vdb, none, swap, sw, 0, 0 ] example 2 resize 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 an ssh key using a cloud config yaml script #cloud config resize rootfs true users \ name pb user gecos demo user sudo all=(all) nopasswd\ all groups users, admin ssh import id none lock passwd true ssh authorized keys \ ssh rsa aaaa 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 within /var/log/cloud init log this provides a comprehensive record based on the user data configure user data via api the cloud api offers increased convenience if you want to automate the provisioning and configuration of cloud instances enter the following details name enter the userdata type enter the type in the form of a 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 imagealias that has cloud init compatibility in conjunction with this property cloud init is configured on the volume resource for cloud api v6 or later versions for more information, see cloud api (6 0) https //api ionos com/docs/cloud/v6/ curl example the following script is an example of how to configure userdata using curl curl include \\ \ request post \\ \ user '\<user\ password>' \\ \ header "content type application/json" \\ \ data binary '{ "properties" { "name" "server 01", "ram" 2048, "cores" 1, "availabilityzone" "zone 1", "cpufamily" "intel skylake" }, "entities" { "volumes" { "items" \[ { "properties" { "size" 10, "type" "hdd", "name" "server 01 hdd", "image" "bf4d1400 b48d 11eb b9b3 d2869b2d44d9", "imagepassword" "\<passw0rd>", "sshkeys" \["\<ssh key>"], "userdata" "i2nsb3vklwnvbmzpzwokcgfja2fnzxm6ciaglsbodhrwzaogic0gzmlyzxdhbgxkcgpydw5jbwq6ciaglsavymlul3n5c3rlbwn0bcblbmfibgugahr0cgqkicatic9iaw4vc3lzdgvty3rsihn0yxj0igh0dhbkciaglsavymlul2zpcmv3ywxslw9mzmxpbmuty21kic0tywrklxbvcnq9odavdgnwciaglsavymlul3n5c3rlbwn0bcblbmfibgugzmlyzxdhbgxkciaglsavymlul3n5c3rlbwn0bcbzdgfydcbmaxjld2fsbgqkicatigxvywrrzxlzigrlcgp3cml0zv9mawxlczoklsbjb250zw50oib8ciagica8iurpq1rzueugahrtbd4kicagidxodg1spgogicagica8agvhzd4kicagicagpc9ozwfkpgogicagica8ym9ket4kicagicagica8cd5xzwxjb21lihrvihlvdxigbmv3ihdlyibzzxj2zxiupc9wpgogicagica8l2jvzhk+ciagica8l2h0bww+ciagcgf0adogl3zhci93d3cvahrtbc9pbmrlec5odg1scgpmaw5hbf9tzxnzywdloiaivghlihn5c3rlbsbpcybmaw5hbgx5ihvwlcbhznrlciakvvbusu1fihnly29uzhmicg==" } } ] }, "nics" { "items" \[ { "properties" { "name" "nic001", "dhcp" true, "lan" 1 } } ] } } }' \\ https //api ionos com/cloudapi/v6/datacenters/\<datacenter id>/servers
