Skip to content

Commit 8196378

Browse files
committed
Blog post about OCI volume sources / KEP-4639
Signed-off-by: Sascha Grunert <[email protected]>
1 parent 947950c commit 8196378

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
layout: blog
3+
title: "Kubernetes 1.31: Image Volume Source alpha feature"
4+
date: 2024-08-15
5+
slug: kubernetes-1-31-image-volume-source
6+
author: Sascha Grunert
7+
---
8+
9+
The Kubernetes community is moving towards fulfilling more Artificial
10+
Intelligence (AI) and Machine Learning (ML) use cases in the future. While the
11+
project has been designed to fulfil microservice architectures in the past, it’s
12+
now time to listen to the end users and introduce features which have a stronger
13+
focus on AI/ML.
14+
15+
One of these requirements is to support [Open Container Initiative (OCI)](https://opencontainers.org)
16+
compatible images and artifacts (referred as OCI objects) directly as a native
17+
volume source. This allows users to focus on OCI standards as well as enables
18+
them to store and distribute any content using OCI registries. A feature like
19+
this gives the Kubernetes project a chance to grow into use cases which go
20+
beyond running particular images.
21+
22+
Given that, the Kubernetes community is proud to present a new alpha feature introduced in
23+
v1.31: The Image Volume Source ([KEP-4639](https://kep.k8s.io/4639)). This
24+
feature allows users to specify an image reference as volume in a pod while
25+
reusing it as volume mount within containers:
26+
27+
```yaml
28+
29+
kind: Pod
30+
spec:
31+
containers:
32+
-
33+
volumeMounts:
34+
- name: my-volume
35+
mountPath: /path/to/directory
36+
volumes:
37+
- name: my-volume
38+
image:
39+
reference: my-image:tag
40+
```
41+
42+
The above example would result in mounting `my-image:tag` to
43+
`/path/to/directory` in the pod’s container.
44+
45+
## Use cases
46+
47+
The goal of this enhancement is to stick as close as possible to the existing
48+
[container image](/docs/concepts/containers/images/) implementation within the
49+
kubelet, while introducing a new API surface to allow more extended use cases.
50+
51+
For example, users could share a configuration file among multiple containers in
52+
a pod without including the file in the main image, so that they can minimize
53+
security risks and the overall image size. They can also package and distribute
54+
binary artifacts using OCI images and mount them directly into Kubernetes pods,
55+
so that they can streamline their CI/CD pipeline as an example.
56+
57+
Data scientists, MLOps engineers, or AI developers, can mount large language
58+
model weights or machine learning model weights in a pod alongside a
59+
model-server, so that they can efficiently serve them without including them in
60+
the model-server container image. They can package these in an OCI object to
61+
take advantage of OCI distribution and ensure efficient model deployment. This
62+
allows them to separate the model specifications/content from the executables
63+
that process them.
64+
65+
Another use case is that security engineers can use a public image for a malware
66+
scanner and mount in a volume of private (commercial) malware signatures, so
67+
that they can load those signatures without baking the own combined image (which
68+
might not be allowed by the copyright on the public image). Those files work
69+
regardless of the OS or version of the scanning software.
70+
71+
But in long term it will be up to **you** as and end user of this project to
72+
outline further important use cases for the new `ImageVolume` feature.
73+
[SIG Node](https://github.com/kubernetes/community/blob/54a67f5/sig-node/README.md)
74+
is happy to retrieve any feedback or suggestions for further enhancements to
75+
allow more advanced usage scenarios. Feel free to provide feedback by either
76+
using the [Kubernetes Slack (#sig-node)](https://kubernetes.slack.com/messages/sig-node)
77+
channel or the [SIG Node mailing list](https://groups.google.com/g/kubernetes-sig-node).
78+
79+
## Detailed Example
80+
81+
The Kubernetes alpha feature gate [`ImageVolume`](/docs/reference/command-line-tools-reference/feature-gates)
82+
needs to be enabled on the [API Server](/docs/reference/command-line-tools-reference/kube-apiserver)
83+
as well as the [kubelet](/docs/reference/command-line-tools-reference/kubelet)
84+
to make it functional. If that’s the case and the [container runtime](/docs/setup/production-environment/container-runtimes)
85+
has support for the feature (like CRI-O ≥ v1.31), then an example `pod.yaml`
86+
like this can be created:
87+
88+
```yaml
89+
apiVersion: v1
90+
kind: Pod
91+
metadata:
92+
name: pod
93+
spec:
94+
containers:
95+
- name: test
96+
image: registry.k8s.io/e2e-test-images/echoserver:2.3
97+
volumeMounts:
98+
- name: volume
99+
mountPath: /volume
100+
volumes:
101+
- name: volume
102+
image:
103+
reference: quay.io/crio/artifact:v1
104+
pullPolicy: IfNotPresent
105+
```
106+
107+
The pod declares a new volume using the `image.reference` of
108+
`quay.io/crio/artifact:v1`, which refers to an OCI object containing two files.
109+
The `pullPolicy` behaves in the same way as for container images and allows the
110+
following values:
111+
112+
- `Always`: the kubelet always attempts to pull the reference and the container
113+
creation will fail if the pull fails.
114+
- `Never`: the kubelet never pulls the reference and only uses a local image or
115+
artifact. The container creation will fail if the reference isn’t present.
116+
- `IfNotPresent`: the kubelet pulls if the reference isn’t already present on
117+
disk. The container creation will fail if the reference isn’t present and the
118+
pull fails.
119+
120+
The `volumeMounts` field is indicating that the container with the name `test`
121+
should mount the volume under the path `/volume`.
122+
123+
If we now create the pod:
124+
125+
```shell
126+
kubectl apply -f pod.yaml
127+
```
128+
129+
And exec into it:
130+
131+
```shell
132+
kubectl exec -it pod -- sh
133+
```
134+
135+
Then we’re able to investigate what has been mounted:
136+
137+
```console
138+
/ # ls /volume
139+
dir file
140+
/ # cat /volume/file
141+
2
142+
/ # ls /volume/dir
143+
file
144+
/ # cat /volume/dir/file
145+
1
146+
```
147+
148+
**We managed to consume an OCI artifact using Kubernetes!**
149+
150+
The container runtime pulled the image (or artifact), mounted it to the
151+
container and made it finally available for direct usage. There are a bunch of
152+
details in the implementation, which closely align to the existing image pull
153+
behavior of the kubelet. For example:
154+
155+
- If a `:latest` tag as `reference` is provided, then the `pullPolicy` will
156+
default to `Always`, while in any other case it will default to `IfNotPresent`
157+
if unset.
158+
- The volume gets re-resolved if the pod gets deleted and recreated, which means
159+
that new remote content will become available on pod recreation. A failure to
160+
resolve or pull the image during pod startup will block containers from
161+
starting and may add significant latency. Failures will be retried using
162+
normal volume backoff and will be reported on the pod reason and message.
163+
- Pull secrets will be assembled in the same way as for the container image by
164+
looking up node credentials, service account image pull secrets, and pod spec
165+
image pull secrets.
166+
- The OCI object gets mounted in a single directory by merging the manifest
167+
layers in the same way as for container images.
168+
- The volume will be mounted read-only (`ro`) and non-executable files
169+
(`noexec`).
170+
- Sub path mounts for containers are not supported
171+
(`spec.containers[*].volumeMounts.subpath`).
172+
- The field `spec.securityContext.fsGroupChangePolicy` has no effect on this
173+
volume type.
174+
- The feature will also work with the [`AlwaysPullImages` admission plugin](/docs/reference/access-authn-authz/admission-controllers/#alwayspullimages)
175+
if enabled.
176+
177+
Thank you for reading through the end of this blog post! SIG Node is proud and
178+
happy to deliver this feature as part of Kubernetes v1.31.
179+
180+
As writer of this blog post, I would like to emphasize my special thanks to
181+
**all** involved individuals out there! You all rock, let’s keep on hacking!
182+
183+
## Further reading
184+
185+
- [Use an Image Volume With a Pod](/docs/tasks/configure-pod-container/image-volumes)
186+
- [`image` volume overview](/docs/concepts/storage/volumes/#image)

0 commit comments

Comments
 (0)