I do lot of Kubernetes related work either on minikube or local OpenShift cluster setup in a VM. Often I need to create a PersistentVolumeClaim a.k.a. pvc. But to use pvc you have to have a PersistentVolume or pv defined.

Enter into the machine running k8s

If using minikube you can do

minikube ssh

Create a local directory for storage

mkdir /tmp/pv0001
chmod 777 /tmp/pv0001

If you are on a machine that has SELinux enabled do the following

sudo chcon -R -t svirt_sandbox_file_t /tmp/pv0001

Creating pv

Create file with following content

$ cat pv.yaml
apiVersion: "v1"
kind: "PersistentVolume"
metadata:
  name: "pv0001"
spec:
  capacity:
    storage: "5Gi"
  accessModes:
    - "ReadWriteOnce"
  persistentVolumeReclaimPolicy: Recycle
  hostPath:
    path: /tmp/pv0001

Get to the terminal from where you can run kubectl commands.

kubectl create -f pv.yaml

If you are doing it for OpenShift cluster then run following command with privileged access.

oc create -f pv.yaml

There you have a pv now you can create pvc’s to use it.