Posted on Leave a comment

Persistent storage on LXD instances

With other containerization technologies such as Docker, persistent volumes (or bind mounts) are common as the container’s life span is often short lived. With LXD instances, the containers live beyond the life of a single process and the demand for persistent storage is not as critical. With that said, there are many use cases for wanting persistent storage within your LXD instance. In this post we will examine how to create persistent storage by mounting a directory on the LXD host server into an instance.

We will create a new profile with the persistent storage settings and then apply that profile to an instance. Let’s call the new profile volume-demo-data. It can be named whatever you want but it’s good practice to name it something meaningful. We will be using a directory on the host server located at /demo/data as our persistent storage location. To create the new profile run the following command:

$ lxc profile create volume-demo-data

We now have an empty profile without any configurations. To get a list of the profiles you have on your LXD server and see the newly created volume-demo-data profile, run the following command:

$ lxc profile list
+---------------------+---------+
|    NAME             | USED BY |
+---------------------+---------+
| default             | 2       |
+---------------------+---------+
| volume-demo-data    | 0       |
+---------------------+---------+

We will need a directory on the host that can be used for persistent storage. For this post, we will create a new directory located at /demo/data. To create the new directories use the following command:

$ sudo mkdir -p /demo/data

The new profile now can be modified to include a disk device that mounts the /demo/data directory (source) to a location within the instance (path). We can choose to edit the profile directly using an editor (lxc profile edit volume-demo-data) or add a new disk device using the command-line.

Choosing the later option, we can create a new device named host-data that uses the host’s directory /demo/data, mounted within the instance at /var/demo/data, by running the following command:

$ lxc profile device add volume-demo-data host-data disk source=/demo/data path=/var/demo/data

To view the contents of the profile use the following command:

$ lxc profile show volume-demo-data
config: {}
description: ""
devices:
  host-data:
    path: /var/demo/data
    source: /demo/data
    type: disk
name: volume-demo-data
used_by: []

If you are launching a new instance you can use the –profile option to add profiles to the instance as it is created. But if the instance already exists you will have to append the new profile to the instance. Let’s assume we have an instance named container1. To append the volume-demo-data profile to this instance use the following command:

$ lxc profile add container1 volume-demo-data

To show the applied configurations to your instance use the following command:

$ lxc config show container1 -e

The profile configuration can be removed from the instance. To remove the profile use the following command:

$ lxc profile remove container1 volume-demo-data

When you remove the profile from the instance, the profile still exists and can be used for other instances. If you want to permanently remove the profile it can be deleted with the following command:

$ lxc profile delete volume-demo-data
Leave a Reply

Your email address will not be published.