Skip to content

Commit b592215

Browse files
committed
Move mounted Secrets sections verbatim to task page
1 parent 4dfacee commit b592215

File tree

3 files changed

+122
-156
lines changed

3 files changed

+122
-156
lines changed

content/en/docs/concepts/configuration/secret.md

Lines changed: 0 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -181,45 +181,6 @@ If you want to access data from a Secret in a Pod, one way to do that is to
181181
have Kubernetes make the value of that Secret be available as a file inside
182182
the filesystem of one or more of the Pod's containers.
183183

184-
To configure that, you:
185-
186-
1. Create a secret or use an existing one. Multiple Pods can reference the same secret.
187-
1. Modify your Pod definition to add a volume under `.spec.volumes[]`. Name the volume anything,
188-
and have a `.spec.volumes[].secret.secretName` field equal to the name of the Secret object.
189-
1. Add a `.spec.containers[].volumeMounts[]` to each container that needs the secret. Specify
190-
`.spec.containers[].volumeMounts[].readOnly = true` and
191-
`.spec.containers[].volumeMounts[].mountPath` to an unused directory name where you would like the
192-
secrets to appear.
193-
1. Modify your image or command line so that the program looks for files in that directory. Each
194-
key in the secret `data` map becomes the filename under `mountPath`.
195-
196-
This is an example of a Pod that mounts a Secret named `mysecret` in a volume:
197-
198-
```yaml
199-
apiVersion: v1
200-
kind: Pod
201-
metadata:
202-
name: mypod
203-
spec:
204-
containers:
205-
- name: mypod
206-
image: redis
207-
volumeMounts:
208-
- name: foo
209-
mountPath: "/etc/foo"
210-
readOnly: true
211-
volumes:
212-
- name: foo
213-
secret:
214-
secretName: mysecret
215-
optional: false # default setting; "mysecret" must exist
216-
```
217-
218-
Each Secret you want to use needs to be referred to in `.spec.volumes`.
219-
220-
If there are multiple containers in the Pod, then each container needs its
221-
own `volumeMounts` block, but only one `.spec.volumes` is needed per Secret.
222-
223184
{{< note >}}
224185
Versions of Kubernetes before v1.22 automatically created credentials for accessing
225186
the Kubernetes API. This older mechanism was based on creating token Secrets that
@@ -238,123 +199,6 @@ You can use the [`kubectl create token`](/docs/reference/generated/kubectl/kubec
238199
command to obtain a token from the `TokenRequest` API.
239200
{{< /note >}}
240201

241-
#### Projection of Secret keys to specific paths
242-
243-
You can also control the paths within the volume where Secret keys are projected.
244-
You can use the `.spec.volumes[].secret.items` field to change the target path of each key:
245-
246-
```yaml
247-
apiVersion: v1
248-
kind: Pod
249-
metadata:
250-
name: mypod
251-
spec:
252-
containers:
253-
- name: mypod
254-
image: redis
255-
volumeMounts:
256-
- name: foo
257-
mountPath: "/etc/foo"
258-
readOnly: true
259-
volumes:
260-
- name: foo
261-
secret:
262-
secretName: mysecret
263-
items:
264-
- key: username
265-
path: my-group/my-username
266-
```
267-
268-
What will happen:
269-
270-
* the `username` key from `mysecret` is available to the container at the path
271-
`/etc/foo/my-group/my-username` instead of at `/etc/foo/username`.
272-
* the `password` key from that Secret object is not projected.
273-
274-
If `.spec.volumes[].secret.items` is used, only keys specified in `items` are projected.
275-
To consume all keys from the Secret, all of them must be listed in the `items` field.
276-
277-
If you list keys explicitly, then all listed keys must exist in the corresponding Secret.
278-
Otherwise, the volume is not created.
279-
280-
#### Secret files permissions
281-
282-
You can set the POSIX file access permission bits for a single Secret key.
283-
If you don't specify any permissions, `0644` is used by default.
284-
You can also set a default mode for the entire Secret volume and override per key if needed.
285-
286-
For example, you can specify a default mode like this:
287-
288-
```yaml
289-
apiVersion: v1
290-
kind: Pod
291-
metadata:
292-
name: mypod
293-
spec:
294-
containers:
295-
- name: mypod
296-
image: redis
297-
volumeMounts:
298-
- name: foo
299-
mountPath: "/etc/foo"
300-
volumes:
301-
- name: foo
302-
secret:
303-
secretName: mysecret
304-
defaultMode: 0400
305-
```
306-
307-
The secret is mounted on `/etc/foo`; all the files created by the
308-
secret volume mount have permission `0400`.
309-
310-
{{< note >}}
311-
If you're defining a Pod or a Pod template using JSON, beware that the JSON
312-
specification doesn't support octal notation. You can use the decimal value
313-
for the `defaultMode` (for example, 0400 in octal is 256 in decimal) instead.
314-
If you're writing YAML, you can write the `defaultMode` in octal.
315-
{{< /note >}}
316-
317-
#### Consuming Secret values from volumes
318-
319-
Inside the container that mounts a secret volume, the secret keys appear as
320-
files. The secret values are base64 decoded and stored inside these files.
321-
322-
This is the result of commands executed inside the container from the example above:
323-
324-
```shell
325-
ls /etc/foo/
326-
```
327-
328-
The output is similar to:
329-
330-
```
331-
username
332-
password
333-
```
334-
335-
```shell
336-
cat /etc/foo/username
337-
```
338-
339-
The output is similar to:
340-
341-
```
342-
admin
343-
```
344-
345-
```shell
346-
cat /etc/foo/password
347-
```
348-
349-
The output is similar to:
350-
351-
```
352-
1f2d1e2e67df
353-
```
354-
355-
The program in a container is responsible for reading the secret data from these
356-
files, as needed.
357-
358202
#### Mounted Secrets are updated automatically
359203

360204
When a volume contains data from a Secret, and that Secret is updated, Kubernetes tracks

content/en/docs/tasks/inject-data-application/distribute-credentials-secure.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,127 @@ Here is a configuration file you can use to create a Pod:
149149
39528$vdg7Jb
150150
```
151151

152+
Modify your image or command line so that the program looks for files in the
153+
`mountPath` directory. Each key in the Secret `data` map becomes a file name
154+
in this directory.
155+
156+
#### Projection of Secret keys to specific paths
157+
158+
You can also control the paths within the volume where Secret keys are projected.
159+
You can use the `.spec.volumes[].secret.items` field to change the target path of each key:
160+
161+
```yaml
162+
apiVersion: v1
163+
kind: Pod
164+
metadata:
165+
name: mypod
166+
spec:
167+
containers:
168+
- name: mypod
169+
image: redis
170+
volumeMounts:
171+
- name: foo
172+
mountPath: "/etc/foo"
173+
readOnly: true
174+
volumes:
175+
- name: foo
176+
secret:
177+
secretName: mysecret
178+
items:
179+
- key: username
180+
path: my-group/my-username
181+
```
182+
183+
What will happen:
184+
185+
* the `username` key from `mysecret` is available to the container at the path
186+
`/etc/foo/my-group/my-username` instead of at `/etc/foo/username`.
187+
* the `password` key from that Secret object is not projected.
188+
189+
If `.spec.volumes[].secret.items` is used, only keys specified in `items` are projected.
190+
To consume all keys from the Secret, all of them must be listed in the `items` field.
191+
192+
If you list keys explicitly, then all listed keys must exist in the corresponding Secret.
193+
Otherwise, the volume is not created.
194+
195+
#### Secret files permissions
196+
197+
You can set the POSIX file access permission bits for a single Secret key.
198+
If you don't specify any permissions, `0644` is used by default.
199+
You can also set a default mode for the entire Secret volume and override per key if needed.
200+
201+
For example, you can specify a default mode like this:
202+
203+
```yaml
204+
apiVersion: v1
205+
kind: Pod
206+
metadata:
207+
name: mypod
208+
spec:
209+
containers:
210+
- name: mypod
211+
image: redis
212+
volumeMounts:
213+
- name: foo
214+
mountPath: "/etc/foo"
215+
volumes:
216+
- name: foo
217+
secret:
218+
secretName: mysecret
219+
defaultMode: 0400
220+
```
221+
222+
The secret is mounted on `/etc/foo`; all the files created by the
223+
secret volume mount have permission `0400`.
224+
225+
{{< note >}}
226+
If you're defining a Pod or a Pod template using JSON, beware that the JSON
227+
specification doesn't support octal notation. You can use the decimal value
228+
for the `defaultMode` (for example, 0400 in octal is 256 in decimal) instead.
229+
If you're writing YAML, you can write the `defaultMode` in octal.
230+
{{< /note >}}
231+
232+
#### Consuming Secret values from volumes
233+
234+
Inside the container that mounts a secret volume, the secret keys appear as
235+
files. The secret values are base64 decoded and stored inside these files.
236+
237+
This is the result of commands executed inside the container from the example above:
238+
239+
```shell
240+
ls /etc/foo/
241+
```
242+
243+
The output is similar to:
244+
245+
```
246+
username
247+
password
248+
```
249+
250+
```shell
251+
cat /etc/foo/username
252+
```
253+
254+
The output is similar to:
255+
256+
```
257+
admin
258+
```
259+
260+
```shell
261+
cat /etc/foo/password
262+
```
263+
264+
The output is similar to:
265+
266+
```
267+
1f2d1e2e67df
268+
```
269+
270+
The program in a container is responsible for reading the secret data from these
271+
files, as needed.
272+
152273
## Define container environment variables using Secret data
153274

154275
### Define a container environment variable with data from a single Secret

content/en/examples/pods/inject/secret-pod.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ spec:
1010
# name must match the volume name below
1111
- name: secret-volume
1212
mountPath: /etc/secret-volume
13+
readOnly: true
1314
# The secret data is exposed to Containers in the Pod through a Volume.
1415
volumes:
1516
- name: secret-volume

0 commit comments

Comments
 (0)