Creating a RancherOS Image for OpenStack
As I’ve been learning more about the container ecosystem, I’ve come across the concept of JeOS (just enough operating system). The idea here is that you want to gain as much performance out of your Docker containers as possible, so you minimize the cruft from your host operating system. There are several different JeOS options, but today we’ll talk about RancherOS. RancherOS is a very small (~20MB), operating system that you can use as a Docker host. Everything in RancherOS runs inside of a container and Docker itself runs as pid 1. RancherOS only ships as an ISO, so today, I’ll guide you through using the ISO to create a QCOW2 image for use in OpenStack.
First, you must ensure you have a proper KVM environment setup. This can involve quite a bit of configuration between making sure virtualization is allowed in the BIOS, your CPU supports it, etc.. I followed these directions on a new Ubuntu machine and it worked just fine.
You can test that your KVM setup is working properly by issuing virsh list. This should return an empty list:
root@ubuntu:/home/rsmitty# virsh list
Id Name State
Finally, install the virt-install tool with sudo apt-get install virtinst.
If you need an intro to Packer in general, I’ve written another guide that was published to the Solinea website. You can find that here.
Now that we are all set up, we need to create two files, a cloud-config.yml file that gets injected into RancherOS and a Packer template called kvm-rancheros.json that we’ll use to build our image.
Create a file called cloud-config.yml with the following content. Be sure to modify the ssh public key with your own, so that it gets baked into the image. Unfortunately, there’s no injection of keys during boot in OpenStack for RancherOS, so take care to make sure you get the correct one in there. Here’s what my cloud-config.yml looked like:
– ssh-rsa … spencer@Spencers-MacBook-Pro.local
There are lots of options for building images with Packer, so it can be a bit daunting at first. For our purposes, we will need to use the KVM builder directly and pass in the RancherOS ISO. Once booted, Packer will scp our cloud-config.yml file into the temporary instance and then issue the proper command to install RancherOS to disk. After this is complete, Packer will provide output on the location of the QCOW2 image. This image will simply be called “rancheros” and that path to it will be “$PWD/output_rancher/rancheros”. Here’s the full template:
“shutdown_command”: “sudo shutdown -h now”,
[ “-m”, “1024M” ]
“sudo rancheros-install -f -c cloud_config.yml -d /dev/vda”
Notice the ‘-m’ flag in the qemuargs section. You MUST have at least 1GB of RAM to complete the install successfully.
Build and Upload
It’s now time to build our image. Packer will fetch and verify the RancherOS ISO for us and proceed to take care of all of the necessary commands. Issue packer build kvm-rancheros.json.
Once our build is complete, we’ll want to upload it to OpenStack Glance. This image is ~40MB, so it shouldn’t take a terribly long time. You can issue the following command (after sourcing your OpenStack credentials):
$ glance image-create –name “RancherOS” \
–is-public false –disk-format qcow2 \
–container-format bare –file $PWD/output_rancher/rancheros
Launch Instance & Connect
We can now spin up our RancherOS instance inside of OpenStack. Ensure that you have ports 22, 80, and 2376 allowed in the security group that you choose to use for your instance.
Once our instance has been created, we will use Docker Machine’s generic driver to connect to our launched instance. Again, because SSH keys aren’t injected into RancherOS, we can’t use the OpenStack driver for Docker Machine and have it launch the instance for us. Instead, we will Docker Machine’s generic driver to control the new host. Here is the command that I used for connecting Docker Machine, notice I passed the path to the private SSH key that corresponds to the public key we injected earlier.
$ docker-machine create -d generic –generic-ssh-user rancher \
–generic-ssh-key ~/.ssh/id_rsa –generic-ip-address 192.168.1.202 \
Now, set rancher-dev as our Docker endpoint with the following:
$ eval “$(docker-machine env rancher-dev)”
You can ensure everything is connected with a combination of docker-machine ls and docker ps. I also like to echo a dashed line to give some separation in my commands.
$ docker-machine ls && echo “————” && docker ps
NAME ACTIVE DRIVER STATE URL SWARM
rancher-dev * generic Running tcp://192.168.1.202:2376
vbox-dev virtualbox Running tcp://192.168.99.100:2376
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Grow The Root Volume & Deploy Container
As mentioned previously, RancherOS doesn’t currently do some cloud-init functions like growing the root volume. We’ll need to launch a privileged contianer to do this. Issue the following:
$ docker run –privileged -i –rm ubuntu bash << EOF
apt-get install -y cloud-guest-utils parted
growpart /dev/vda 1
Credit to Darren Shepherd from Rancher for this script mentioned here
Launch my test-webserver container, which is a simple Apache container detailed here, by issuing the following:
$ docker run -d -p 80:80 rsmitty/test-webserver \
/usr/sbin/apache2ctl -D FOREGROUND
Check out the result!
Author: Spencer Smith