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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
apiVersion: v1
kind: PersistentVolume
metadata:
  name: persistent-test-volume
  labels:
    name: persistent-test-volume
spec:
  volumeMode: Filesystem
  storageClassName: standard
  accessModes:
    - ReadWriteOnce # type of access
  capacity:
    storage: 100Gi # Size of the volume
  hostPath:
    path: "/storage/volumes/test-volume"

Next you must create a PersistentVolumeClaim (PVC) to request access to the resources of the PersistentVolume (PV).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: persistent-test-volume-claim
spec:
  volumeMode: Filesystem
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
  selector:
    matchLabels:
      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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: Pod
metadata:
  name: pv-tester
  namespace: default
spec:
  restartPolicy: Never
  containers:
    - name: pv-tester
      image: busybox
      command: ["/bin/sh", "-c", "echo 'Hello volume' > /test_vol/hello.txt"]
      volumeMounts:
        - name: vol
          mountPath: /test_vol
  volumes:
    - name: vol
      persistentVolumeClaim:
        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.