Can I combine StorageClass with PersistentVolume in GKE?

10/30/2019

I'm fairly new to Kubernetes and find it difficult to get it working from documentation, Kubenetes docs says that StorageClass contains the fields provisioner, parameters, and reclaimPolicy, which are used when a PersistentVolume belonging to the class needs to be dynamically provisioned however can I use StorageClass with PV(not dynamic allocation) to specify high performance disk allocation such as ssd?

without StorageClass it worked fine for me.

following is my manifest

kind: PersistentVolume
metadata:
  name: gke-pv
  labels:
    app: test
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: gce-disk
    fsType: ext4
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gke-pvc
  labels:
    app: test
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ssd-sc 
  resources:
    requests:
      storage: 2Gi
  selector:
    matchLabels:
      app: test
-- epsan
google-cloud-platform
google-kubernetes-engine
kubernetes
storage

2 Answers

10/30/2019

You need storage class if the storage needs to be provisioned dynamically.

If you are provisioning persistent volumes then it is called static storage provisioning. You don't need storage class in this scenario

-- P Ekambaram
Source: StackOverflow

10/30/2019

The problem that is going on here is that if you want to statically provision PersistentVolumes, they don't have a StorageClass. However, GKE clusters are created with a standard StorageClass which is the default, and so the PVC gets confused and tries to dynamically allocate.

The solution is to have the PVC request an empty storage class, which forces it to look at the statically provisioned PVs.

So you'd use a sequence like this to create a PV and then get it bound to a PVC:

  • Manually provision the ssd:

gcloud compute disks create --size=10Gi --zone=[YOUR ZONE] --type=pd-ssd already-created-ssd-disk

  • Then apply a PV object that uses the statically provisioned disk, like so:
apiVersion: v1
kind: PersistentVolume
metadata:
  name: ssd-for-k8s-volume
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: already-created-ssd-disk
    fsType: ext4
  • Then, you can claim it with a PVC like this:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-ssd-demo
spec:
  storageClassName: ""
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

You could also use labels to refine which PVs are selected, of course, for example if you have some that are SSD and others that are regular spinning metal.

Note that the idea of using a StorageClass for static provisioning isn't really the right thing, since StorageClass is tied to how you describe storage for dynamic provisioning.

-- robsiemb
Source: StackOverflow