Skip to content

CKA: Storage

10% of the CKA exam, 3 competencies. Back to the CKA overview.

Each competency lists what to know and the official documentation to read, then a demo you can run. Demos run on the demo lab, a throwaway kind cluster on your own machine; set it up once before you start.

Implement storage classes and dynamic volume provisioning

Section titled “Implement storage classes and dynamic volume provisioning”

Know

  • A StorageClass names a provisioner (CSI driver), parameters, reclaimPolicy, and volumeBindingMode. A PVC that names it gets a PV created on demand.
  • WaitForFirstConsumer delays provisioning until a pod is scheduled, so zonal disks land in the right zone.

Read

Demo: Write your own StorageClass

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace cka-sc
    kubectl config set-context --current --namespace=cka-sc
  2. Read the default class, then create one that keeps the data after the claim is deleted.

    Terminal window
    kubectl get storageclass standard -o yaml | grep -E 'provisioner|reclaimPolicy|volumeBindingMode|is-default'
    kubectl apply -f - <<'EOF'
    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
    name: keep
    provisioner: rancher.io/local-path
    reclaimPolicy: Retain
    volumeBindingMode: WaitForFirstConsumer
    EOF
  3. Claim from it and start a pod so the volume is provisioned.

    Terminal window
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: kept
    spec:
    storageClassName: keep
    accessModes: ["ReadWriteOnce"]
    resources:
    requests:
    storage: 1Gi
    ---
    apiVersion: v1
    kind: Pod
    metadata:
    name: user
    spec:
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sleep", "3600"]
    volumeMounts:
    - name: v
    mountPath: /data
    volumes:
    - name: v
    persistentVolumeClaim:
    claimName: kept
    EOF
    kubectl wait --for=condition=Ready pod/user --timeout=120s
    kubectl get pvc kept
    kubectl get pv
  4. Delete the pod and claim, then look at the volume.

    Terminal window
    kubectl delete pod user && kubectl delete pvc kept
    kubectl get pv

What you should see: The PV was created for you by the provisioner and bound to the claim. After deleting the claim it stays, with status Released, because the class says Retain. With the default Delete it would be gone.

Clean up:

Terminal window
kubectl delete pv $(kubectl get pv -o jsonpath='{.items[?(@.spec.storageClassName=="keep")].metadata.name}')
kubectl delete storageclass keep
kubectl config set-context --current --namespace=default
kubectl delete namespace cka-sc

Practise

  • Mark a StorageClass as default with the storageclass.kubernetes.io/is-default-class annotation and create a PVC without storageClassName.

On this site

Configure volume types, access modes and reclaim policies

Section titled “Configure volume types, access modes and reclaim policies”

Know

  • Access modes: ReadWriteOnce (one node), ReadOnlyMany, ReadWriteMany, ReadWriteOncePod (one pod).
  • Reclaim policies: Delete removes the backing storage with the PV, Retain keeps it for manual recovery.

Read

Demo: Three volume types in one pod

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace cka-voltypes
    kubectl config set-context --current --namespace=cka-voltypes
  2. Mount an emptyDir, a ConfigMap, and a projected service account token in one pod.

    Terminal window
    kubectl create configmap settings --from-literal=greeting=hello
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
    name: volumes
    spec:
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sh", "-c", "echo scratch > /scratch/f; ls /scratch /config /token; cat /config/greeting; sleep 3600"]
    volumeMounts:
    - {name: scratch, mountPath: /scratch}
    - {name: config, mountPath: /config, readOnly: true}
    - {name: token, mountPath: /token, readOnly: true}
    volumes:
    - name: scratch
    emptyDir:
    sizeLimit: 64Mi
    - name: config
    configMap:
    name: settings
    - name: token
    projected:
    sources:
    - serviceAccountToken:
    path: token
    expirationSeconds: 3600
    audience: demo
    EOF
    kubectl wait --for=condition=Ready pod/volumes --timeout=120s && kubectl logs volumes
  3. Ask for an access mode the provisioner can’t give.

    Terminal window
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: shared
    spec:
    accessModes: ["ReadWriteMany"]
    resources:
    requests:
    storage: 1Gi
    EOF
    kubectl run mounter --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 --overrides='{"spec":{"containers":[{"name":"m","image":"registry.k8s.io/e2e-test-images/busybox:1.37.0-2","command":["sleep","3600"],"volumeMounts":[{"name":"v","mountPath":"/v"}]}],"volumes":[{"name":"v","persistentVolumeClaim":{"claimName":"shared"}}]}}'
    sleep 20; kubectl describe pvc shared | sed -n '/Events:/,$p'

