Some application and services will require persistent data. Pod are ephemeral, they will likely die and the data will be removed. For that, K8s has an amazing feature called persistent volume, which is kind of a folder that won’t get lost on a restart.
Typically applications should not need to persist anything on disk not to violate 12 factor app principles.
We are going to use mongo as an example where all the data is kept in the directory /data/db
volumeMounts:
- name: mongo-persistent-volume
- mountPath: /data/db
We are going to specify where do we want the data to be and to create the directory if it does not exist:
volumes:
- name: mongo-persistent-volume
hostPath:
path: /mount/mongodbdata
type: DirectoryOrCreate
On this example we used hostPath
which mounts a file or a directory from the host node’s filesystem into your Pod.
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
# This GCE PD must already exist.
gcePersistentDisk:
pdName: my-data-disk
fsType: ext4
Sadly, depending on the provider we are using the volume type will vary, and for that reason, Kubernetes added a new abstraction called Persistent Volume Claim. So, instead of manually creating the volume with our IaaS specific configuration, we should be using Persistent Volume Claims that way we will decouple the creation and administration of those resources.
However, there is another way to create them and it is by having Dynamic Volumes. We can have a default one so that we don’t have to have to create them beforehand. Careful since you can start requesting a lot of disk if you are not cleaning them and it will increase your IaaS cost.