I have being flerting for some time with the idea to be able to spin a “datacenter in a box”, which would essentially work as my own “cloud” when I have some spare machines or am unable to use services like DigitalOcean by any reason. Or for my more ambitious plans to learn kubernetes on the near future, setting it up on a cluster of local VMs on a spare Intel NUC I have hanging around here at home idle.
For now I have being using Arch Linux with VirtualBox on this NUC. It serves the purpose to some extent, but I do not intend to use it forever. So I’ve always heard about KVM, which is a nice virtualization platform that I could use that is supported by many relatively old hardware, and is also well supported on Linux.
With the platform problem solution on the horizon, I am also searching for which open image format to use with KVM - since VDI, from Virtualbox, is, well, proprietary. I also did not intend to use vagrant boxes, and I’ve heard somewhere about qcow2
. It seemed the perfect fit, since I can find many linux “cloud” images on this image format available for download.
But to spin up a cloud image like DigitalOcean does on its droplets, it provides you ssh keys you can use to connect to the machine when it is ready. I needed this functionality, and after some more research I’ve learned that cloud images come with a package that provides a daemon called cloud-init
. It can do that and so, so much more.
cloud-init
service runs at boot time, looks for a configuration on a virtual CD-ROM device and finishes the configuration of the VM, like setting up users, passwords, SSH keys, running some scripts, copying some files, installing some packages etc before reaching the login prompt (it runs as root). Another interesting use is that you could use it to install e.g. ansible early on so it can take care of the remaining provisioning tasks, since it’s what it does best. :)cloud-init
installed and it has its corresponding system(d) service up early on the boot process, so cloud-init can hook up on it to do its job.qcow2
is a pretty nice image format, it can be used with qemu
or kvm
.qcow2
images to VirtualBox (and other formats also) with qemu-img
: qemu-img convert -f qcow2 [filename].qcow2 -O vdi [filename].vdi
centos
set up.