Skip to content

Commit 831f795

Browse files
authored
Merge pull request #38083 from shannonxtreme/using-secrets
Improve wording around Secrets as environment variables
2 parents c3cb31c + 679d205 commit 831f795

File tree

3 files changed

+123
-264
lines changed

3 files changed

+123
-264
lines changed

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

Lines changed: 29 additions & 264 deletions
Original file line numberDiff line numberDiff line change
@@ -165,35 +165,9 @@ for that Pod, including details of the problem fetching the Secret.
165165

166166
#### Optional Secrets {#restriction-secret-must-exist}
167167

168-
When you define a container environment variable based on a Secret,
169-
you can mark it as _optional_. The default is for the Secret to be
170-
required.
171-
172-
None of a Pod's containers will start until all non-optional Secrets are
173-
available.
174-
175-
If a Pod references a specific key in a Secret and that Secret does exist, but
176-
is missing the named key, the Pod fails during startup.
177-
178-
### Using Secrets as files from a Pod {#using-secrets-as-files-from-a-pod}
179-
180-
If you want to access data from a Secret in a Pod, one way to do that is to
181-
have Kubernetes make the value of that Secret be available as a file inside
182-
the filesystem of one or more of the Pod's containers.
183-
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:
168+
When you reference a Secret in a Pod, you can mark the Secret as _optional_,
169+
such as in the following example. If an optional Secret doesn't exist,
170+
Kubernetes ignores it.
197171

198172
```yaml
199173
apiVersion: v1
@@ -212,13 +186,20 @@ spec:
212186
- name: foo
213187
secret:
214188
secretName: mysecret
215-
optional: false # default setting; "mysecret" must exist
189+
optional: true
216190
```
217191
218-
Each Secret you want to use needs to be referred to in `.spec.volumes`.
192+
By default, Secrets are required. None of a Pod's containers will start until
193+
all non-optional Secrets are available.
194+
195+
If a Pod references a specific key in a non-optional Secret and that Secret
196+
does exist, but is missing the named key, the Pod fails during startup.
219197
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.
198+
### Using Secrets as files from a Pod {#using-secrets-as-files-from-a-pod}
199+
200+
If you want to access data from a Secret in a Pod, one way to do that is to
201+
have Kubernetes make the value of that Secret be available as a file inside
202+
the filesystem of one or more of the Pod's containers.
222203
223204
{{< note >}}
224205
Versions of Kubernetes before v1.22 automatically created credentials for accessing
@@ -238,123 +219,6 @@ You can use the [`kubectl create token`](/docs/reference/generated/kubectl/kubec
238219
command to obtain a token from the `TokenRequest` API.
239220
{{< /note >}}
240221

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-
358222
#### Mounted Secrets are updated automatically
359223

360224
When a volume contains data from a Secret, and that Secret is updated, Kubernetes tracks
@@ -388,53 +252,23 @@ watch propagation delay, the configured cache TTL, or zero for direct polling).
388252
To use a Secret in an {{< glossary_tooltip text="environment variable" term_id="container-env-variables" >}}
389253
in a Pod:
390254

391-
1. Create a Secret (or use an existing one). Multiple Pods can reference the same Secret.
392-
1. Modify your Pod definition in each container that you wish to consume the value of a secret
393-
key to add an environment variable for each secret key you wish to consume. The environment
394-
variable that consumes the secret key should populate the secret's name and key in `env[].valueFrom.secretKeyRef`.
395-
1. Modify your image and/or command line so that the program looks for values in the specified
396-
environment variables.
397-
398-
This is an example of a Pod that uses a Secret via environment variables:
399-
400-
```yaml
401-
apiVersion: v1
402-
kind: Pod
403-
metadata:
404-
name: secret-env-pod
405-
spec:
406-
containers:
407-
- name: mycontainer
408-
image: redis
409-
env:
410-
- name: SECRET_USERNAME
411-
valueFrom:
412-
secretKeyRef:
413-
name: mysecret
414-
key: username
415-
optional: false # same as default; "mysecret" must exist
416-
# and include a key named "username"
417-
- name: SECRET_PASSWORD
418-
valueFrom:
419-
secretKeyRef:
420-
name: mysecret
421-
key: password
422-
optional: false # same as default; "mysecret" must exist
423-
# and include a key named "password"
424-
restartPolicy: Never
425-
```
255+
1. For each container in your Pod specification, add an environment variable
256+
for each Secret key that you want to use to the
257+
`env[].valueFrom.secretKeyRef` field.
258+
1. Modify your image and/or command line so that the program looks for values
259+
in the specified environment variables.
426260

