wangzhe
wangzhe

Reputation: 615

kubernetes persistent volume accessmode

It seems that Kubernetes supports 3 kinds of access mode for persistent volume: ReadWriteOnce, ReadOnlyMany, ReadWriteMany. I'm really curious about the scheduler strategy for a pod which uses the ReadWriteOnce mode volume. For example, I created an RC which have pod num=2, I guess the two pods will be scheduled into the same host because they use the volume that has ReadWriteOnce mode? I really want to know the source code of this part.

Upvotes: 40

Views: 51504

Answers (3)

Aditya Bhuyan
Aditya Bhuyan

Reputation: 448

In Kubernetes you provision storage either dynamically (using a storage class) or statically(Persistent Volume). Once the storage is available to bound and claimed, you need to configure it in what way your Pods or Nodes are connecting to the storage (a persistent volume). That could be configured in below four modes.

ReadOnlyMany (ROX)
In this mode multiple pods running on different Nodes could connect to the storage and carry out read operation.

ReadWriteMany (RWX)
In this mode multiple pods running on different Nodes could connect to the storage and carry out read and write operation.

ReadWriteOnce (RWO)
In this mode multiple pods running in only one Node could connect to the storage and carry out read and write operation.

ReadWriteOncePod (RWOP)
In this mode the volume can be mounted as read-write by a single Pod. Use ReadWriteOncePod access mode if you want to ensure that only one pod across whole cluster can read that PVC or write to it. This is only supported for CSI volumes and Kubernetes version 1.22+.

Follow the documentation to get more insight.

Upvotes: 6

DavidO
DavidO

Reputation: 1763

If a pod mounts a volume with ReadWriteOnce access mode, no other pod can mount it. In GCE (Google Compute Engine) the only allowed modes are ReadWriteOnce and ReadOnlyMany. So either one pod mounts the volume ReadWrite, or one or more pods mount the volume ReadOnlyMany.

The scheduler will not allow a pod to schedule if it uses a GCE volume that has already been mounted read-write.

(Documentation reference: persistent volume access modes)

Upvotes: 10

0xMH
0xMH

Reputation: 2132

I think DavidO's answer is wrong. As per Kubernetes docs on Access Modes:

The access modes are:

  • ReadWriteOnce -- the volume can be mounted as read-write by a single node
  • ReadOnlyMany -- the volume can be mounted read-only by many nodes
  • ReadWriteMany -- the volume can be mounted as read-write by many nodes

So AccessModes as defined today, only describe node attach (not pod mount) semantics, and doesn't enforce anything.

So to prevent two pods mount the same PVC even if they are scheduled to be run on the same node you can use pod anti-affinity. It is not the same as not to mount one volume to 2 pods scheduled on the same node. But anti-affinity can be used to ask scheduler not to run 2 pods on the same node. Therefore it prevents mounting one volume into 2 pods.

Upvotes: 31

Related Questions