In this example, a cluster-admin
or storage-admin
enables a default
storage class for all other users and projects that do not implicitly specify a
StorageClass annotation in their claim. This is useful for a cluster-admin
or storage-admin
to provide easy management of a storage volume without having
to set up or communicate specialized StorageClasses across the cluster.
Example 3. Default StorageClass Object Definition
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
name: generic (1)
annotations:
storageclass.beta.kubernetes.io/is-default-class: "true" (2)
provisioner: kubernetes.io/gce-pd
parameters:
type: pd-standard
zone: us-east1-d
1 |
Name of the StorageClass, which needs to be unique in the cluster. |
2 |
Annotation that marks this StorageClass as the default class. You must
use "true" quoted in this version of the API. Without this
annotation, OpenShift Origin considers this not the default StorageClass. |
As a cluster-admin
or storage-admin
save the definition to a YAML file
(generic-gce.yaml
), then create the StorageClasses:
# oc create -f generic-gce.yaml
storageclass "generic" created
# oc get storageclass
NAME TYPE
generic kubernetes.io/gce-pd
fast kubernetes.io/gce-pd
slow kubernetes.io/gce-pd
As a regular user, create a new claim definition without any StorageClass
annotation and save it to a file (generic-pvc.yaml
).
Example 4. default Storage Claim Object Definition
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-engineering2
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
Execute it and check the claim is bound:
# oc create -f generic-pvc.yaml
persistentvolumeclaim "pvc-engineering2" created
3s
# oc get pvc
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
pvc-engineering Bound pvc-e9b4fef7-8bf7-11e6-9962-42010af00004 10Gi RWX 41m
pvc-engineering2 Bound pvc-a9f70544-8bfd-11e6-9962-42010af00004 5Gi RWX 7s (1)
1 |
pvc-engineering2 is bound to a dynamically provisioned Volume by default. |
As a cluster-admin
or storage-admin
, view the Persistent Volumes defined so
far:
# oc get pv
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE
pvc-a9f70544-8bfd-11e6-9962-42010af00004 5Gi RWX Delete Bound rh-eng/pvc-engineering2 5m (1)
pvc-ba4612ce-8b4d-11e6-9962-42010af00004 5Gi RWO Delete Bound mytest/gce-dyn-claim1 21h
pvc-e9b4fef7-8bf7-11e6-9962-42010af00004 10Gi RWX Delete Bound rh-eng/pvc-engineering 46m (2)
Create a manually provisioned disk using
GCE (not dynamically
provisioned). Then create a Persistent Volume that connects to the new GCE disk (pv-manual-gce.yaml
).
Example 5. Manual PV Object Defition
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-manual-gce
spec:
capacity:
storage: 35Gi
accessModes:
- ReadWriteMany
gcePersistentDisk:
readOnly: false
pdName: the-newly-created-gce-PD
fsType: ext4
Execute the object definition file:
# oc create -f pv-manual-gce.yaml
Now view the PVs again. Notice that a pv-manual-gce
volume is Available.
# oc get pv
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE
pv-manual-gce 35Gi RWX Retain Available 4s
pvc-a9f70544-8bfd-11e6-9962-42010af00004 5Gi RWX Delete Bound rh-eng/pvc-engineering2 12m
pvc-ba4612ce-8b4d-11e6-9962-42010af00004 5Gi RWO Delete Bound mytest/gce-dyn-claim1 21h
pvc-e9b4fef7-8bf7-11e6-9962-42010af00004 10Gi RWX Delete Bound rh-eng/pvc-engineering 53m
Now create another claim identical to the generic-pvc.yaml
PVC definition but
change the name and do not set an annotation.
Example 6. Claim Object Definition
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-engineering3
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 15Gi
Because default StorageClass is enabled in this instance, the manually
created PV does not satisfy the claim request. The user receives a new
dynamically provisioned Persistent Volume.
# oc get pvc
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
pvc-engineering Bound pvc-e9b4fef7-8bf7-11e6-9962-42010af00004 10Gi RWX 1h
pvc-engineering2 Bound pvc-a9f70544-8bfd-11e6-9962-42010af00004 5Gi RWX 19m
pvc-engineering3 Bound pvc-6fa8e73b-8c00-11e6-9962-42010af00004 15Gi RWX 6s
Since the default StorageClass is enabled on this system, you would need to
create the PV in the default StorageClass for the manually created Persistent
Volume to get bound to the above claim and not have a new dynamic provisioned
volume bound to the claim.
To fix this, the cluster-admin
or storage-admin
user simply needs to create
another GCE disk or delete the first manual PV and use a PV object definition
that assigns a StorageClass annotation (pv-manual-gce2.yaml
)
if necessary:
Example 7. Manual PV Spec with default StorageClass annotation
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-manual-gce2
annotations:
volume.beta.kubernetes.io/storage-class: generic (1)
spec:
capacity:
storage: 35Gi
accessModes:
- ReadWriteMany
gcePersistentDisk:
readOnly: false
pdName: the-newly-created-gce-PD
fsType: ext4
1 |
The annotation for previously created generic StorageClass. |
Execute the object definition file:
# oc create -f pv-manual-gce2.yaml
# oc get pv
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE
pv-manual-gce 35Gi RWX Retain Available 4s (1)
pv-manual-gce2 35Gi RWX Retain Bound rh-eng/pvc-engineering3 4s (2)
pvc-a9f70544-8bfd-11e6-9962-42010af00004 5Gi RWX Delete Bound rh-eng/pvc-engineering2 12m
pvc-ba4612ce-8b4d-11e6-9962-42010af00004 5Gi RWO Delete Bound mytest/gce-dyn-claim1 21h
pvc-e9b4fef7-8bf7-11e6-9962-42010af00004 10Gi RWX Delete Bound rh-eng/pvc-engineering 53m
1 |
The original manual PV, still unbound and Available. This is because it was not created in the default StorageClass. |
2 |
The second PVC (other than the name) is bound to the Available manually created PV pv-manual-gce2 . |
|
Notice that all dynamically provisioned volumes by default have a RECLAIMPOLICY of Delete. Once the PVC dynamically bound to the PV is deleted, the GCE volume is deleted and all data is lost. However, the manually created PV has a default RECLAIMPOLICY of Retain.
|