For the past few years I have been migrating between different OpenVZ VPS providers and each time it has been a hassle to migrate and upgrade my virtual machines between them. Then I stumbled upon LXD which in combination with a KVM VPS seems like a perfect fit for running multiple containers on a single KVM VPS and migrating them to and from others LXD hosts with a single command! Notice that using OpenVZ VPS’es is not possible since the kernel is too old. For this post I used VirtualBox, but plan on migrating the whole setup to a KVM VPS at RamNode (affiliate link) which I highly recommend.
Check out my post here for installing the latest stable version of LXD on Ubuntu 16.04
Installation of LXD host
Start by installing a copy of the newest Ubuntu server on VirtualBox, I used Ubuntu 16.04.1 LTS. This already comes with LXD which just need to be configured. I configured LXD with the default values except saying yes to making LXD available over the network. Replace
somepassword with your own password.
This will open the bridge configurator. I used the default values.
This should end with a
LXD has been successfully configured message.
Setup a container
Lets create an Ubuntu container
After it is done we confirm that the container is running
That’s it! We can now enter the container
Which will enter the container as root
Here we can as an example setup NGINX
Then exit the container
Which drops us back to the host. Here we can now access the webserver we just installed on the container
Lastly I wanted a static ip address for my containers
Find the line
LXD_CONFILE and change it to
LXD_CONFILE="/etc/default/lxd_dnsmasq.conf" and save the file. Next we create the
lxd_dnsmasq.conf file which will contain our static ip mapping
For each container add a line
dhcp-host=container_name,ip_address, for our container we add the following in the beginning of the file
dhcp-host=test-container,10.0.0.60 and save the file. Now stop the container
Restart the LXD bridge
And start the container again
Now this container will always use ip 10.0.0.60
Migrate containers between LXD hosts
Lets assume that this LXD host we have created has the ip of 192.168.1.20. And lets assume we have another LXD host at ip 192.168.1.30 (if you used VirtualBox for this guide you can just clone the whole setup and change the ip). We want to migrate the
test-container from 192.168.1.20 to 192.168.1.30.
Both LXD hosts
First we install
criu on both LXD hosts.
Then we add a reference to 192.168.1.20 from 192.168.1.30. Replace
somepassword with your own password from the beginning of this post.
We can now move or copy
test-container to 192.168.1.30
This can take a long time if the container is large or the connection is slow. For me it takes less than a minute, but there is no progress indicator at the moment. Lastly we check if the new container is transferred.
Since we cloned this host from 192.168.1.20, it also remembers our configuration that a container named
test-container will get the ip 10.0.0.60. Notice that a copy will stop the original container. To prevent this we can take a snapshot first and then transfer the snapshot.
Lastly one can play around with live migration, but I choose not to do that yet since it is an experimental feature in LXD 2.0, but take a look at this post for more information.