|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: 'Kubernetes 1.30: Read-only volume mounts can be finally literally read-only' |
| 4 | +date: 2024-04-23 |
| 5 | +slug: recursive-read-only-mounts |
| 6 | +--- |
| 7 | + |
| 8 | +**Author:** Akihiro Suda (NTT) |
| 9 | + |
| 10 | +Read-only volume mounts have been a feature of Kubernetes since the beginning. |
| 11 | +Surprisingly, read-only mounts are not completely read-only under certain conditions on Linux. |
| 12 | +As of the v1.30 release, they can be made completely read-only, |
| 13 | +with alpha support for _recursive read-only mounts_. |
| 14 | + |
| 15 | +## Read-only volume mounts are not really read-only by default |
| 16 | + |
| 17 | +Volume mounts can be deceptively complicated. |
| 18 | + |
| 19 | +You might expect that the following manifest makes everything under `/mnt` in the containers read-only: |
| 20 | +```yaml |
| 21 | +--- |
| 22 | +apiVersion: v1 |
| 23 | +kind: Pod |
| 24 | +spec: |
| 25 | + volumes: |
| 26 | + - name: mnt |
| 27 | + hostPath: |
| 28 | + path: /mnt |
| 29 | + containers: |
| 30 | + - volumeMounts: |
| 31 | + - name: mnt |
| 32 | + mountPath: /mnt |
| 33 | + readOnly: true |
| 34 | +``` |
| 35 | +
|
| 36 | +However, any sub-mounts beneath `/mnt` may still be writable! |
| 37 | +For example, consider that `/mnt/my-nfs-server` is writeable on the host. |
| 38 | +Inside the container, writes to `/mnt/*` will be rejected but `/mnt/my-nfs-server/*` will still be writeable. |
| 39 | + |
| 40 | +## New mount option: recursiveReadOnly |
| 41 | + |
| 42 | +Kubernetes 1.30 added a new mount option `recursiveReadOnly` so as to make submounts recursively read-only. |
| 43 | + |
| 44 | +The option can be enabled as follows: |
| 45 | +{{< highlight yaml "linenos=false,hl_lines=14-17" >}} |
| 46 | +--- |
| 47 | +apiVersion: v1 |
| 48 | +kind: Pod |
| 49 | +spec: |
| 50 | + volumes: |
| 51 | + - name: mnt |
| 52 | + hostPath: |
| 53 | + path: /mnt |
| 54 | + containers: |
| 55 | + - volumeMounts: |
| 56 | + - name: mnt |
| 57 | + mountPath: /mnt |
| 58 | + readOnly: true |
| 59 | + # NEW |
| 60 | + # Possible values are `Enabled`, `IfPossible`, and `Disabled`. |
| 61 | + # Needs to be specified in conjunction with `readOnly: true`. |
| 62 | + recursiveReadOnly: Enabled |
| 63 | +{{< /highlight >}} |
| 64 | + |
| 65 | +This is implemented by applying the `MOUNT_ATTR_RDONLY` attribute with the `AT_RECURSIVE` flag |
| 66 | +using [`mount_setattr(2)`](https://man7.org/linux/man-pages/man2/mount_setattr.2.html) added in |
| 67 | +Linux kernel v5.12. |
| 68 | + |
| 69 | +For backwards compatibility, the `recursiveReadOnly` field is not a replacement for `readOnly`, |
| 70 | +but is used _in conjunction_ with it. |
| 71 | +To get a properly recursive read-only mount, you must set both fields. |
| 72 | + |
| 73 | +## Feature availability {#availability} |
| 74 | + |
| 75 | +To enable `recursiveReadOnly` mounts, the following components have to be used: |
| 76 | + |
| 77 | +* Kubernetes: v1.30 or later, with the `RecursiveReadOnlyMounts` |
| 78 | + [feature gate](/docs/reference/command-line-tools-reference/feature-gates/) enabled. |
| 79 | + As of v1.30, the gate is marked as alpha. |
| 80 | + |
| 81 | +* CRI runtime: |
| 82 | + * containerd: v2.0 or later |
| 83 | + |
| 84 | +* OCI runtime: |
| 85 | + * runc: v1.1 or later |
| 86 | + * crun: v1.8.6 or later |
| 87 | + |
| 88 | +* Linux kernel: v5.12 or later |
| 89 | + |
| 90 | +## What's next? |
| 91 | + |
| 92 | +Kubernetes SIG Node hope - and expect - that the feature will be promoted to beta and eventually |
| 93 | +general availability (GA) in future releases of Kubernetes, so that users no longer need to enable |
| 94 | +the feature gate manually. |
| 95 | + |
| 96 | +The default value of `recursiveReadOnly` will still remain `Disabled`, for backwards compatibility. |
| 97 | + |
| 98 | +## How can I learn more? |
| 99 | + |
| 100 | +<!-- https://github.com/kubernetes/website/pull/45159 --> |
| 101 | +Please check out the [documentation](/docs/concepts/storage/volumes/#read-only-mounts) |
| 102 | +for the further details of `recursiveReadOnly` mounts. |
| 103 | + |
| 104 | +## How to get involved? |
| 105 | + |
| 106 | +This feature is driven by the SIG Node community. Please join us to connect with |
| 107 | +the community and share your ideas and feedback around the above feature and |
| 108 | +beyond. We look forward to hearing from you! |
0 commit comments