40728

Why can you set multiple accessModes on a persistent volume?

<h3>Question</h3>

For example in the following example:

apiVersion: v1 kind: PersistentVolumeClaim metadata: name: exmaple-pvc spec: accessModes: - ReadOnlyMany - ReadWriteMany storageClassName: standard volumeMode: Filesystem resources: requests: storage: 1Gi

Why is this allowed? What is the actual behavior of the volume in this case? Read only? Read and write?


<h3>Answer1:</h3>

To be able to fully understand why a certain structure is used in a specific field of yaml definition, first we need to understand the purpose of this particular field. We need to ask what it is for, what is its function in this particular kubernetes api-resource.

I struggled a bit finding the proper explanation of accessModes in PersistentVolumeClaim and I must admit that what I found in official kubernetes docs did not safisfy me:

<blockquote>

A PersistentVolume can be mounted on a host in any way supported by the resource provider. As shown in the table below, providers will have different capabilities and each PV’s access modes are set to the specific modes supported by that particular volume. For example, NFS can support multiple read/write clients, but a specific NFS PV might be exported on the server as read-only. Each PV gets its own set of access modes describing that specific PV’s capabilities.

</blockquote>

Fortunately this time I managed to find really great explanation of this topic in openshift documentation. We can read there:

<blockquote>

Claims are matched to volumes with similar access modes. The only two matching criteria are access modes and size. A claim’s access modes represent a request. Therefore, you might be granted more, but never less. For example, if a claim requests RWO, but the only volume available is an NFS PV (RWO+ROX+RWX), the claim would then match NFS because it supports RWO.

Direct matches are always attempted first. The volume’s modes must match or contain more modes than you requested. The size must be greater than or equal to what is expected. If two types of volumes, such as NFS and iSCSI, have the same set of access modes, either of them can match a claim with those modes. There is no ordering between types of volumes and no way to choose one type over another.

All volumes with the same modes are grouped, and then sorted by size, smallest to largest. The binder gets the group with matching modes and iterates over each, in size order, until one size matches.

</blockquote>

And now probably the most important part:

<blockquote>

A volume’s AccessModes are descriptors of the volume’s capabilities. They are not enforced constraints. The storage provider is responsible for runtime errors resulting from invalid use of the resource.

</blockquote>

I emphasized this part as AccessModes can be very easily misunderstood. Let's look at the example:

apiVersion: v1 kind: PersistentVolumeClaim metadata: name: exmaple-pvc-2 spec: accessModes: - ReadOnlyMany storageClassName: standard volumeMode: Filesystem resources: requests: storage: 1Gi

The fact that we specified in our PersistentVolumeClaim definition only ReadOnlyMany access mode doesn't mean it cannot be used in other accessModes supported by our storage provider. It's important to understand that we cannot put here any constraint on how the requested storage can be used by our Pods. If our storage provider, hidden behind our standard storage class, supports also ReadWriteOnce, it will be also available for use.

Answering your particular question...

<blockquote>

Why is this allowed? What is the actual behavior of the volume in this case? Read only? Read and write?

</blockquote>

It doesn't define behavior of the volume at all. The volume will behave according to its capabilities (we don't define them, they are imposed in advance, being part of the storage specification). In other words we will be able to use it in our Pods in all possible ways, in which it is allowed to be used.

Let's say our standard storage provisioner, which in case of GKE happens to be Google Compute Engine Persistent Disk:

$ kubectl get storageclass NAME PROVISIONER AGE standard (default) kubernetes.io/gce-pd 10d

currently supports two AccessModes:

<ul><li>ReadWriteOnce</li> <li>ReadOnlyMany</li> </ul>

So we can use all of them, no matter what we specified in our claim e.g. this way:

apiVersion: apps/v1 kind: Deployment metadata: name: my-app labels: app: my-app spec: replicas: 1 selector: matchLabels: app: debian template: metadata: labels: app: debian spec: containers: - name: debian image: debian command: ['sh', '-c', 'sleep 3600'] volumeMounts: - mountPath: "/mnt" name: my-volume readOnly: true volumes: - name: my-volume persistentVolumeClaim: claimName: example-pvc-2 initContainers: - name: init-myservice image: busybox command: ['sh', '-c', 'echo "Content of my file" > /mnt/my_file'] volumeMounts: - mountPath: "/mnt" name: my-volume

In the above example both capabilities are used. First our volume is mounted in rw mode by the init container which saves to it some file and after that it is mounted to the main container as read-only file system. We are still able to do it even though we specified in our PersistentVolumeClaim only one access mode:

spec: accessModes: - ReadOnlyMany

Going back to the question you asked in the title:

<blockquote>

Why can you set multiple accessModes on a persistent volume?

</blockquote>

the answer is: You cannot set them at all as they are already set by the storage provider, you can only request this way what storage you want, what requirements it must meet and one of these requirements are access modes it supports.

Basically by typing:

spec: accessModes: - ReadOnlyMany - ReadWriteOnce

in our PersistentVolulmeClaim definition we say:

<em>"Hey! Storage provider! Give me a volume that supports this set of accessModes. I don't care if it supports any others, like ReadWriteMany, as I don't need them. Give me something that meets my requirements!"</em>

I believe that further explanation why <em>an array</em> is used here is not needed.


<h3>Answer2:</h3>

A persistent volume can be mounted by multiple pods on the different node at the same. One pod can mount a persistent volume with only one access mode at a time and other pods can mount the same persistent volume with different access mode. But a pod can mount the persistent volume with only one access mode.

Documentation reference for those who didn't understand the question: persistent volume access modes

来源:https://stackoverflow.com/questions/60248790/why-can-you-set-multiple-accessmodes-on-a-persistent-volume

Recommend

  • Compile error using fast pipe operator after pipe last in ReasonML
  • How To Set a Value to a TableAdapter Parameter
  • Android Blog App, should I use Sharedpreferences or Intents?
  • How does NumPy process docstrings into sphinx documentation for Parameters?
  • How to return only text after a comma in a string
  • All I get is a blank page when I visit localhost:9080
  • placeholder works for text but not password box in IE/Firefox using this javascript
  • swift how to execute javascript after anjular have rendered the page?
  • send arraylist as parameter in volley request
  • layer popup with jQuery
  • How to add database query results to an array
  • python & pandas- Calculation bewteen rows based on certain values in columns from DataFrame
  • Objective-C version of PHP mcrypt_encrypt
  • Redirecting the print output to a .txt file in Python
  • Parallel exact matrix diagonalization with Python
  • Delphi MainFormOnTaskBar Modal windows bug
  • Spring Security sec:authorize tag using Java Config
  • Create my own classList object when the browser does not implement it itself
  • Raphael-GWT: fill image of a shape (Rect) appears offset. How to resolve this?
  • List using with references, changes behavior when used as a member
  • Map Annotation Disclosure Indicator - Xamarin.Form
  • Was there ever a proposal to include the URL fragment into the HTTP request?
  • Custom Timeline items in Glimpse
  • Optimization of optim() in R ( L-BFGS-B needs finite values of 'fn')
  • Pick Out Specific Number from Array? [duplicate]
  • how to add dashed border on highcharts “area” graph for every point
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • Ajax call on Multiple selection in Select box
  • gnuplot - How to make zmin equal to zmax keeeping autoscale on z axis
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • Print Longitude and Latitude in textbox Android
  • iphone image to video issue in video speed
  • Firebase: How to read from external DB?