<p>With the storage add-on for MicroK8s, Persistent Volume Claims are by default given storage under <code>/var/snap/microk8s/common/default-storage</code> on the host system.  How can that be changed?</p>

<p>Viewing the declaration for the <code>hostpath-provisioner</code> pod, shows that there is an environment setting called <code>PV_DIR</code> pointing to <code>/var/snap/microk8s/common/default-storage</code> - seems like what I'd like to change, but how can that be done?</p>

<p>Not sure if I'm asking a MicroK8s specific question or if this is something that applies to Kubernetes in general?</p>

<pre><code>$ microk8s.kubectl describe -n kube-system pod/hostpath-provisioner-7b9cb5cdb4-q5jh9

Name:         hostpath-provisioner-7b9cb5cdb4-q5jh9
Namespace:    kube-system
Priority:     0
Node:         ...
Start Time:   ...
Labels:       k8s-app=hostpath-provisioner
              pod-template-hash=7b9cb5cdb4
Annotations:  &lt;none&gt;
Status:       Running
IP:           ...
IPs:
  IP:           ...
Controlled By:  ReplicaSet/hostpath-provisioner-7b9cb5cdb4
Containers:
  hostpath-provisioner:
    Container ID:   containerd://0b74a5aa06bfed0a66dbbead6306a0bc0fd7e46ec312befb3d97da32ff50968a
    Image:          cdkbot/hostpath-provisioner-amd64:1.0.0
    Image ID:       docker.io/cdkbot/hostpath-provisioner-amd64@sha256:339f78eabc68ffb1656d584e41f121cb4d2b667565428c8dde836caf5b8a0228
    Port:           &lt;none&gt;
    Host Port:      &lt;none&gt;
    State:          Running
      Started:      ...
    Last State:     Terminated
      Reason:       Unknown
      Exit Code:    255
      Started:      ...
      Finished:     ...
    Ready:          True
    Restart Count:  3
    Environment:
      NODE_NAME:   (v1:spec.nodeName)
      PV_DIR:     /var/snap/microk8s/common/default-storage
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from microk8s-hostpath-token-nsxbp (ro)
      /var/snap/microk8s/common/default-storage from pv-volume (rw)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  pv-volume:
    Type:          HostPath (bare host directory volume)
    Path:          /var/snap/microk8s/common/default-storage
    HostPathType:  
  microk8s-hostpath-token-nsxbp:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  microk8s-hostpath-token-nsxbp
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  &lt;none&gt;
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          &lt;none&gt;
</code></pre>

<h2>HostPath</h2>

<p>If You want to add your own path to your persistentVolume You can use <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#create-a-persistentvolume" rel="noreferrer">spec.hostPath.path</a> value </p>

<p>example yamls</p>

<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: base                 
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: Immediate
</code></pre>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: base
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

</code></pre>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: base
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
</code></pre>

<p><strong>kindly reminder</strong></p>

<blockquote>
  <p>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 <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class-1" rel="noreferrer">PersistentVolumeClaim documentation</a> for details.</p>
</blockquote>

<p>You can check your storageclass by using </p>

<pre><code>kubectl get storageclass
</code></pre>

<p>If there is no <code>&lt;your-class-name&gt;(default)</code> that means You need to make your own default storage class.</p>

<p>Mark a StorageClass as default:</p>

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

<p>After You make defualt <a href="https://kubernetes.io/docs/concepts/storage/storage-classes/" rel="noreferrer">storageClass</a> You can use those yamls to create pv and pvc</p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume3
  labels:
    type: local
spec:
  storageClassName: ""
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data2"
</code></pre>

<pre><code>
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim3
spec:
  storageClassName: ""
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

</code></pre>

<h2>one pv for each pvc</h2>

<p>Based on <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#binding" rel="noreferrer">kubernetes documentation</a></p>

<blockquote>
  <p>Once bound, PersistentVolumeClaim binds are exclusive, regardless of how they were bound. <strong>A PVC to PV binding is a one-to-one mapping</strong>.</p>
</blockquote>
