How to setup a local persistent volume in kubernetes
I’m running a single node kubernetes cluster and one of the first things I needed was persistent storage. To create a volume that you can mount into your containers in a pod you have to create a PersistentVolume (PV) and then request it with a PersistentVolumeClaim (PVC).
Create a PersistentVolume (PV) object, pointing at a path on your host. Note the spec.capacity.storage
, spec.hostPath.path
and change these accordingly.
1apiVersion: v1
2kind: PersistentVolume
3metadata:
4 name: persistent-test-volume
5 labels:
6 name: persistent-test-volume
7spec:
8 volumeMode: Filesystem
9 storageClassName: standard
10 accessModes:
11 - ReadWriteOnce # type of access
12 capacity:
13 storage: 100Gi # Size of the volume
14 hostPath:
15 path: "/storage/volumes/test-volume"
Next you must create a PersistentVolumeClaim (PVC) to request access to the resources of the PersistentVolume (PV).
1apiVersion: v1
2kind: PersistentVolumeClaim
3metadata:
4 name: persistent-test-volume-claim
5spec:
6 volumeMode: Filesystem
7 storageClassName: standard
8 accessModes:
9 - ReadWriteOnce
10 resources:
11 requests:
12 storage: 100Gi
13 selector:
14 matchLabels:
15 name: persistent-test-volume
Now that we’ve set these two resources up, we can create a pod with a container that references the PVC we made above in the spec.volumes
1apiVersion: v1
2kind: Pod
3metadata:
4 name: pv-tester
5 namespace: default
6spec:
7 restartPolicy: Never
8 containers:
9 - name: pv-tester
10 image: busybox
11 command: ["/bin/sh", "-c", "echo 'Hello volume' > /test_vol/hello.txt"]
12 volumeMounts:
13 - name: vol
14 mountPath: /test_vol
15 volumes:
16 - name: vol
17 persistentVolumeClaim:
18 claimName: persistent-test-volume-claim
You now should be able to see the hello.txt
file at the path /storage/volumes/
on the host machine.