Create Longhorn Volumes

In this tutorial, you’ll learn how to create Kubernetes persistent storage resources of persistent volumes (PVs) and persistent volume claims (PVCs) that correspond to Longhorn volumes. You will use kubectl to dynamically provision storage for workloads using a Longhorn storage class. For help creating volumes from the Longhorn UI, refer to this section.

This section assumes that you understand how Kubernetes persistent storage works. For more information, see the Kubernetes documentation.

Creating Longhorn Volumes with kubectl

First, you will create a Longhorn StorageClass. The Longhorn StorageClass contains the parameters to provision PVs.

Next, a PersistentVolumeClaim is created that references the StorageClass. Finally, the PersistentVolumeClaim is mounted as a volume within a Pod.

When the Pod is deployed, the Kubernetes master will check the PersistentVolumeClaim to make sure the resource request can be fulfilled. If storage is available, the Kubernetes master will create the Longhorn volume and bind it to the Pod.

  1. Use following command to create a StorageClass called longhorn:

    1. kubectl create -f https://raw.githubusercontent.com/longhorn/longhorn/v1.7.2/examples/storageclass.yaml

    The following example StorageClass is created:

    1. kind: StorageClass
    2. apiVersion: storage.k8s.io/v1
    3. metadata:
    4. name: longhorn
    5. provisioner: driver.longhorn.io
    6. allowVolumeExpansion: true
    7. parameters:
    8. numberOfReplicas: "3"
    9. staleReplicaTimeout: "2880" # 48 hours in minutes
    10. fromBackup: ""
    11. fsType: "ext4"
    12. # mkfsParams: "-I 256 -b 4096 -O ^metadata_csum,^64bit"
    13. # diskSelector: "ssd,fast"
    14. # nodeSelector: "storage,fast"
    15. # recurringJobSelector: '[
    16. # {
    17. # "name":"snap",
    18. # "isGroup":true,
    19. # },
    20. # {
    21. # "name":"backup",
    22. # "isGroup":false,
    23. # }
    24. # ]'

    In particular, starting with v1.4.0, the parameter mkfsParams can be used to specify filesystem format options for each StorageClass.

  2. Create a Pod that uses Longhorn volumes by running this command:

    1. kubectl create -f https://raw.githubusercontent.com/longhorn/longhorn/v1.7.2/examples/pod_with_pvc.yaml

    A Pod named volume-test is launched, along with a PersistentVolumeClaim named longhorn-volv-pvc. The PersistentVolumeClaim references the Longhorn StorageClass:

    1. apiVersion: v1
    2. kind: PersistentVolumeClaim
    3. metadata:
    4. name: longhorn-volv-pvc
    5. spec:
    6. accessModes:
    7. - ReadWriteOnce
    8. storageClassName: longhorn
    9. resources:
    10. requests:
    11. storage: 2Gi

    The persistentVolumeClaim is mounted in the Pod as a volume:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: volume-test
    5. namespace: default
    6. spec:
    7. containers:
    8. - name: volume-test
    9. image: nginx:stable-alpine
    10. imagePullPolicy: IfNotPresent
    11. volumeMounts:
    12. - name: volv
    13. mountPath: /data
    14. ports:
    15. - containerPort: 80
    16. volumes:
    17. - name: volv
    18. persistentVolumeClaim:
    19. claimName: longhorn-volv-pvc

More examples are available here.

Binding Workloads to PVs without a Kubernetes StorageClass

It is possible to use a Longhorn StorageClass to bind a workload to a PV without creating a StorageClass object in Kubernetes.

Since the Storage Class is also a field used to match a PVC with a PV, which doesn’t have to be created by a Provisioner, you can create a PV manually with a custom StorageClass name, then create a PVC asking for the same StorageClass name.

When a PVC requests a StorageClass that does not exist as a Kubernetes resource, Kubernetes will try to bind your PVC to a PV with the same StorageClass name. The StorageClass will be used like a label to find the matching PV, and only existing PVs labeled with the StorageClass name will be used.

If the PVC names a StorageClass, Kubernetes will:

  1. Look for an existing PV that has the label matching the StorageClass
  2. Look for an existing StorageClass Kubernetes resource. If the StorageClass exists, it will be used to create a PV.

Creating Longhorn Volumes with the Longhorn UI

Since the Longhorn volume already exists while creating PV/PVC, a StorageClass is not needed for dynamically provisioning Longhorn volume. However, the field storageClassName should be set in PVC/PV, to be used for PVC bounding purpose. And it’s unnecessary for users to create the related StorageClass object.

By default the StorageClass for Longhorn created PV/PVC is longhorn-static. Users can modify it in Setting - General - Default Longhorn Static StorageClass Name as they need.

Users need to manually delete PVC and PV created by Longhorn.

PV/PVC Creation for Existing Longhorn Volume

Now users can create PV/PVC via our Longhorn UI for the existing Longhorn volumes. Only detached volume can be used by a newly created pod.

The Failure of the Longhorn Volume Creation

Creating a Longhorn volume will fail if there are no available nodes, disks, or insufficient storage. The failures are categorized into:

  • insufficient storage,
  • disk not found,
  • disks are unavailable,
  • failed to retrieve scheduling settings failed to retrieve,
  • tags not fulfilled,
  • node not found,
  • nodes are unavailable,
  • none of the node candidates contains a ready engine image,
  • hard affinity cannot be satisfied,
  • replica scheduling failed.

The failure results in the workload failing to use the provisioned PV and showing a warning message

  1. # kubectl describe pod workload-test
  2. Events:
  3. Type Reason Age From Message
  4. ---- ------ ---- ---- -------
  5. Warning FailedAttachVolume 14s (x8 over 82s) attachdetach-controller AttachVolume.Attach
  6. failed for volume "pvc-e130e369-274d-472d-98d1-f6074d2725e8" : rpc error: code = Aborted
  7. desc = volume pvc-e130e369-274d-472d-98d1-f6074d2725e8 is not ready for workloads

In order to help users understand the error causes, Longhorn summarizes them in the PV annotation, longhorn.io/volume-scheduling-error. Failures are combined in this annotation and separated by a semicolon, for example, longhorn.io/volume-scheduling-error: insufficient storage;disks are unavailable. The annotation can be checked by using kubectl describe pv <pvc name>.

  1. # kubectl describe pv pvc-e130e369-274d-472d-98d1-f6074d2725e8
  2. Name: pvc-e130e369-274d-472d-98d1-f6074d2725e8
  3. Labels: <none>
  4. Annotations: longhorn.io/volume-scheduling-error: insufficient storage
  5. pv.kubernetes.io/provisioned-by: driver.longhorn.io
  6. ...