@@ -83,7 +83,7 @@ username and password:
83
83
Output:
84
84
-->
85
85
输出:
86
-
86
+
87
87
```
88
88
NAME TYPE DATA AGE
89
89
test-secret Opaque 2 1m
@@ -222,59 +222,204 @@ Here is a configuration file you can use to create a Pod:
222
222
my-app
223
223
39528$vdg7Jb
224
224
```
225
+ <!--
226
+ Modify your image or command line so that the program looks for files in the
227
+ `mountPath` directory. Each key in the Secret `data` map becomes a file name
228
+ in this directory.
229
+ -->
230
+ 修改你的镜像或命令行,使程序在 ` mountPath ` 目录下查找文件。
231
+ Secret ` data ` 映射中的每个键都成为该目录中的文件名。
225
232
226
233
<!--
227
- ## Define container environment variables using Secret data
234
+ ### Project Secret keys to specific file paths
228
235
229
- ### Define a container environment variable with data from a single Secret
236
+ You can also control the paths within the volume where Secret keys are projected. Use the `.spec.volumes[].secret.items` field to change the target
237
+ path of each key:
238
+ -->
239
+ ### 映射 Secret 键到特定文件路径 {#project-secret-keys-to-specific-file-paths}
240
+
241
+ 你还可以控制卷内 Secret 键的映射路径。
242
+ 使用 ` .spec.volumes[].secret.items ` 字段来改变每个键的目标路径。
243
+
244
+ ``` yaml
245
+ apiVersion : v1
246
+ kind : Pod
247
+ metadata :
248
+ name : mypod
249
+ spec :
250
+ containers :
251
+ - name : mypod
252
+ image : redis
253
+ volumeMounts :
254
+ - name : foo
255
+ mountPath : " /etc/foo"
256
+ readOnly : true
257
+ volumes :
258
+ - name : foo
259
+ secret :
260
+ secretName : mysecret
261
+ items :
262
+ - key : username
263
+ path : my-group/my-username
264
+ ` ` `
265
+
266
+ <!--
267
+ When you deploy this Pod, the following happens:
268
+ -->
269
+ 当你部署此 Pod 时,会发生以下情况:
270
+
271
+ <!--
272
+ * The ` username` key from `mysecret` is available to the container at the path
273
+ ` /etc/foo/my-group/my-username` instead of at `/etc/foo/username`.
274
+ * The `password` key from that Secret object is not projected.
275
+ -->
276
+ * 来自 `mysecret` 的键 `username` 可以在路径 `/etc/foo/my-group/my-username`
277
+ 下供容器使用,而不是路径 `/etc/foo/username`。
278
+ * 来自该 Secret 的键 `password` 没有映射到任何路径。
279
+
280
+ <!--
281
+ If you list keys explicitly using `.spec.volumes[].secret.items`, consider the
282
+ following :
283
+ -->
284
+
285
+ 如果你使用 `.spec.volumes[].secret.items` 明确地列出键,请考虑以下事项:
286
+
287
+ <!--
288
+ * Only keys specified in `items` are projected.
289
+ * To consume all keys from the Secret, all of them must be listed in the
290
+ ` items` field.
291
+ * All listed keys must exist in the corresponding Secret. Otherwise, the volume
292
+ is not created.
293
+ -->
294
+ * 只有在 `items` 字段中指定的键才会被映射。
295
+ * 要使用 Secret 中全部的键,那么全部的键都必须列在 `items` 字段中。
296
+ * 所有列出的键必须存在于相应的 Secret 中。否则,该卷不被创建。
297
+
298
+ <!--
299
+ # ## Set POSIX permissions for Secret keys
300
+
301
+ You can set the POSIX file access permission bits for a single Secret key.
302
+ If you don't specify any permissions, `0644` is used by default.
303
+ You can also set a default POSIX file mode for the entire Secret volume, and
304
+ you can override per key if needed.
305
+ -->
306
+ # ## 为 Secret 键设置 POSIX 权限
307
+
308
+ 你可以为单个 Secret 键设置 POSIX 文件访问权限位。
309
+ 如果不指定任何权限,默认情况下使用 `0644`。
310
+ 你也可以为整个 Secret 卷设置默认的 POSIX 文件模式,需要时你可以重写单个键的权限。
230
311
312
+ <!--
313
+ For example, you can specify a default mode like this :
314
+ -->
315
+ 例如,可以像这样指定默认模式:
316
+
317
+ ` ` ` yaml
318
+ apiVersion: v1
319
+ kind: Pod
320
+ metadata:
321
+ name: mypod
322
+ spec:
323
+ containers:
324
+ - name: mypod
325
+ image: redis
326
+ volumeMounts:
327
+ - name: foo
328
+ mountPath: "/etc/foo"
329
+ volumes:
330
+ - name: foo
331
+ secret:
332
+ secretName: mysecret
333
+ defaultMode: 0400
334
+ ` ` `
335
+
336
+ <!--
337
+ The Secret is mounted on `/etc/foo`; all the files created by the
338
+ secret volume mount have permission `0400`.
339
+ -->
340
+ Secret 被挂载在 `/etc/foo` 目录下;所有由 Secret 卷挂载创建的文件的访问许可都是 `0400`。
341
+
342
+ {{< note >}}
343
+ <!--
344
+ If you're defining a Pod or a Pod template using JSON, beware that the JSON
345
+ specification doesn't support octal literals for numbers because JSON considers
346
+ ` 0400` to be the _decimal_ value `400`. In JSON, use decimal values for the
347
+ ` defaultMode` instead. If you're writing YAML, you can write the `defaultMode`
348
+ in octal.
349
+ -->
350
+ 如果使用 JSON 定义 Pod 或 Pod 模板,请注意 JSON 规范不支持数字的八进制形式,
351
+ 因为 JSON 将 `0400` 视为**十进制**的值 `400`。
352
+ 在 JSON 中,要改为使用十进制的 `defaultMode`。
353
+ 如果你正在编写 YAML,则可以用八进制编写 `defaultMode`。
354
+ {{< /note >}}
355
+
356
+ <!--
357
+ # # Define container environment variables using Secret data
231
358
-->
232
359
# # 使用 Secret 数据定义容器变量 {#define-container-env-var-using-secret-data}
233
360
361
+ <!--
362
+ You can consume the data in Secrets as environment variables in your
363
+ containers.
364
+
365
+ If a container already consumes a Secret in an environment variable,
366
+ a Secret update will not be seen by the container unless it is
367
+ restarted. There are third party solutions for triggering restarts when
368
+ secrets change.
369
+ -->
370
+ 在你的容器中,你可以以环境变量的方式使用 Secret 中的数据。
371
+
372
+ 如果容器已经使用了在环境变量中的 Secret,除非容器重新启动,否则容器将无法感知到 Secret 的更新。
373
+ 有第三方解决方案可以在 Secret 改变时触发容器重启。
374
+
375
+ <!--
376
+ # ## Define a container environment variable with data from a single Secret
377
+ -->
378
+
234
379
# ## 使用来自 Secret 中的数据定义容器变量 {#define-a-container-env-var-with-data-from-a-single-secret}
235
380
236
381
<!--
237
- * Define an environment variable as a key-value pair in a Secret:
382
+ * Define an environment variable as a key-value pair in a Secret:
238
383
-->
239
- * 定义环境变量为 Secret 中的键值偶对:
384
+ * 定义环境变量为 Secret 中的键值偶对:
240
385
241
- ``` shell
242
- kubectl create secret generic backend-user --from-literal=backend-username=' backend-admin'
243
- ```
386
+ ` ` ` shell
387
+ kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
388
+ ` ` `
244
389
245
390
<!--
246
391
* Assign the `backend-username` value defined in the Secret to the `SECRET_USERNAME` environment variable in the Pod specification.
247
392
-->
248
- * 在 Pod 规约中,将 Secret 中定义的值 ` backend-username ` 赋给 ` SECRET_USERNAME ` 环境变量。
393
+ * 在 Pod 规约中,将 Secret 中定义的值 `backend-username` 赋给 `SECRET_USERNAME` 环境变量。
249
394
250
- {{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" >}}
395
+ {{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" >}}
251
396
252
397
<!--
253
398
* Create the Pod:
254
399
-->
255
- * 创建 Pod:
400
+ * 创建 Pod:
256
401
257
- ``` shell
258
- kubectl create -f https://k8s.io/examples/pods/inject/pod-single-secret-env-variable.yaml
259
- ```
402
+ ` ` ` shell
403
+ kubectl create -f https://k8s.io/examples/pods/inject/pod-single-secret-env-variable.yaml
404
+ ` ` `
260
405
261
406
<!--
262
407
* In your shell, display the content of `SECRET_USERNAME` container environment variable
263
408
-->
264
- * 在 Shell 中,显示容器环境变量 ` SECRET_USERNAME ` 的内容:
409
+ * 在 Shell 中,显示容器环境变量 `SECRET_USERNAME` 的内容:
265
410
266
- ``` shell
267
- kubectl exec -i -t env-single-secret -- /bin/sh -c ' echo $SECRET_USERNAME'
411
+ ` ` ` shell
412
+ kubectl exec -i -t env-single-secret -- /bin/sh -c 'echo $SECRET_USERNAME'
268
413
` ` `
269
414
270
- <!--
271
- The output is
272
- -->
273
- 输出为:
274
- ```
275
- backend-admin
276
- ```
277
-
415
+ <!--
416
+ The output is
417
+ -->
418
+ 输出为:
419
+ ```
420
+ backend-admin
421
+ ```
422
+
278
423
<!--
279
424
### Define container environment variables with data from multiple Secrets
280
425
-->
@@ -283,45 +428,45 @@ Here is a configuration file you can use to create a Pod:
283
428
<!--
284
429
* As with the previous example, create the Secrets first.
285
430
-->
286
- * 和前面的例子一样,先创建 Secret:
431
+ * 和前面的例子一样,先创建 Secret:
287
432
288
- ``` shell
289
- kubectl create secret generic backend-user --from-literal=backend-username=' backend-admin'
290
- kubectl create secret generic db-user --from-literal=db-username=' db-admin'
291
- ```
433
+ ```shell
434
+ kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
435
+ kubectl create secret generic db-user --from-literal=db-username='db-admin'
436
+ ```
292
437
293
438
<!--
294
439
* Define the environment variables in the Pod specification.
295
440
-->
296
- * 在 Pod 规约中定义环境变量:
441
+ * 在 Pod 规约中定义环境变量:
297
442
298
- {{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" >}}
443
+ {{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" >}}
299
444
300
445
<!--
301
446
* Create the Pod:
302
447
-->
303
- * 创建 Pod:
448
+ * 创建 Pod:
304
449
305
- ``` shell
306
- kubectl create -f https://k8s.io/examples/pods/inject/pod-multiple-secret-env-variable.yaml
307
- ```
450
+ ``` shell
451
+ kubectl create -f https://k8s.io/examples/pods/inject/pod-multiple-secret-env-variable.yaml
452
+ ```
308
453
309
454
<!--
310
455
* In your shell, display the container environment variables
311
456
-->
312
- * 在你的 Shell 中,显示容器环境变量的内容:
457
+ * 在你的 Shell 中,显示容器环境变量的内容:
313
458
314
- ``` shell
315
- kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c ' env | grep _USERNAME'
316
- ```
317
- <!--
318
- The output is
319
- -->
320
- 输出:
321
- ```
322
- DB_USERNAME=db-admin
323
- BACKEND_USERNAME=backend-admin
324
- ```
459
+ ``` shell
460
+ kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c ' env | grep _USERNAME'
461
+ ```
462
+ <!--
463
+ The output is
464
+ -->
465
+ 输出:
466
+ ```
467
+ DB_USERNAME=db-admin
468
+ BACKEND_USERNAME=backend-admin
469
+ ```
325
470
326
471
<!--
327
472
## Configure all key-value pairs in a Secret as container environment variables
@@ -338,28 +483,28 @@ This functionality is available in Kubernetes v1.6 and later.
338
483
<!--
339
484
* Create a Secret containing multiple key-value pairs
340
485
-->
341
- * 创建包含多个键值偶对的 Secret:
486
+ * 创建包含多个键值偶对的 Secret:
342
487
343
- ``` shell
344
- kubectl create secret generic test-secret --from-literal=username=' my-app' --from-literal=password=' 39528$vdg7Jb'
345
- ```
488
+ ``` shell
489
+ kubectl create secret generic test-secret --from-literal=username=' my-app' --from-literal=password=' 39528$vdg7Jb'
490
+ ```
346
491
347
492
<!--
348
493
* 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.
349
494
-->
350
- * 使用 ` envFrom ` 来将 Secret 中的所有数据定义为环境变量。
351
- Secret 中的键名成为容器中的环境变量名:
495
+ * 使用 ` envFrom ` 来将 Secret 中的所有数据定义为环境变量。
496
+ Secret 中的键名成为容器中的环境变量名:
352
497
353
- {{< codenew file="pods/inject/pod-secret-envFrom.yaml" >}}
498
+ {{< codenew file="pods/inject/pod-secret-envFrom.yaml" >}}
354
499
355
500
<!--
356
501
* Create the Pod:
357
502
-->
358
- * 创建 Pod:
503
+ * 创建 Pod:
359
504
360
- ``` shell
361
- kubectl create -f https://k8s.io/examples/pods/inject/pod-secret-envFrom.yaml
362
- ```
505
+ ``` shell
506
+ kubectl create -f https://k8s.io/examples/pods/inject/pod-secret-envFrom.yaml
507
+ ```
363
508
364
509
<!--
365
510
* In your shell, display `username` and `password` container environment variables
@@ -397,4 +542,3 @@ This functionality is available in Kubernetes v1.6 and later.
397
542
-->
398
543
* 进一步了解 [ Secret] ( /zh-cn/docs/concepts/configuration/secret/ ) 。
399
544
* 了解[ 卷] ( /zh-cn/docs/concepts/storage/volumes/ ) 。
400
-
0 commit comments