261+
For instructions, refer to
262+
[Define container environment variables using Secret data](/docs/tasks/inject-data-application/distribute-credentials-secure/#define-container-environment-variables-using-secret-data).
427263

428264
#### Invalid environment variables {#restriction-env-from-invalid}
429265

430-
Secrets used to populate environment variables by the `envFrom` field that have keys
431-
that are considered invalid environment variable names will have those keys
432-
skipped. The Pod is allowed to start.
266+
If your environment variable definitions in your Pod specification are
267+
considered to be invalid environment variable names, those keys aren't made
268+
available to your container. The Pod is allowed to start.
433269

434-
If you define a Pod with an invalid variable name, the failed Pod startup includes
435-
an event with the reason set to `InvalidVariableNames` and a message that lists the
436-
skipped invalid keys. The following example shows a Pod that refers to a Secret
437-
named `mysecret`, where `mysecret` contains 2 invalid keys: `1badkey` and `2alsobad`.
270+
Kubernetes adds an Event with the reason set to `InvalidVariableNames` and a
271+
message that lists the skipped invalid keys. The following example shows a Pod that refers to a Secret named `mysecret`, where `mysecret` contains 2 invalid keys: `1badkey` and `2alsobad`.
438272

439273
```shell
440274
kubectl get events
@@ -447,42 +281,6 @@ LASTSEEN FIRSTSEEN COUNT NAME KIND SUBOBJECT
447281
0s 0s 1 dapi-test-pod Pod Warning InvalidEnvironmentVariableNames kubelet, 127.0.0.1 Keys [1badkey, 2alsobad] from the EnvFrom secret default/mysecret were skipped since they are considered invalid environment variable names.
448282
```
449283

450-
451-
#### Consuming Secret values from environment variables
452-
453-
Inside a container that consumes a Secret using environment variables, the secret keys appear
454-
as normal environment variables. The values of those variables are the base64 decoded values
455-
of the secret data.
456-
457-
This is the result of commands executed inside the container from the example above:
458-
459-
```shell
460-
echo "$SECRET_USERNAME"
461-
```
462-
463-
The output is similar to:
464-
465-
```
466-
admin
467-
```
468-
469-
```shell
470-
echo "$SECRET_PASSWORD"
471-
```
472-
473-
The output is similar to:
474-
475-
```
476-
1f2d1e2e67df
477-
```
478-
479-
{{< note >}}
480-
If a container already consumes a Secret in an environment variable,
481-
a Secret update will not be seen by the container unless it is
482-
restarted. There are third party solutions for triggering restarts when
483-
secrets change.
484-
{{< /note >}}
485-
486284
### Container image pull secrets {#using-imagepullsecrets}
487285

488286
If you want to fetch container images from a private repository, you need a way for
@@ -518,43 +316,10 @@ You cannot use ConfigMaps or Secrets with {{< glossary_tooltip text="static Pods
518316

519317
## Use cases
520318

521-
### Use case: As container environment variables
522-
523-
Create a secret
524-
```yaml
525-
apiVersion: v1
526-
kind: Secret
527-
metadata:
528-
name: mysecret
529-
type: Opaque
530-
data:
531-
USER_NAME: YWRtaW4=
532-
PASSWORD: MWYyZDFlMmU2N2Rm
533-
```
534-
535-
Create the Secret:
536-
```shell
537-
kubectl apply -f mysecret.yaml
538-
```
319+
### Use case: As container environment variables {#use-case-as-container-environment-variables}
539320

540-
Use `envFrom` to define all of the Secret's data as container environment variables. The key from
541-
the Secret becomes the environment variable name in the Pod.
542-
543-
```yaml
544-
apiVersion: v1
545-
kind: Pod
546-
metadata:
547-
name: secret-test-pod
548-
spec:
549-
containers:
550-
- name: test-container
551-
image: registry.k8s.io/busybox
552-
command: [ "/bin/sh", "-c", "env" ]
553-
envFrom:
554-
- secretRef:
555-
name: mysecret
556-
restartPolicy: Never
557-
```
321+
You can create a Secret and use it to
322+
[set environment variables for a container](/docs/tasks/inject-data-application/distribute-credentials-secure/#define-container-environment-variables-using-secret-data).
558323

559324
### Use case: Pod with SSH keys
560325

0 commit comments

Comments
 (0)