@@ -6,13 +6,12 @@ min-kubernetes-server-version: v1.6
6
6
---
7
7
8
8
<!-- overview -->
9
+
9
10
This page shows how to securely inject sensitive data, such as passwords and
10
11
encryption keys, into Pods.
11
12
12
-
13
13
## {{% heading "prerequisites" %}}
14
14
15
-
16
15
{{< include "task-tutorial-prereqs.md" >}}
17
16
18
17
### Convert your secret data to a base-64 representation
@@ -94,7 +93,6 @@ kubectl create secret generic test-secret --from-literal='username=my-app' --fro
94
93
This is more convenient. The detailed approach shown earlier runs
95
94
through each step explicitly to demonstrate what is happening.
96
95
97
-
98
96
## Create a Pod that has access to the secret data through a Volume
99
97
100
98
Here is a configuration file you can use to create a Pod:
@@ -125,7 +123,7 @@ Here is a configuration file you can use to create a Pod:
125
123
```
126
124
127
125
1 . The secret data is exposed to the Container through a Volume mounted under
128
- ` /etc/secret-volume ` .
126
+ ` /etc/secret-volume ` .
129
127
130
128
In your shell, list the files in the ` /etc/secret-volume ` directory:
131
129
``` shell
@@ -182,17 +180,17 @@ spec:
182
180
183
181
When you deploy this Pod, the following happens:
184
182
185
- * The ` username` key from `mysecret` is available to the container at the path
183
+ - The ` username` key from `mysecret` is available to the container at the path
186
184
` /etc/foo/my-group/my-username` instead of at `/etc/foo/username`.
187
- * The `password` key from that Secret object is not projected.
185
+ - The `password` key from that Secret object is not projected.
188
186
189
187
If you list keys explicitly using `.spec.volumes[].secret.items`, consider the
190
188
following :
191
189
192
- * Only keys specified in `items` are projected.
193
- * To consume all keys from the Secret, all of them must be listed in the
190
+ - Only keys specified in `items` are projected.
191
+ - To consume all keys from the Secret, all of them must be listed in the
194
192
` items` field.
195
- * All listed keys must exist in the corresponding Secret. Otherwise, the volume
193
+ - All listed keys must exist in the corresponding Secret. Otherwise, the volume
196
194
is not created.
197
195
198
196
# ## Set POSIX permissions for Secret keys
@@ -246,87 +244,86 @@ secrets change.
246
244
247
245
# ## Define a container environment variable with data from a single Secret
248
246
249
- * Define an environment variable as a key-value pair in a Secret:
247
+ - Define an environment variable as a key-value pair in a Secret :
250
248
251
- ` ` ` shell
252
- kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
253
- ` ` `
249
+ ` ` ` shell
250
+ kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
251
+ ` ` `
254
252
255
- * Assign the `backend-username` value defined in the Secret to the `SECRET_USERNAME` environment variable in the Pod specification.
253
+ - Assign the `backend-username` value defined in the Secret to the `SECRET_USERNAME` environment variable in the Pod specification.
256
254
257
- {{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" >}}
255
+ {{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" >}}
258
256
259
- * Create the Pod:
257
+ - Create the Pod :
260
258
261
- ` ` ` shell
262
- kubectl create -f https://k8s.io/examples/pods/inject/pod-single-secret-env-variable.yaml
263
- ` ` `
259
+ ` ` ` shell
260
+ kubectl create -f https://k8s.io/examples/pods/inject/pod-single-secret-env-variable.yaml
261
+ ` ` `
264
262
265
- * In your shell, display the content of `SECRET_USERNAME` container environment variable
263
+ - In your shell, display the content of `SECRET_USERNAME` container environment variable
266
264
267
- ` ` ` shell
268
- kubectl exec -i -t env-single-secret -- /bin/sh -c 'echo $SECRET_USERNAME'
269
- ` ` `
265
+ ` ` ` shell
266
+ kubectl exec -i -t env-single-secret -- /bin/sh -c 'echo $SECRET_USERNAME'
267
+ ` ` `
270
268
271
- The output is
272
- ```
273
- backend-admin
274
- ```
269
+ The output is
270
+ ` ` `
271
+ backend-admin
272
+ ` ` `
275
273
276
274
# ## Define container environment variables with data from multiple Secrets
277
275
278
- * As with the previous example, create the Secrets first.
279
-
280
- ```shell
281
- kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
282
- kubectl create secret generic db-user --from-literal=db-username='db-admin'
283
- ```
276
+ - As with the previous example, create the Secrets first.
284
277
285
- * Define the environment variables in the Pod specification.
278
+ ` ` ` shell
279
+ kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
280
+ kubectl create secret generic db-user --from-literal=db-username='db-admin'
281
+ ` ` `
286
282
287
- {{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" >}}
283
+ - Define the environment variables in the Pod specification.
288
284
289
- * Create the Pod:
285
+ {{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" >}}
290
286
291
- ``` shell
292
- kubectl create -f https://k8s.io/examples/pods/inject/pod-multiple-secret-env-variable.yaml
293
- ```
287
+ - Create the Pod :
294
288
295
- * In your shell, display the container environment variables
289
+ ` ` ` shell
290
+ kubectl create -f https://k8s.io/examples/pods/inject/pod-multiple-secret-env-variable.yaml
291
+ ` ` `
296
292
297
- ``` shell
298
- kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c ' env | grep _USERNAME'
299
- ```
300
- The output is
301
- ```
302
- DB_USERNAME=db-admin
303
- BACKEND_USERNAME=backend-admin
304
- ```
293
+ - In your shell, display the container environment variables
305
294
295
+ ` ` ` shell
296
+ kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c 'env | grep _USERNAME'
297
+ ` ` `
298
+ The output is
299
+ ` ` `
300
+ DB_USERNAME=db-admin
301
+ BACKEND_USERNAME=backend-admin
302
+ ` ` `
306
303
307
304
# # Configure all key-value pairs in a Secret as container environment variables
308
305
309
306
{{< note >}}
310
307
This functionality is available in Kubernetes v1.6 and later.
311
308
{{< /note >}}
312
309
313
- * Create a Secret containing multiple key-value pairs
310
+ - Create a Secret containing multiple key-value pairs
314
311
315
- ``` shell
316
- kubectl create secret generic test-secret --from-literal=username=' my-app' --from-literal=password=' 39528$vdg7Jb'
317
- ```
312
+ ` ` ` shell
313
+ kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb'
314
+ ` ` `
318
315
319
- * Use envFrom to define all of the Secret's data as container environment variables. The key from the Secret becomes the environment variable name in the Pod.
316
+ - Use envFrom to define all of the Secret's data as container environment variables. The key from the Secret becomes the environment variable name in the Pod.
320
317
321
- {{< codenew file="pods/inject/pod-secret-envFrom.yaml" >}}
318
+ {{< codenew file="pods/inject/pod-secret-envFrom.yaml" >}}
322
319
323
- * Create the Pod:
320
+ - Create the Pod :
324
321
325
- ``` shell
326
- kubectl create -f https://k8s.io/examples/pods/inject/pod-secret-envFrom.yaml
327
- ```
322
+ ` ` ` shell
323
+ kubectl create -f https://k8s.io/examples/pods/inject/pod-secret-envFrom.yaml
324
+ ` ` `
328
325
329
- * In your shell, display ` username ` and ` password ` container environment variables
326
+ - In your shell, display `username` and `password` container environment variables
330
327
331
328
` ` ` shell
332
329
kubectl exec -i -t envfrom-secret -- /bin/sh -c 'echo "username: $username\n password: $password\n "'
@@ -340,11 +337,11 @@ This functionality is available in Kubernetes v1.6 and later.
340
337
341
338
# ## References
342
339
343
- * [ Secret] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#secret-v1-core)
344
- * [ Volume] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
345
- * [ Pod] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#pod-v1-core)
340
+ - [Secret](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#secret-v1-core)
341
+ - [Volume](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
342
+ - [Pod](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#pod-v1-core)
346
343
347
344
# # {{% heading "whatsnext" %}}
348
345
349
- * Learn more about [ Secrets] ( /docs/concepts/configuration/secret/ ) .
350
- * Learn about [ Volumes] ( /docs/concepts/storage/volumes/ ) .
346
+ - Learn more about [Secrets](/docs/concepts/configuration/secret/).
347
+ - Learn about [Volumes](/docs/concepts/storage/volumes/).
0 commit comments