Working with Volumes in Microk8s

  • 2 minute read
  • 12 code samples in YAML and Shell
  • 1 image

Also available in Română

On this page

Introduction

Hello šŸ‘‹,

In this quick tutorial šŸ’» we’ll explore how to use Volumes and PersistentVolumes with hostpath storage in Microk8s.

We will deploy a simple nginx instance and mount a volume inside it that points to the ~/Downloads folder.

Let’s get started!

How-To

You can follow along if you want to setup a quick deployment with a host path volume.

Persistent Volume

Let’s start by creating a Persistent Volume resource:

Warning

This hostPath exposes a directory on the MicroK8s host to the pod. Use a disposable test directory instead of your personal Downloads folder, and check the pod’s write permissions before running workloads against it.

k apply -f persistent-volume.yaml
apiVersion: v1kind: PersistentVolumemetadata:  name: nuculabs-persistent-volume  labels:    app: nginx    owner: dnutiu    name: tutorial-volumespec:  storageClassName: microk8s-hostpath  # Exclusive to PVC  claimRef:    name: nuculabs-persistent-volume-claim    namespace: default  capacity:    storage: 1Gi  accessModes:    - ReadWriteMany  hostPath:    path: "/home/denis/Downloads"

After applying the file you should see:

āžœ  Downloads k apply -f persistent_volume.yml persistentvolume/nuculabs-persistent-volume createdāžœ  Downloads k get pvNAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                                      STORAGECLASS        REASON   AGEnuculabs-persistent-volume                 1Gi        RWX            Retain           Available   default/nuculabs-persistent-volume-claim   microk8s-hostpath            5s

The created persistent volume is exclusive to the PersistentVolumeClaim we’ll create next.

Persistent Volume Claim

To reserve the nuculabs-persistent-volume Persistent Volume we need to create a claim:

Note

The claim below requests 500Gi, but the PersistentVolume provides only 1Gi. Those sizes cannot bind as written; request at most the PV’s capacity.

k apply -f persistent-volume-claim.yaml
apiVersion: v1kind: PersistentVolumeClaimmetadata:  name: nuculabs-persistent-volume-claim  namespace: default  labels:    app: nginx    owner: dnutiu    name: tutorial-volumespec:  storageClassName: "microk8s-hostpath"  volumeName: nuculabs-persistent-volume  accessModes:    - ReadWriteMany  resources:    requests:      storage: 500Gi

We can ensure that the claim is bound to our PV by referencing the volumeName:

spec:  storageClassName: "microk8s-hostpath"  volumeName: nuculabs-persistent-volume

or by using the matchLabels selector:

spec:  storageClassName: "microk8s-hostpath"  selector:    matchLabels:      app: nginx      owner: dnutiu      name: tutorial-volume

To verify that the volume is bound run:

k get pvNAME  STATUS   CLAIM  nuculabs-persistent-volume ...        Bound    default/nuculabs-persistent-volume-claim 

It should list the volume name along with the Bound status.

If you’re following along, congrats for making it this far! 😁

hero

Deployment

Finally, let’s create our nginx deployment that contains the mounted volume.

k apply -f nginx-deployment.yaml
apiVersion: apps/v1kind: Deploymentmetadata:  name: nginx-deployment  labels:    app: nginxspec:  replicas: 1  selector:    matchLabels:      app: nginx  template:    metadata:      labels:        app: nginx    spec:      containers:      - name: nginx        image: nginx:1.14.2        ports:        - containerPort: 80        volumeMounts:          - mountPath: "/data"            name: data-volume      volumes:        - name: "data-volume"          persistentVolumeClaim:            claimName: nuculabs-persistent-volume-claim

We mount the volume to the nginx container with:

        volumeMounts:          - mountPath: "/data"            name: data-volume

Then we add volumes to the spec and map ā€œdata-volumeā€ to our PVC:

      volumes:        - name: "data-volume"          persistentVolumeClaim:            claimName: nuculabs-persistent-volume-claim

That’s it! šŸŽ‰

If you want to verify that the volume is mounted correctly you can SSH into the nginx pod and navigate to the /data directory.

Thanks for reading! šŸ“š