How to change a Kubernetes hostpath-provisioner mount path?

前端 未结 1 863
野的像风
野的像风 2021-02-15 10:25

With the storage add-on for MicroK8s, Persistent Volume Claims are by default given storage under /var/snap/microk8s/common/default-storage on the host system. How

1条回答
  •  囚心锁ツ
    2021-02-15 10:50

    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/v1
    kind: StorageClass
    metadata:
      name: base                 
    provisioner: kubernetes.io/no-provisioner
    volumeBindingMode: Immediate
    
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: task-pv-volume
      labels:
        type: local
    spec:
      storageClassName: base
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: "/mnt/data"
    
    
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: task-pv-claim
    spec:
      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 (default) that means You need to make your own default storage class.

    Mark a StorageClass as default:

    kubectl patch storageclass  -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: v1
    kind: PersistentVolume
    metadata:
      name: task-pv-volume3
      labels:
        type: local
    spec:
      storageClassName: ""
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: "/mnt/data2"
    
    
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: task-pv-claim3
    spec:
      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.

    0 讨论(0)
提交回复
热议问题