Create Kubernetes Persistent Volume

4/10/2021
Ondat
demo

Kubernetes provides a Persistent Volume subsystem that abstracts details of how storage is provided from how it is consumed. You still need to provide the underlying storage system.

Ondat provides cloud native storage for Kubernetes used for provisioning Persistent Volumes. Ondat is free to use up to 500 GB of storage. Get started by reading our self-evaluation guide and installing in four steps:

  • Install etcd
  • Install StorageOS Operator
  • Create a Kubernetes secret detailing the default StorageOS administrator account
  • Install StorageOS using a StorageOSCluster Custom Resource

Ondat Kubernetes Persistent Volume Guide

As a simple first use of Ondat with Kubernetes following the example below will create a PersistentVolumeClaim (PVC) and schedule a Pod to mount the PersistentVolume (PV) provisioned by the PVC. You can read more details about Ondat and Kubernetes best practices at our docs.

Creating the PersistentVolumeClaim

  1. You can find the latest files in the StorageOS example deployment repository
     git clone https://github.com/storageos/deploy.git storageos


    PVC definition

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: my-vol-1
    spec:
      storageClassName: "fast"
      accessModes:
     - ReadWriteOnce
      resources:
     requests:
       storage: 5Gi
    


    The above PVC will dynamically provision a 5GB volume using the fast StorageClass. This StorageClass was created during the StorageOS install and causes StorageOS to provision a PersistentVolume.

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: my-vol-1
      labels:
       storageos.com/replicas: "1"
    spec:
      storageClassName: "fast"
      accessModes:
     - ReadWriteOnce
      resources:
     requests:
       storage: 5Gi
    


    The above PVC has the storageos.com/replicas label set. This label tells StorageOS to create a replica for the volume that is created. For the sake of keeping this example simple the unreplicated volume will be used.

  2. Move into the examples folder and create a PVC using the PVC definition above.
    $ cd storageos
    $ kubectl create -f ./k8s/examples/pvc.yaml
    


    You can view the PVC that you have created with the command below

    $ kubectl get pvc
    NAME         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
    my-vol-1     Bound    pvc-f8ffa027-e821-11e8-bc0b-0ac77ccc61fa   5Gi        RWO            fast           1m
    
  3. Create a pod that mounts the PVC created in step 2.
     $ kubectl create -f ./k8s/examples/debian-pvc.yaml
    


    The command above creates a Pod that uses the PVC that was created in step 1.

     apiVersion: v1
     kind: Pod
     metadata:
       name: d1
     spec:
       containers:
         - name: debian
           image: debian:9-slim
           command: ["/bin/sleep"]
           args: [ "3600" ]
           volumeMounts:
             - mountPath: /mnt
               name: v1
       volumes:
         - name: v1
           persistentVolumeClaim:
             claimName: my-vol-1
    


    In the Pod definition above the volume v1, which references the PVC created in step 2, is mounted in the pod at /mnt. In this example, a debian image is used for the container but any container image with a shell would work for this example.

  4. Confirm that the pod is up and running
     $ kubectl get pods
     NAME      READY   STATUS    RESTARTS   AGE
     d1        1/1     Running   0          1m
    
  5. Execute a shell inside the container and write some contents to a file
     $ kubectl exec -it d1 -- bash 
     [email protected]:/# echo "Hello World!" > /mnt/helloworld
     [email protected]:/# cat /mnt/helloworld
     Hello World!
    


    By writing to /mnt inside the container, the StorageOS volume created by the PVC is being written to. If you were to kill the pod and start it again on a new node, the helloworld file would still be available.