Selector-Label Volume Binding
Overview
This guide provides the steps necessary to enable binding of persistent volume claims (PVCs) to persistent volumes (PVs) via selector and label attributes. By implementing selectors and labels, regular users are able to target provisioned storage by identifiers defined by a cluster administrator.
Motivation
In cases of statically provisioned storage, developers seeking persistent storage are required to know a handful of identifying attributes of a PV in order to deploy and bind a PVC. This creates several problematic situations. Regular users might have to contact a cluster administrator to either deploy the PVC or provide the PV values. PV attributes alone do not convey the intended use of the storage volumes, nor do they provide methods by which volumes can be grouped.
Selector and label attributes can be used to abstract away PV details from the user while providing cluster administrators with a way of identifying volumes by a descriptive and customizable tag. Through the selector-label method of binding, users are only required to know which labels are defined by the administrator.
The selector-label feature is currently only available for statically provisioned storage and is currently not implemented for storage provisioned dynamically. |
Deployment
This section reviews how to define and deploy PVCs.
Prerequisites
A running OKD 3.3+ cluster
A volume provided by a supported storage provider
A user with a cluster-admin role binding
Define the Persistent Volume and Claim
As the cluster-admin user, define the PV. For this example, we will be using a GlusterFS volume. See the appropriate storage provider for your provider’s configuration.
Example 1. Persistent Volume with Labels
apiVersion: v1
kind: PersistentVolume
metadata:
name: gluster-volume
labels: (1)
volume-type: ssd
aws-availability-zone: us-east-1
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
glusterfs:
endpoints: glusterfs-cluster
path: myVol1
readOnly: false
persistentVolumeReclaimPolicy: Retain
1 A PVC whose selectors match all of a PV’s labels will be bound, assuming a PV is available. Define the PVC:
Example 2. Persistent Volume Claim with Selectors
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: gluster-claim
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
selector: (1)
matchLabels: (2)
volume-type: ssd
aws-availability-zone: us-east-1
1 Begin selectors section. 2 List all labels by which the user is requesting storage. Must match all labels of targeted PV.
Optional: Bind a PVC to a specific PV
A PVC that does not specify a PV name or selector will match any PV.
To bind a PVC to a specific PV as a cluster administrator:
Use
pvc.spec.volumeName
if you know the PV name.Use
pvc.spec.selector
if you know the PV labels.By specifying a selector, the PVC requires the PV to have specific labels.
Optional: Reserve a PV to a specific PVC
To reserve a PV for specific tasks, you have two options: create a specific storage class, or pre-bind the PV to your PVC.
Request a specific storage class for the PV by specifying the storage class’s name.
The following resource shows the required values that you use to configure a StorageClass. This example uses the AWS ElasticBlockStore (EBS) object definition.
Example 3. StorageClass definition for EBS
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: kafka
provisioner: kubernetes.io/aws-ebs
...
If necessary in a multi-tenant environment, use a quota definition to reserve the storage class and PV(s) only to a specific namespace.
Pre-bind the PV to your PVC using the PVC namespace and name. A PV defined as such will bind only to the specified PVC and to nothing else, as shown in the following example:
Example 4. claimRef in PV definition
apiVersion: v1
kind: PersistentVolume
metadata:
name: mktg-ops--kafka--kafka-broker01
spec:
capacity:
storage: 15Gi
accessModes:
- ReadWriteOnce
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: kafka-broker01
namespace: default
...
Deploy the Persistent Volume and Claim
As the cluster-admin user, create the persistent volume:
Example 5. Create the Persistent Volume
# oc create -f gluster-pv.yaml
persistentVolume "gluster-volume" created
# oc get pv
NAME LABELS CAPACITY ACCESSMODES STATUS CLAIM REASON AGE
gluster-volume map[] 2147483648 RWX Available 2s
Once the PV is created, any user whose selectors match all its labels can create their PVC.
Example 6. Create the Persistent Volume Claim
# oc create -f gluster-pvc.yaml
persistentVolumeClaim "gluster-claim" created
# oc get pvc
NAME LABELS STATUS VOLUME
gluster-claim Bound gluster-volume