0
0
Kubernetesdevops~5 mins

Access modes (ReadWriteOnce, ReadOnlyMany, ReadWriteMany) in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you use storage in Kubernetes, you need to tell how your app will use it. Access modes define if the storage can be used by one or many apps and if it can be written to or only read.
When you want a single app to write data to a storage volume.
When multiple apps need to read the same data but not change it.
When multiple apps need to read and write data on the same storage at the same time.
When you want to avoid data conflicts by limiting write access to one app.
When you want to share configuration files or logs across many apps in read-only mode.
Config File - persistentvolumeclaim.yaml
persistentvolumeclaim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: example-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: standard

This file creates a PersistentVolumeClaim (PVC) that asks for 1 gigabyte of storage.

The accessModes field sets how the storage can be used:

  • ReadWriteOnce: One app can write to it.
  • ReadOnlyMany: Many apps can read it but not write.
  • ReadWriteMany: Many apps can read and write.

The storageClassName tells Kubernetes which storage type to use.

Commands
This command creates the PersistentVolumeClaim in Kubernetes, requesting storage with the specified access mode.
Terminal
kubectl apply -f persistentvolumeclaim.yaml
Expected OutputExpected
persistentvolumeclaim/example-pvc created
This command checks the status of the PersistentVolumeClaim to see if it is bound to storage and ready to use.
Terminal
kubectl get pvc example-pvc
Expected OutputExpected
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE example-pvc Bound pvc-12345678-90ab-cdef-1234-567890abcdef 1Gi RWO standard 10s
This command shows detailed information about the PersistentVolumeClaim, including the access modes and volume details.
Terminal
kubectl describe pvc example-pvc
Expected OutputExpected
Name: example-pvc Namespace: default StorageClass: standard Status: Bound Volume: pvc-12345678-90ab-cdef-1234-567890abcdef Labels: <none> Annotations: <none> Finalizers: [kubernetes.io/pvc-protection] Capacity: 1Gi Access Modes: ReadWriteOnce Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: access modes control how many apps can use the storage and if they can write or only read.

Common Mistakes
Using ReadWriteMany on storage that does not support multiple writers.
The storage will fail to mount or cause data corruption because the underlying storage does not allow multiple writers.
Check your storage provider supports ReadWriteMany before using it; otherwise, use ReadWriteOnce.
Setting accessModes to ReadOnlyMany but expecting to write data.
The storage will be mounted read-only, so write operations will fail.
Use ReadWriteOnce or ReadWriteMany if you need write access.
Not specifying accessModes in the PersistentVolumeClaim.
Kubernetes may not know how to bind the volume properly, causing errors or unexpected behavior.
Always specify the correct accessModes that match your app's needs.
Summary
Create a PersistentVolumeClaim with the desired access mode to control storage usage.
Use kubectl apply to create the claim and kubectl get pvc to check its status.
Access modes define if storage can be used by one or many apps and if it is writable or read-only.