What is Cloud-Init and Why is it so Cool?

Cloud-Init Header Picture

Setting up new cloud instances can be very time-consuming, especially if you are dealing with a lot of configurations and software. With cloud-init, you’ll never have to worry about this ever again.

Cloud-init is a software developed for cloud server instances that will set up the server automatically using the provided metadata. This metadata describes how your server should look like, e.g. which packages should be installed and commands that’ll be executed on init.

How does it work?

Cloud-init will set up your cloud instance as described in a config file. If you’re familiar with Docker, the cloud-init config is like the Dockerfile and the cloud instance is the Container. The config file is written in YAML and you can use it as a blueprint for new cloud instances. Here is an example of a cloud.cfg:

#cloud-config

users:

    - default

disable_root: true

preserve_hostname: false

apt_preserve_sources_list: true

system_info:

   distro: debian

   default_user:

      name: debian

      lock_passwd: True

      gecos: Debian

      groups: [adm, audio, cdrom, dialout, dip, floppy, netdev, plugdev, sudo, video]

      sudo: ["ALL=(ALL) NOPASSWD:ALL"]

      shell: /bin/bash

   paths:

      cloud_dir: /var/lib/cloud/

      templates_dir: /etc/cloud/templates/

      upstart_dir: /etc/init/

   package_mirrors:

     - arches: [default]

       failsafe:

         primary: http://deb.debian.org/debian

         security: http://security.debian.org/

   ssh_svcname: ssh

bootcmd:

  - echo “Hello world!” > /home/hello.txt

packages:

  - vim

  - git

  - htop

This config will install the packages vim, git, and htop and also create a new file /home/hello.txt with “Hello world!”. Also, it’ll create a default user as described in system_info and disable the root user.

There are many modules you can use to configure your server exactly how you need it. For example, use the bootcmd module to run custom commands early in the boot process. Or create custom user accounts and assign them groups using the users and groups module.

You can find more examples of cloud-init configs here.

Why Cloud-Init when I have Docker?

When talking about replicating environments, you might think about Docker instead. But the good news is, you don’t actually have to choose between the two. Because in order to use Docker, you need to have Docker installed and configured on the machine and that’s what cloud-init is for. When setting up a new machine, cloud-init can install and set up Docker for you and then Docker can be used to run your software.

Cloud-Init at Contabo

Contabo customers can use cloud-init with Cloud VPS and VDS. The best way to do that is to use the Contabo API. With the API, it’s possible to pass your cloud-init config directly when you order the VPS. After your VPS is ready, your configuration is already applied and the server is ready. It is also possible to enable cloud-init on existing server and reinstall it using cloud-init.

Head over to the API documentation to read more about setting up a cloud instance using the Contabo REST API.

The Full Power of Cloud-Init

Cloud-init can be used way beyond just setting up the cloud instance with specific users and packages. For example, you can set up a whole LAMP stack application like WordPress using only this method. With the Contabo API, this means a new WordPress installation is just one API call away.

This also means that creating multiple machines with the same configuration and software isn’t a big deal anymore. Create a cloud-init config once, use it all the time. There is no need to manually configure all of the instances anymore.

Disaster Recovery

Setting up new machines may also be required in the event of a disaster, as you will need more instances to handle the workload if others fail. This is another use case of cloud-init, as in such times it is more than important that new instances with all the required configuration and software are quickly up and running.

Recreating a specific environment is no big deal anymore when you have cloud-init, and this is very important in cases where you have to replicate a machine quickly.

Clusters and Kubernetes

In a cluster, you have multiple nodes (machines) to perform specific tasks and quickly respond to changing workloads. But even then, you will get to the point where your current server configuration just isn’t capable of handling all the tasks and maybe you’ll get there much faster than expected, so it’s important to act quickly. Again, that’s where cloud-init comes to the rescue by setting up the new nodes automatically according to a central configuration so no manual configuration is needed.