Docker Certified Associate Exam Course

Kubernetes

Storage Classes

In this lesson, we’ll explore how StorageClasses enable dynamic volume provisioning in Kubernetes. If you’re familiar with static provisioning—where you manually create cloud disks and bind them via PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs)—you’ll see how StorageClasses automate this workflow.


Table of Contents


Static Provisioning Recap

With static provisioning, you manually create the underlying cloud disk before you define a PV:

gcloud beta compute disks create \
  --size=1GB \
  --region=us-east1 \
  pd-disk

Then you define:

# pv-definition.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-vol1
spec:
  capacity:
    storage: 500Mi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: pd-disk
    fsType: ext4
# pvc-definition.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myclaim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
# pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
  name: random-number-generator
spec:
  containers:
    - name: alpine
      image: alpine
      command: ["/bin/sh", "-c"]
      args:
        - shuf -i 0-100 -n 1 >> /opt/number.out
      volumeMounts:
        - name: data-volume
          mountPath: /opt
  volumes:
    - name: data-volume
      persistentVolumeClaim:
        claimName: myclaim

Note

Static provisioning works, but each new disk requires manual cloud CLI steps. Maintenance and scaling can become cumbersome.


Dynamic Provisioning with StorageClass

With a StorageClass, Kubernetes can automatically create and bind a PV when you apply a PVC. This dynamic provisioning eliminates manual disk creation.

StorageClass Definition

Define a StorageClass that uses the GCE PD provisioner:

# sc-definition.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: google-storage
provisioner: kubernetes.io/gce-pd

Using a Dynamic PVC and Pod

  1. Create a PVC that references your StorageClass:
    # pvc-dynamic.yaml
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: myclaim
    spec:
      storageClassName: google-storage
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 500Mi
    
  2. Deploy a Pod that mounts the PVC:
    # pod-dynamic.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: random-number-generator
    spec:
      containers:
        - name: alpine
          image: alpine
          command: ["/bin/sh", "-c"]
          args:
            - shuf -i 0-100 -n 1 >> /opt/output.txt
          volumeMounts:
            - name: data-volume
              mountPath: /opt
      volumes:
        - name: data-volume
          persistentVolumeClaim:
            claimName: myclaim
    

Kubernetes will then:

  1. Provision a new GCE PD of the requested size.
  2. Create a corresponding PV.
  3. Bind your PVC to that PV.
  4. Attach the volume to your Pod.

Comparing Provisioning Methods

FeatureStatic ProvisioningDynamic Provisioning (StorageClass)
Disk CreationManual via cloud CLIAutomated by Kubernetes
PV DefinitionPre-created PersistentVolumeGenerated automatically
PVC BindingManual or automatic if labels matchAutomatic
ScalabilityLimited (manual work for each disk)High (one PVC, one step)
FlexibilityLowHigh (parameters, tiers, provisioners)

Customizing StorageClass Parameters

Most provisioners let you fine-tune disks. For GCE PD, you can specify:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: google-storage
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-standard        # pd-standard or pd-ssd
  replication-type: none   # none or regional-pd

Warning

Be cautious with regional-pd: it offers high availability at higher cost. Always choose based on your SLA requirements.


Defining Service Tiers

You can create multiple StorageClasses to represent different performance tiers:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: silver
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-standard
  replication-type: none
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gold
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-ssd
  replication-type: none
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: platinum
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-ssd
  replication-type: regional-pd
TierDisk TypeReplicationUse Case
silverpd-standardnoneDevelopment, testing
goldpd-ssdnoneProduction, low-latency
platinumpd-ssdregional-pdMission-critical, high-availability

When you create a PVC, just set storageClassName to select your tier. Kubernetes handles provisioning and binding behind the scenes.



Mastering StorageClasses empowers you to build scalable, self-service storage in your Kubernetes clusters—no more manual volume management!

Watch Video

Watch video content

Previous
Persistent Volume Claims