A volume is a persistent storage device that keeps data across restarts.
On KraftCloud, 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.
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:
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 group 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
persient 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: