taken from https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/

Create yaml file for the persistent volume(PV).

  • storageClassName = a name you give to create a storage class a persistent volume claim(PVC) will use
  • accesModes ReadWriteOnce = only one pod can read and write to it
  • hostPath = location of the data on the local machine that will be mounted to the pod
apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
kubectl apply -f name-of-yaml-file.yaml

Create a yaml file for persistent volume claim(PVC) that will be used by the pod

  • storageClassName = the storage class of the desired persistent volume
  • accessModes = the access mode of the desired persistent volume
  • storage = the size needed
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
kubectl apply -f name-of-yaml-file.yaml

Kubernetes control plane will find a persistent volume to match the persistent volume claim base on the storage class, size , access mode …

kubectl get pv task-pv-volume

This will show the persistent volume was claimed by the persistent volume claim

NAME             CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM                   STORAGECLASS   REASON    AGE
task-pv-volume   10Gi       RWO           Retain          Bound     default/task-pv-claim   manual                   2m

Create yaml file for the pod using the persistent volume claim

  • claimName = the given name of the persistent volume claim
  • volumes . name = the name given for the volume to be used for the volume mount
  • image = the image used for the pod
  • containerPort = the port the container listens on
  • volumeMounts . mountPath = the path in the container to mount the volume
apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage
kubectl apply -f name-of-yaml-file.yaml