cloud-init – Server Initialization the Cloud Way
Although you do not usually click through an OS installer manually to set up a new cloud server, each server still needs a certain degree of individual configuration. This is where cloud-init comes into play as a versatile package that takes care of all the basic settings required to get started with a new server. In addition, it allows you to perfectly integrate the server into your specific cloud environment and connect it to your own tools right from the start.
Ready to use immediately thanks to cloud-init
In order to be able to start a new cloud server within seconds, the major Linux distributions provide images that essentially contain a snapshot of a hard disk with the fully installed operating system. A new server, which is created as a clone from such an image, is thus almost ready to use. Some details, however, such as the hostname or the authorized SSH keys, are not present in this generic image and still need to be set individually.
cloud-init package included in many images is activated at system startup and manages these settings in a fully automated way. When detecting that the specific server is starting for the first time, the full-blown process is run; in addition to the name of the server and access credentials, cloud-init also takes care of creating new SSH host keys, among other things. At cloudscale.ch, the public part of these keys is also output to the serial console, which means that we can display the fingerprints in the cloud control panel, allowing you to verify that the connection is trusted right from your first login. On subsequent system boots, cloud-init can, for example, resize the file system for you if you have scaled up your server's virtual volume in the meantime.
Hub for the config: the metadata server
Details such as hostname and SSH key, which you enter when launching a server, are stored on our metadata server. From here cloud-init can retrieve this data to properly configure your server. One way to get to the data is via what is known as the "Magic IP": the server is assigned a special route via DHCP, and cloud-init can then retrieve its config from the URL
http://169.254.169.254. We now also make this config available via "Config Drive", with each new server being assigned a virtual CD-ROM drive (e.g.
/dev/sr0) that contains its individual configuration.
To get the maximum benefit from cloud-init, you can use this tool for your own setup tasks as well. When launching a server, you can add "User Data" to specify a broad range of settings including any desired commands that will be executed on the new server without further interaction (see also the cloud-init documentation). In this way, your server installs the packages and patches of your choice and integrates itself into your config management or monitoring, before you even log in for the first time.
It goes without saying that you can also read and use the data from the metadata server with your own tools, e.g. get the UUID of your server in order to perform automated actions via our API. If you configure the network options statically, the data pertaining to your server is now still available locally thanks to the new Config Drive. Alternatively, you can also find the information in
/run/cloud-init/instance-data.json, where cloud-init stores a copy of the config it has read.
Good to know
As the version and capabilities of the
cloud-init package may differ considerably between various Linux distributions, be sure to check for each specific case which of the many features are supported or can be activated with optional modules. When adding User Data, also bear in mind that the metadata can potentially be read by any user or tool on your server.
Incidentally, although widely used, cloud-init is not the only project to automate the setup of new servers; one alternative is Ignition, which is used e.g. in Flatcar Container Linux. Ignition expects its config to be in JSON format, but when launching a server with Flatcar at cloudscale.ch, you can also choose to specify the User Data in the form of a YAML-formatted "cloud-config" as you would for cloud-init.
Irrespective of whether you keep a copy/paste template for use in the control panel or start servers through the API, with cloud-init and Ignition, in addition to creating servers, many setup steps that you used to perform manually can be automated. And even during normal operation, you – or your scripts – can access the relevant metadata of your servers at any time.
For a great start,
Your cloudscale.ch team