What you should see: The first pod lists all three mounts and prints hello. The RWX claim never binds: the local-path provisioner only supports ReadWriteOnce (and ReadWriteOncePod), and the claim’s events say so. Access modes are a promise the storage backend must be able to keep.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace cka-voltypes

Practise

  • Create a hostPath PV and a PVC that binds to it by size and access mode.

On this site

Manage persistent volumes and persistent volume claims

Section titled “Manage persistent volumes and persistent volume claims”

Know

  • PV and PVC bind one-to-one. Expanding a PVC requires allowVolumeExpansion: true on its StorageClass.
  • A PVC in use is protected from deletion by a finalizer until no pod uses it.

Read

Demo: Bind a claim to a volume you created by hand

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace cka-pv
    kubectl config set-context --current --namespace=cka-pv
  2. Create a static PV on one node with a hostPath, and a claim that selects it.

    Terminal window
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: PersistentVolume
    metadata:
    name: manual-pv
    labels:
    tier: manual
    spec:
    capacity:
    storage: 1Gi
    accessModes: ["ReadWriteOnce"]
    persistentVolumeReclaimPolicy: Retain
    storageClassName: ""
    hostPath:
    path: /tmp/manual-pv
    type: DirectoryOrCreate
    nodeAffinity:
    required:
    nodeSelectorTerms:
    - matchExpressions:
    - key: kubernetes.io/hostname
    operator: In
    values: ["kind-worker"]
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: manual-claim
    spec:
    storageClassName: ""
    selector:
    matchLabels:
    tier: manual
    accessModes: ["ReadWriteOnce"]
    resources:
    requests:
    storage: 500Mi
    EOF
    kubectl get pv manual-pv; kubectl get pvc manual-claim
  3. Try to expand a dynamically provisioned claim. Bind it first: only bound claims can grow.

    Terminal window
    kubectl get storageclass standard -o jsonpath='{.allowVolumeExpansion}{"\n"}'
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: grow
    spec:
    accessModes: ["ReadWriteOnce"]
    resources:
    requests:
    storage: 1Gi
    ---
    apiVersion: v1
    kind: Pod
    metadata:
    name: grower
    spec:
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/busybox:1.37.0-2
    command: ["sleep", "3600"]
    volumeMounts:
    - {name: v, mountPath: /data}
    volumes:
    - name: v
    persistentVolumeClaim:
    claimName: grow
    EOF
    kubectl wait --for=condition=Ready pod/grower --timeout=120s
    kubectl patch pvc grow -p '{"spec":{"resources":{"requests":{"storage":"2Gi"}}}}'

What you should see: The manual claim binds to manual-pv even though it asked for less: a claim gets a whole volume. The expansion patch is rejected because the standard class doesn’t set allowVolumeExpansion: true; on a CSI driver that supports it, the same patch grows the volume.

Clean up:

Terminal window
kubectl delete pv manual-pv
kubectl config set-context --current --namespace=default
kubectl delete namespace cka-pv

Practise

  • Bind a PVC, mount it in a pod, write a file, delete and recreate the pod, and read the file back.

On this site


Domain and competency names are from the CNCF CKA curriculum (v1.37), cncf/curriculum, licensed CC BY 4.0. Summaries, demos, and exercises are original to this site.