Using Volumes
A volume is a persistent storage device that keeps data across restarts. On Unikraft Cloud (UKC), you can initialize a volume through an instance, or you can create one by importing local data, either from a directory, or through a Dockerfile-driven build.
In this guide we will create a volume, attach it to an instance (a web server), and write to it. We will then show how to detach it, kill that instance, and attach it to a new instance, showing that what we wrote to it persisted across instances. Finally, we will discuss how you can share volumes across instances (read-only at present).
Setting Up the Volume
To start, let’s create the volume via kraft cloud volume
, with size in MBs and name my-volume
:
The command should return the volume’s UUID, and you can check the operation worked via:
which should output something similar to:
Notice the ATTACHED TO
field is empty as we haven’t attached it to any instances yet.
Populating the Volume with Local Data (Optional)
If you’d like to populate your empty volume with local data, you can use the kraft cloud volume import
command.
For example, assuming the volume’s name is my-volume
and that the data you want to import are in your my-data
directory, you would run:
You should see output similar to:
Setting Up the Web Server
We’ll use a Flask web server to write to the volume:
Replace the contents of server.py
with:
On every request, this simple server will write a timestamp to a file on the mounted persistent volume and print out the current contents of the file.
With this in place, let’s start our Flask web server, create a service for it via the -p
flag, and have the instance mount the my-volume
volume we created earlier at /mnt
:
You should see output similar to:
To see that the volume is attached, run:
You should see output similar to:
Testing it
Our Python Flask server should write the time and date to the volume (more specifically to /mnt/log.txt
) each time the server’s URL is accessed.
To test simply curl
the URL multiple times, you should see output similar to:
To test that the data (the date and time) were properly written to the persistent volume, let’s first stop the instance, detaching the volume, and removing the instance:
Now let’s start another instance and, like before, mount the same volume:
This should output something similar to:
Do one final curl
to this new URL; you should see the contents written by the previous instance on there,
plus a new entry at the bottom:
Clearning Up
To clean up the volume, detach it from all instances and then remove it:
Learn More
- The
kraft cloud
CLI reference, and in particular the deploy and volumes sub-commands - Unikraft Cloud’s REST API reference, and in particular the section on volumes