Bird
Raised Fist0
Kubernetesdevops~5 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which Kubernetes access mode allows a volume to be mounted as read-write by only one node at a time?
easy
A. ReadWriteOnce
B. ReadOnlyMany
C. ReadWriteMany
D. ReadWriteAll

Solution

  1. Step 1: Understand ReadWriteOnce meaning

    ReadWriteOnce means the volume can be mounted as read-write by a single node only.
  2. Step 2: Compare with other modes

    ReadOnlyMany allows multiple nodes but only read access; ReadWriteMany allows multiple nodes with read-write access.
  3. Final Answer:

    ReadWriteOnce -> Option A
  4. Quick Check:

    Single writer = ReadWriteOnce [OK]
Hint: Single writer means ReadWriteOnce [OK]
Common Mistakes:
  • Confusing ReadWriteOnce with ReadWriteMany
  • Thinking ReadOnlyMany allows write access
  • Assuming ReadWriteAll is a valid mode
2. Which of the following is the correct YAML snippet to specify a volume with ReadOnlyMany access mode?
easy
A. accessModes: - ReadWriteOnce
B. accessModes: - ReadWriteMany
C. accessModes: - ReadOnlyMany
D. accessModes: - ReadOnlyOnce

Solution

  1. Step 1: Identify correct access mode syntax

    The accessModes field is a list with valid values: ReadWriteOnce, ReadOnlyMany, ReadWriteMany.
  2. Step 2: Match ReadOnlyMany exactly

    accessModes: - ReadOnlyMany correctly uses "ReadOnlyMany" as the access mode in YAML format.
  3. Final Answer:

    accessModes:\n - ReadOnlyMany -> Option C
  4. Quick Check:

    YAML list with ReadOnlyMany is correct [OK]
Hint: YAML list with dash and correct mode name [OK]
Common Mistakes:
  • Using invalid mode ReadOnlyOnce
  • Missing dash before access mode
  • Using ReadWriteOnce instead of ReadOnlyMany
3. Given a PersistentVolumeClaim with accessModes set to [ReadWriteMany], what will happen if two pods on different nodes try to write to the volume simultaneously?
medium
A. Pods can only read, not write to the volume.
B. Only one pod can write; the other will get a write error.
C. The volume will be mounted read-only on both pods.
D. Both pods can write to the volume at the same time without issues.

Solution

  1. Step 1: Understand ReadWriteMany behavior

    ReadWriteMany allows multiple nodes to mount the volume with read-write access simultaneously.
  2. Step 2: Apply to pods on different nodes

    Since both pods are on different nodes, they can both write to the volume at the same time without conflict.
  3. Final Answer:

    Both pods can write to the volume at the same time without issues. -> Option D
  4. Quick Check:

    ReadWriteMany = multiple writers allowed [OK]
Hint: ReadWriteMany means multiple writers allowed [OK]
Common Mistakes:
  • Assuming only one writer allowed with ReadWriteMany
  • Confusing ReadOnlyMany with ReadWriteMany
  • Thinking volume becomes read-only automatically
4. You have a PersistentVolumeClaim with accessModes set to ReadWriteOnce. You try to mount it as read-write on two pods running on different nodes. What is the likely outcome?
medium
A. Both pods mount the volume read-write successfully.
B. Only one pod mounts the volume read-write; the other fails to mount.
C. Both pods mount the volume read-only.
D. Both pods fail to mount the volume.

Solution

  1. Step 1: Recall ReadWriteOnce limitation

    ReadWriteOnce allows only one node to mount the volume as read-write at a time.
  2. Step 2: Analyze mounting on two nodes

    Since pods are on different nodes, only one pod can mount read-write; the other pod's mount will fail.
  3. Final Answer:

    Only one pod mounts the volume read-write; the other fails to mount. -> Option B
  4. Quick Check:

    ReadWriteOnce = single node writer [OK]
Hint: ReadWriteOnce limits to one node writer [OK]
Common Mistakes:
  • Assuming both pods can mount read-write
  • Thinking volume auto-switches to read-only
  • Believing both pods fail to mount
5. You need to design a shared storage solution for a Kubernetes cluster where multiple pods across different nodes must read and write data simultaneously. Which access mode should you choose and why?
hard
A. ReadWriteMany, because it allows multiple pods on different nodes to read and write simultaneously.
B. ReadOnlyMany, because it allows multiple pods to write simultaneously.
C. ReadWriteOnce, because it supports multiple writers on different nodes.
D. ReadWriteOnce, because it allows multiple pods to read-only mount the volume.

Solution

  1. Step 1: Identify requirement for multiple writers on different nodes

    The requirement is for multiple pods on different nodes to read and write simultaneously.
  2. Step 2: Match access mode to requirement

    ReadWriteMany allows multiple nodes to mount the volume with read-write access at the same time, fulfilling the requirement.
  3. Final Answer:

    ReadWriteMany, because it allows multiple pods on different nodes to read and write simultaneously. -> Option A
  4. Quick Check:

    Shared read-write = ReadWriteMany [OK]
Hint: Multiple writers/readers need ReadWriteMany [OK]
Common Mistakes:
  • Choosing ReadWriteOnce for multi-node writes
  • Confusing ReadOnlyMany as write-capable
  • Misunderstanding access mode capabilities
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard