Skip to content

Commit 81aa4c8

Browse files
Merge pull request kubernetes#478 from ingvagabund/extend-secrets-with-key2path-and-atomic-updates
Extend secrets docs with key to path mapping and automatic updates
2 parents 06b9604 + 3589ddb commit 81aa4c8

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

docs/user-guide/secrets/index.md

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,53 @@ You can package many files into one secret, or use many secrets, whichever is co
214214

215215
See another example of creating a secret and a pod that consumes that secret in a volume [here](/docs/user-guide/secrets/).
216216

217-
##### Consuming Secret Values from Volumes
217+
**Projection of secret keys to specific paths**
218+
219+
We can also control the paths within the volume where Secret keys are projected.
220+
You can use `spec.volumes[].secret.items` field to change target path of each key:
221+
222+
```json
223+
{
224+
"apiVersion": "v1",
225+
"kind": "Pod",
226+
"metadata": {
227+
"name": "mypod",
228+
"namespace": "myns"
229+
},
230+
"spec": {
231+
"containers": [{
232+
"name": "mypod",
233+
"image": "redis",
234+
"volumeMounts": [{
235+
"name": "foo",
236+
"mountPath": "/etc/foo",
237+
"readOnly": true
238+
}]
239+
}],
240+
"volumes": [{
241+
"name": "foo",
242+
"secret": {
243+
"secretName": "mysecret",
244+
"items": [{
245+
"key": "username",
246+
"path": "my-group/my-username"
247+
}]
248+
}
249+
}]
250+
}
251+
}
252+
```
253+
254+
What will happen:
255+
256+
* `username` secret is stored under `/etc/foo/my-group/my-username` file instead of `/etc/foo/username`.
257+
* `password` secret is not projected
258+
259+
If `spec.volumes[].secret.items` is used, only keys specified in `items` are projected.
260+
To consume all keys from the secret, all of them must be listed in the `items` field.
261+
All listed keys must exist in the corresponding secret. Otherwise, the volume is not created.
262+
263+
**Consuming Secret Values from Volumes**
218264

219265
Inside the container that mounts a secret volume, the secret keys appear as
220266
files and the secret values are base-64 decoded and stored inside these files.
@@ -234,6 +280,11 @@ $ cat /etc/foo/password
234280
The program in a container is responsible for reading the secret(s) from the
235281
files.
236282

283+
**Mounted Secrets are updated automatically**
284+
285+
When a secret being already consumed in a volume is updated, projected keys are eventually updated as well.
286+
The update time depends on the kubelet syncing period.
287+
237288
#### Using Secrets as Environment Variables
238289

239290
To use a secret in an environment variable in a pod:
@@ -267,7 +318,7 @@ spec:
267318
restartPolicy: Never
268319
```
269320

270-
##### Consuming Secret Values from Environment Variables
321+
**Consuming Secret Values from Environment Variables**
271322

272323
Inside a container that consumes a secret in an environment variables, the secret keys appear as
273324
normal environment variables containing the base-64 decoded values of the secret data.
@@ -285,7 +336,7 @@ $ echo $SECRET_PASSWORD
285336
An imagePullSecret is a way to pass a secret that contains a Docker (or other) image registry
286337
password to the Kubelet so it can pull a private image on behalf of your Pod.
287338

288-
##### Manually specifying an imagePullSecret
339+
**Manually specifying an imagePullSecret**
289340

290341
Use of imagePullSecrets is described in the [images documentation](/docs/user-guide/images/#specifying-imagepullsecrets-on-a-pod)
291342

@@ -338,23 +389,6 @@ reason it is not started yet. Once the secret is fetched, the kubelet will
338389
create and mount a volume containing it. None of the pod's containers will
339390
start until all the pod's volumes are mounted.
340391

341-
Once the kubelet has started a pod's containers, its secret volumes will not
342-
change, even if the secret resource is modified. To change the secret used,
343-
the original pod must be deleted, and a new pod (perhaps with an identical
344-
`PodSpec`) must be created. Therefore, updating a secret follows the same
345-
workflow as deploying a new container image. The `kubectl rolling-update`
346-
command can be used ([man page](/docs/user-guide/kubectl/kubectl_rolling-update)).
347-
348-
The [`resourceVersion`](https://github.com/kubernetes/kubernetes/tree/{{page.githubbranch}}/docs/devel/api-conventions.md#concurrency-control-and-consistency)
349-
of the secret is not specified when it is referenced.
350-
Therefore, if a secret is updated at about the same time as pods are starting,
351-
then it is not defined which version of the secret will be used for the pod. It
352-
is not possible currently to check what resource version of a secret object was
353-
used when a pod was created. It is planned that pods will report this
354-
information, so that a replication controller restarts ones using an old
355-
`resourceVersion`. In the interim, if this is a concern, it is recommended to not
356-
update the data of existing secrets, but to create new ones with distinct names.
357-
358392
## Use cases
359393

360394
### Use-Case: Pod with ssh keys

0 commit comments

Comments
 (0)