Persistent Volumes
Generally, containers are stateless. That means that whenever you write anything to the filesystem of the container, it will be removed when the container restarts.
Altough it is recommend to keep your applications as stateless as possible, sometimes you will need to store data in a directory that should "survive" a container restart, e.g. the data directory within a database container (e.g. /var/lib/mysql
for mysql).
Persistent volumes allow you to define a virtual device which is independent of your containers and can be mounted into the containers. An analogy would be a USB stick (persistent volume) that you plug into a computer (container) which always resets the in-build hard drive on every restart.
When you want to persist a folder within a container of one of your components, you need to:
- Define a volume within this component
- Mount this volume into the container
note
Components require you to specify separete volumes for each component, i.e. the volumes of a component can only be mounted by the containers of the same component.
Define Volumes
DevSpace components allow you to define the following types of Kubernetes volumes:
Persistent Volumes
You can define persistent volumes in the volumes
section of each component deployment:
volumes:- name: nginx size: "2Gi"- name: mysql-data size: "5Gi"
The above example defines two volumes, one called nginx
with size 2 Gigabyte
and one called mysql-data
with size 5 Gigabyte
.
Show specification for Persistent Volumes
volumes:- name: [a-z0-9-]{1,253} # Name of the volume (used to mount the volume) size: [number] + Gi|Mi|Ki # Size of the volume in Gigabyte, Megabyte or Kilobyte
emptyDir
Volumes
Using DevSpace components, you can define temporary volumes to share files between containers of a Pod according to Kubernetes EmptyDirVolumeSource specification
volumes:- name: temp-storage emptyDir: {}
Show specification for emptyDir Volumes
volumes:- name: [a-z0-9-]{1,253} # Name of the volume (used to mount the volume) emptyDir: # Kubernetes EmptyDirVolumeSource v1 medium: "" | Memory # Default "". The storage medium type sizeLimit: nil | [number] + Gi|Mi|Ki # Default nil. Size of the volume in Gigabyte, Megabyte or Kilobyte ...
ConfigMap Volumes
Using DevSpace components, you can define Kubernetes ConfigMaps as volumes according to the Kubernetes ConfigMapVolumeSource specification:
volumes:- name: nginx-config configMap: name: my-configmap
Show specification for ConfigMap Volumes
volumes:- name: [a-z0-9-]{1,253} # Name of the volume (used to mount the volume) configMap: # Kubernetes ConfigMapVolumeSource v1 name: [a-z0-9-]{1,253} # Name of the ConfigMap ...
Secret Volumes
Using DevSpace components, you can define Kubernetes Secrets as volumes according to the Kubernetes SecretVolumeSource specification:
volumes:- name: secret-token secret: secretName: my-secret
Show specification for Secret Volumes
volumes:- name: [a-z0-9-]{1,253} # Name of the volume (used to mount the volume) secret: # Kubernetes SecretVolumeSource v1 secretName: [a-z0-9-]{1,253} # Name of the Secret ...
Mount Volumes
After defining a volume for a component, you can mount it in the containers of the same component within the volumeMounts
section:
containers:- image: "dscr.io/username/mysql" volumeMounts: - containerPath: /var/lib/mysql volume: name: mysql-data subPath: /mysql readOnly: falsevolumes:- name: mysql-data size: "5Gi"
The example above would create a volume called mysql-data
for the component my-component
and mount the folder /mysql
within this volume into the path /var/lib/mysql
within a container of this component. By mounting this volume to /var/lib/mysql
, you allow the container to edit the files and folders contained within /var/lib/mysql
and restart without losing these changes.
View the specification for volume mounts
containerPath: [path] # Path within the containervolume: # Volume to mount name: [volume-name] # Name of the volume as defined in `volumes` within `chart/values.yaml` subPath: [path] # Path within the volume readOnly: false|true # Detault: false | set to true for read-only mounting
Delete Persistent Volumes
By default, persisent volumes will not be deleted automatically when you remove them from the volumes
section of chart/values.yaml
. This ensures that persistent data is not being deleted on accident.
To remove a persistent volume, you have to remove it manually with the following commands:
devspace add space [SPACE_NAME]kubectl delete persistentvolumeclaim [VOLUME_NAME]
Data Loss
Deleting persistent volumes cannot be undone. Your data will be lost forever and cannot be recovered.
FAQ
Can I mount one persistent volume within multiple containers?
Yes, but only if the containers are either in the same component or if at most one of the containers mounts the volume with the readOnly: false
option (e.g. one container with readOnly: false
and 3 other containers with readOnly: true
would work).
Will my persistent volumes be deleted when I re-deploy my application?
Generally: No.
The component chart used to deploy DevSpace components will automatically deploy containers as part of a StatefulSet when you mount any persistent volumes. Kubernetes will not delete these persistent volumes when you delete or update the StatefulSet.
How can I delete everything (including persistent volumes) within a Space?
If you want to force-delete everything (including persistent volumes) within a Space, you can run the following commands:
devspace purgekubectl delete persistentvolumeclaims --all
Warning: The commands listed above will delete everything within your Space. All your data will be lost forever and cannot be recovered.