How to change a Kubernetes hostpath-provisioner mount path? How to change a Kubernetes hostpath-provisioner mount path? kubernetes kubernetes

How to change a Kubernetes hostpath-provisioner mount path?


HostPath

If You want to add your own path to your persistentVolume You can use spec.hostPath.path value

example yamls

apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:  name: base                 provisioner: kubernetes.io/no-provisionervolumeBindingMode: Immediate
apiVersion: v1kind: PersistentVolumemetadata:  name: task-pv-volume  labels:    type: localspec:  storageClassName: base  capacity:    storage: 10Gi  accessModes:    - ReadWriteOnce  hostPath:    path: "/mnt/data"
apiVersion: v1kind: PersistentVolumeClaimmetadata:  name: task-pv-claimspec:  storageClassName: base  accessModes:    - ReadWriteOnce  resources:    requests:      storage: 3Gi

kindly reminder

Depending on the installation method, your Kubernetes cluster may be deployed with an existing StorageClass that is marked as default. This default StorageClass is then used to dynamically provision storage for PersistentVolumeClaims that do not require any specific storage class. See PersistentVolumeClaim documentation for details.

You can check your storageclass by using

kubectl get storageclass

If there is no <your-class-name>(default) that means You need to make your own default storage class.

Mark a StorageClass as default:

kubectl patch storageclass <your-class-name> -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

After You make defualt storageClass You can use those yamls to create pv and pvc

apiVersion: v1kind: PersistentVolumemetadata:  name: task-pv-volume3  labels:    type: localspec:  storageClassName: ""  capacity:    storage: 10Gi  accessModes:    - ReadWriteOnce  hostPath:    path: "/mnt/data2"
apiVersion: v1kind: PersistentVolumeClaimmetadata:  name: task-pv-claim3spec:  storageClassName: ""  accessModes:    - ReadWriteOnce  resources:    requests:      storage: 3Gi

one pv for each pvc

Based on kubernetes documentation

Once bound, PersistentVolumeClaim binds are exclusive, regardless of how they were bound. A PVC to PV binding is a one-to-one mapping.