Linux:Powerful Server Administration
上QQ阅读APP看书,第一时间看更新

Managing LXD containers – advanced options

In this recipe, we will learn about some advanced options provided by LXD.

How to do it…

Follow these steps to deal with LXD containers:

  1. Sometimes, you may need to clone a container and have it running as a separate system. LXD provides a copy command to create such clones:
    $ lxc copy c1 c2 # lxc copy source destination
    

    You can also create a temporary copy with the --ephemeral flag and it will be deleted after one use.

  2. Similarly, you can create a container, configure it as per you requirements, have it stored as an image, and use it to create more containers. The lxc publish command allows you to export existing containers as a new image. The resulting image will contain all modifications from the original container:
    $ lxc publish c1 --alias nginx # after installing nginx
    

    The container to be published should be in the stopped state. Alternatively, you can use the --force flag to publish a running container, which will internally stop the container before exporting.

  3. You can also move the entire container from one system to another. The move command helps you with moving containers across hosts. If you move a container on the same host, the original container will be renamed. Note that the container to be renamed must not be running:
    $ lxc move c1 c2 # container c1 will be renamed to c2
    
  4. Finally, we have the snapshot and restore functionality. You can create snapshots of the container or, in simple terms, take a backup of its current state. The snapshot can be a stateful snapshot that stores the container's memory state. Use the following command to create a snapshot of your container:
    $ lxc snapshot c1 snap1 # lxc snapshot container cnapshot
    
  5. The lxc list command will show you the number of snapshots for a given container. To get the details of every snapshot, check the container information with the lxc info command:
    $ lxc info c1
    ...
    Snapshots:
     c1/shap1 (taken at 2016/05/22 10:34 UTC) (stateless)
    

    Tip

    You can skip the snapshot name and LXD will name it for you. But, as of writing this, there's no option to add a description with snapshots. You can use the filename to describe the purpose of each snapshot.

  6. Once you have the snapshots created, you can restore it to go back to a point or create new containers out of your snapshots and have both states maintained. To restore your snapshot, use lxc restore, as follows:
    $ lxc restore c1 snap1 # lxc restore container snapshot
    
  7. To create a new container out of your snapshot, use lxc copy, as follows:
    $ lxc copy c1/snap1 c4 # lxc copy container/snapshot new_container
    
  8. When you no longer need a snapshot, delete it with lxc delete, as follows:
    $ lxc delete c1/snap1 # lxc delete container/snapshot
    

How it works…

Most of these commands work with the rootfs or root filesystem of containers. The rootfs is stored under the /var/lib/lxd/containers directory. Copying creates a copy of the rootfs while deleting removes the rootfs for a given container. These commands benefit with the use of the ZFS file system. Features such as copy-on-write speed up the copy and snapshot operations while reducing the total disk space use.