# Addon: Hostpath Storage

**URL:** <https://discuss.kubernetes.io/t/addon-hostpath-storage/20878>\
**Category:** microk8s\
**Tags:** docs\
**Created:** [August 6, 2022, 2:28am UTC](https://discuss.kubernetes.io/t/addon-hostpath-storage/20878 "2022-08-06T02:28:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![balchua1](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/balchua1/32/5372_2.png) [@balchua1](https://discuss.kubernetes.io/u/balchua1)\
**Post date:** [August 6, 2022, 2:28am UTC](https://discuss.kubernetes.io/t/addon-hostpath-storage/20878/1 "2022-08-06T02:28:48Z")

</div>

The hostpath storage MicroK8s add-on can be used to easily provision PersistentVolumes backed by a host directory. It is ideal for local development, but for all uses it is important to be aware:

- PersistentVolumeClaims created by the hostpath storage provisioner are bound to the local node, so it is _impossible to move them to a different node_.
- A hostpath volume can _grow beyond the capacity set in the volume claim manifest_.

## Enable

```no-highlight
microk8s enable hostpath-storage

```

## Verify

Create an example pod with a PVC, using the `microk8s-hostpath` storage class. Note that the `microk8s-hostpath` storage class is marked as default, so you do not have to specify a `storageClassName` for the PVC definition:

```yaml
microk8s kubectl apply -f - <<EOF
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-pvc
spec:
  accessModes: [ReadWriteOnce]
  resources: { requests: { storage: 1Gi } }
---
apiVersion: v1
kind: Pod
metadata:
  name: test-nginx
spec:
  volumes:
    - name: pvc
      persistentVolumeClaim:
        claimName: test-pvc
  containers:
    - name: nginx
      image: nginx
      ports:
        - containerPort: 80
      volumeMounts:
        - name: pvc
          mountPath: /usr/share/nginx/html
EOF

```

Then use `microk8s kubectl get pod,pvc` to verify that the volume and the container were successfully created:

```auto
NAME READY STATUS RESTARTS AGE
pod/test-nginx 1/1 Running 0 32s

NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/test-pvc Bound pvc-c48dbcc1-ca7e-482d-954f-b9e80119e438 1Gi RWO microk8s-hostpath 52s

```

You can also use `microk8s kubectl describe pv` to see more details about the volume that was provisioned. Two fields of note are `Node Affinity` (which mentions the node that the provisioned volume is bound to) and `Source` (which mentions the local directory where the volume data is stored).

```bash
Name: pvc-c48dbcc1-ca7e-482d-954f-b9e80119e438
Labels: <none>
Annotations: hostPathProvisionerIdentity: u2
                   pv.kubernetes.io/provisioned-by: microk8s.io/hostpath
Finalizers: [kubernetes.io/pv-protection]
StorageClass: microk8s-hostpath
Status: Bound
Claim: default/test-pvc
Reclaim Policy: Delete
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 1Gi
Node Affinity:
  Required Terms:
    Term 0: kubernetes.io/hostname in [u2]
Message:
Source:
    Type: HostPath (bare host directory volume)
    Path: /var/snap/microk8s/common/default-storage/default-test-pvc-pvc-c48dbcc1-ca7e-482d-954f-b9e80119e438
    HostPathType: DirectoryOrCreate

```

## Customize directory used for PersistentVolume

> _NOTE_: This feature is available starting from MicroK8s 1.25

By default, the hostpath provisioner will store all volume data under `/var/snap/microk8s/common/default-storage`. It is possible that a cluster administrator might want to change this path, or support different paths (e.g. so that they specify a path backed by SSD storage).

This is possible via the use of custom storage classes, by specifying the `pvDir` parameter. For example, to create an `ssd-hostpath` storage class that stores data under `/mnt/ssd-disk`:

1. Create the storage class definition like so:

2. Apply the storage class on the cluster:

3. Ensure that the storage class was created successfully with `microk8s kubectl get storageclass ssd-hostpath`:

4. Create an example PVC using the `ssd-hostpath` storageclass:

5. Ensure that the volume data is stored under `/mnt/ssd-disk` with `microk8s kubectl describe pv`. The output should look like this:

---

<div class="post-metadata">

**Author:** ![balchua1](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/balchua1/32/5372_2.png) [@balchua1](https://discuss.kubernetes.io/u/balchua1)\
**Post date:** [August 6, 2022, 2:31am UTC](https://discuss.kubernetes.io/t/addon-hostpath-storage/20878/2 "2022-08-06T02:31:59Z")

</div>

@evilnick  
The `hostpath-storage` addon will have new options that we think will be nice to have it documented.  
Before linking this to the main Addon page, I want to run this first with you.

As far as I know this may come together with the release of `1.25` @neoaggelos , you can help verify this one.  
Thanks!

---

<div class="post-metadata">

**Author:** ![evilnick](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/evilnick/32/3481_2.png) [@evilnick](https://discuss.kubernetes.io/u/evilnick)\
**Post date:** [August 22, 2022, 2:14pm UTC](https://discuss.kubernetes.io/t/addon-hostpath-storage/20878/3 "2022-08-22T14:14:47Z")

</div>

thanks and done!
