Testing volume access modes

ReadWriteMany

Pods on different nodes can write simultaneously.

Trying it with pods same node

Pods are able to read/write simultaneously.

pod1.yaml

apiVersion: v1
kind: Pod
metadata:
  name: busybox-with-pvc
  labels:
    type: busybox
  namespace: learning
spec:
  containers:
    - name: busybox
      image: nginx
      volumeMounts:
        - name: nginx-pvc
          mountPath: /shared
  volumes:
    - name: nginx-pvc
      persistentVolumeClaim:
        claimName: nginx-pvc

pod2.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-with-pvc
  labels:
    type: router
  namespace: learning
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: nginx-pvc
          mountPath: /shared
  volumes:
    - name: nginx-pvc
      persistentVolumeClaim:
        claimName: nginx-pvc

And pv.yaml and pvc.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: multinode-pv
  namespace: learning  # does not matter. PVs are created irrespective of namespace
spec:
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: node
              values:
                - C
              operator: In
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 1Gi
  hostPath:
    path: /shared
    type: DirectoryOrCreate
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-pvc
  namespace: learning
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

After creation of these resources,

k exec -it pods/nginx-with-pvc -n learning -- bash
---
root@nginx-with-pvc:/shared# for i in {1..10}; do echo "$i from nginx" >> file; sleep 1; done
k exec -it pods/busybox-with-pvc -n learning -- bash
---

root@busybox-with-pvc:/shared# for i in {1..10}; do echo "$i from buxybox" >> file; sleep 1; done

Outputs

cat file
1 from buxybox
2 from buxybox
1 from nginx
3 from buxybox
2 from nginx
4 from buxybox
3 from nginx
5 from buxybox
4 from nginx
6 from buxybox
5 from nginx
7 from buxybox
6 from nginx
8 from buxybox
7 from nginx
9 from buxybox
8 from nginx
10 from buxybox
9 from nginx
10 from nginx

Trying with pods on different nodes

pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: multinode-pv
  namespace: learning  # does not matter. PVs are created irrespective of namespace
spec:
  #  nodeAffinity:
  #    required:
  #      nodeSelectorTerms:
  #        - matchExpressions:
  #            - key: node
  #              values:
  #                - C
  #              operator: In
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 1Gi
  hostPath:
    path: /shared
    type: DirectoryOrCreate

This is multi node volume as access mode is ReadWriteMany.

There are two same pods as shown above, running on two different nodes B and C.

One pod get assigned to node on B and second one on node C. So, /shared inside pods are different based on the node pods are running. As persistent volume supports multi node, so same volume works for pod on A and pod on B with different mounts for /shared on their respective node.

Conclusion

  • ReadWriteMany allows multiple pods on same or different nodes to access the persistent volume.
  • If pods are on same node and they share the same persistent volume claim, they get the same mounted path on the node.
  • If pods are on different nodes and they share same persistent volume claim, they get mounted path on their nodes.