1
1
---
2
- title : 使用 Secret 安全地分发凭证
2
+ title : 使用 Secret 安全地分发凭据
3
3
content_type : task
4
4
weight : 50
5
5
min-kubernetes-server-version : v1.6
6
6
---
7
+ <!--
8
+ title: Distribute Credentials Securely Using Secrets
9
+ content_type: task
10
+ weight: 50
11
+ min-kubernetes-server-version: v1.6
12
+ -->
7
13
8
14
<!-- overview -->
9
15
<!--
@@ -59,7 +65,7 @@ username and password:
59
65
60
66
这里是一个配置文件,可以用来创建存有用户名和密码的 Secret:
61
67
62
- {{< codenew file="pods/inject/secret.yaml" > }}
68
+ {{% code file="pods/inject/secret.yaml" % }}
63
69
64
70
<!--
65
71
1. Create the Secret
@@ -148,7 +154,7 @@ Here is a configuration file you can use to create a Pod:
148
154
149
155
这里是一个可以用来创建 Pod 的配置文件:
150
156
151
- {{< codenew file="pods/inject/secret-pod.yaml" > }}
157
+ {{% code file="pods/inject/secret-pod.yaml" % }}
152
158
153
159
1 . <!-- Create the Pod:-->
154
160
创建 Pod:
@@ -233,8 +239,8 @@ Secret `data` 映射中的每个键都成为该目录中的文件名。
233
239
<!--
234
240
### Project Secret keys to specific file paths
235
241
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:
242
+ You can also control the paths within the volume where Secret keys are projected. Use the
243
+ `.spec.volumes[].secret.items` field to change the target path of each key:
238
244
-->
239
245
### 映射 Secret 键到特定文件路径 {#project-secret-keys-to-specific-file-paths}
240
246
@@ -392,7 +398,7 @@ secrets change.
392
398
-->
393
399
- 在 Pod 规约中,将 Secret 中定义的值 `backend-username` 赋给 `SECRET_USERNAME` 环境变量。
394
400
395
- {{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" > }}
401
+ {{% code file="pods/inject/pod-single-secret-env-variable.yaml" % }}
396
402
397
403
<!--
398
404
- Create the Pod :
@@ -404,18 +410,19 @@ secrets change.
404
410
` ` `
405
411
406
412
<!--
407
- - In your shell, display the content of `SECRET_USERNAME` container environment variable
413
+ - In your shell, display the content of `SECRET_USERNAME` container environment variable.
408
414
-->
409
415
- 在 Shell 中,显示容器环境变量 `SECRET_USERNAME` 的内容:
410
416
411
417
` ` ` shell
412
418
kubectl exec -i -t env-single-secret -- /bin/sh -c 'echo $SECRET_USERNAME'
413
- ` ` `
419
+ ` ` `
414
420
415
421
<!--
416
- The output is
422
+ The output is similar to :
417
423
-->
418
- 输出为:
424
+ 输出类似于:
425
+
419
426
```
420
427
backend-admin
421
428
```
@@ -440,7 +447,7 @@ secrets change.
440
447
-->
441
448
- 在 Pod 规约中定义环境变量:
442
449
443
- {{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" > }}
450
+ {{% code file="pods/inject/pod-multiple-secret-env-variable.yaml" % }}
444
451
445
452
<!--
446
453
- Create the Pod:
@@ -452,17 +459,19 @@ secrets change.
452
459
```
453
460
454
461
<!--
455
- - In your shell, display the container environment variables
462
+ - In your shell, display the container environment variables.
456
463
-->
457
464
- 在你的 Shell 中,显示容器环境变量的内容:
458
465
459
466
``` shell
460
467
kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c ' env | grep _USERNAME'
461
468
```
469
+
462
470
<!--
463
- The output is
471
+ The output is similar to:
464
472
-->
465
- 输出:
473
+ 输出类似于:
474
+
466
475
```
467
476
DB_USERNAME=db-admin
468
477
BACKEND_USERNAME=backend-admin
@@ -490,12 +499,13 @@ This functionality is available in Kubernetes v1.6 and later.
490
499
```
491
500
492
501
<!--
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.
502
+ - Use envFrom to define all of the Secret's data as container environment variables.
503
+ The key from the Secret becomes the environment variable name in the Pod.
494
504
-->
495
505
- 使用 ` envFrom ` 来将 Secret 中的所有数据定义为环境变量。
496
506
Secret 中的键名成为容器中的环境变量名:
497
507
498
- {{< codenew file="pods/inject/pod-secret-envFrom.yaml" > }}
508
+ {{% code file="pods/inject/pod-secret-envFrom.yaml" % }}
499
509
500
510
<!--
501
511
- Create the Pod:
@@ -507,7 +517,7 @@ This functionality is available in Kubernetes v1.6 and later.
507
517
```
508
518
509
519
<!--
510
- - In your shell, display `username` and `password` container environment variables
520
+ - In your shell, display `username` and `password` container environment variables.
511
521
-->
512
522
- 在 Shell 中,显示环境变量 ` username ` 和 ` password ` 的内容:
513
523
@@ -516,15 +526,191 @@ This functionality is available in Kubernetes v1.6 and later.
516
526
```
517
527
518
528
<!--
519
- The output is
529
+ The output is similar to:
520
530
-->
521
- 输出为 :
531
+ 输出类似于 :
522
532
523
533
```
524
534
username: my-app
525
535
password: 39528$vdg7Jb
526
536
```
527
537
538
+ <!--
539
+ ## Example: Provide prod/test credentials to Pods using Secrets {#provide-prod-test-creds}
540
+
541
+ This example illustrates a Pod which consumes a secret containing production credentials and
542
+ another Pod which consumes a secret with test environment credentials.
543
+ -->
544
+ ## 示例:使用 Secret 为 Pod 提供生产环境或测试环境的凭据 {#provide-prod-test-creds}
545
+
546
+ 此示例展示的是一个使用了包含生产环境凭据的 Secret 的 Pod 和一个使用了包含测试环境凭据的 Secret 的 Pod。
547
+
548
+ <!--
549
+ 1. Create a secret for prod environment credentials:
550
+ -->
551
+ 1 . 创建用于生产环境凭据的 Secret:
552
+
553
+ ``` shell
554
+ kubectl create secret generic prod-db-secret --from-literal=username=produser --from-literal=password=Y4nys7f11
555
+ ```
556
+
557
+ <!--
558
+ The output is similar to:
559
+ -->
560
+ 输出类似于:
561
+
562
+ ```
563
+ secret "prod-db-secret" created
564
+ ```
565
+
566
+ <!--
567
+ 1. Create a secret for test environment credentials.
568
+ -->
569
+ 2 . 为测试环境凭据创建 Secret。
570
+
571
+ ``` shell
572
+ kubectl create secret generic test-db-secret --from-literal=username=testuser --from-literal=password=iluvtests
573
+ ```
574
+
575
+ <!--
576
+ The output is similar to:
577
+ -->
578
+ 输出类似于:
579
+
580
+ ```
581
+ secret "test-db-secret" created
582
+ ```
583
+
584
+ {{< note >}}
585
+ <!--
586
+ Special characters such as `$`, `\`, `*`, `=`, and `!` will be interpreted by your
587
+ [shell](https://en.wikipedia.org/wiki/Shell_(computing)) and require escaping.
588
+
589
+ In most shells, the easiest way to escape the password is to surround it with single quotes (`'`).
590
+ For example, if your actual password is `S!B\*d$zDsb=`, you should execute the command as follows:
591
+ -->
592
+ ` $ ` 、` \ ` 、` * ` 、` = ` 和 ` ! ` 这类特殊字符会被你的 [ Shell] ( https://en.wikipedia.org/wiki/Shell_(computing) )
593
+ 解释,需要进行转义。
594
+
595
+ 在大多数 Shell 中,最简单的密码转义方法是使用单引号(` ' ` )将密码包起来。
596
+ 例如,如果你的实际密码是 ` S!B\*d$zDsb= ` ,则应执行以下命令:
597
+
598
+ ``` shell
599
+ kubectl create secret generic dev-db-secret --from-literal=username=devuser --from-literal=password=' S!B\*d$zDsb='
600
+ ```
601
+
602
+ <!--
603
+ You do not need to escape special characters in passwords from files (`--from-file`).
604
+ -->
605
+ 你无需转义来自文件(` --from-file ` )的密码中的特殊字符。
606
+ {{< /note >}}
607
+
608
+ <!--
609
+ 1. Create the Pod manifests:
610
+ -->
611
+ 3 . 创建 Pod 清单:
612
+
613
+ ``` shell
614
+ cat << EOF > pod.yaml
615
+ apiVersion: v1
616
+ kind: List
617
+ items:
618
+ - kind: Pod
619
+ apiVersion: v1
620
+ metadata:
621
+ name: prod-db-client-pod
622
+ labels:
623
+ name: prod-db-client
624
+ spec:
625
+ volumes:
626
+ - name: secret-volume
627
+ secret:
628
+ secretName: prod-db-secret
629
+ containers:
630
+ - name: db-client-container
631
+ image: myClientImage
632
+ volumeMounts:
633
+ - name: secret-volume
634
+ readOnly: true
635
+ mountPath: "/etc/secret-volume"
636
+ - kind: Pod
637
+ apiVersion: v1
638
+ metadata:
639
+ name: test-db-client-pod
640
+ labels:
641
+ name: test-db-client
642
+ spec:
643
+ volumes:
644
+ - name: secret-volume
645
+ secret:
646
+ secretName: test-db-secret
647
+ containers:
648
+ - name: db-client-container
649
+ image: myClientImage
650
+ volumeMounts:
651
+ - name: secret-volume
652
+ readOnly: true
653
+ mountPath: "/etc/secret-volume"
654
+ EOF
655
+ ` ` `
656
+
657
+ {{< note > }}
658
+ < ! --
659
+ How the specs for the two Pods differ only in one field; this facilitates creating Pods
660
+ with different capabilities from a common Pod template.
661
+ -->
662
+ 这两个 Pod 的规约只在一个字段上有所不同;这样便于从一个通用的 Pod 模板创建具有不同权能的 Pod。
663
+ {{< /note > }}
664
+
665
+ < ! --
666
+ 1. Apply all those objects on the API server by running:
667
+ -->
668
+ 4. 通过运行以下命令将所有这些对象应用到 API 服务器:
669
+
670
+ ` ` ` shell
671
+ kubectl create -f pod.yaml
672
+ ` ` `
673
+
674
+ < ! --
675
+ Both containers will have the following files present on their filesystems with the values
676
+ for each container' s environment:
677
+ -->
678
+ 两个容器的文件系统中都将存在以下文件,其中包含每个容器环境的值:
679
+
680
+ ```
681
+ /etc/secret-volume/username
682
+ /etc/secret-volume/password
683
+ ```
684
+
685
+ <!--
686
+ You could further simplify the base Pod specification by using two service accounts:
687
+
688
+ 1. `prod-user` with the `prod-db-secret`
689
+ 1. `test-user` with the `test-db-secret`
690
+
691
+ The Pod specification is shortened to:
692
+ -->
693
+ 你可以通过使用两个服务账号进一步简化基础 Pod 规约:
694
+
695
+ 1. 带有 `prod-db-secret` 的 `prod-user`
696
+ 1. 带有 `test-db-secret` 的 `test-user`
697
+
698
+ Pod 规约精简为:
699
+
700
+ ```yaml
701
+ apiVersion: v1
702
+ kind: Pod
703
+ metadata:
704
+ name: prod-db-client-pod
705
+ labels:
706
+ name: prod-db-client
707
+ spec:
708
+ serviceAccount: prod-db-client
709
+ containers:
710
+ - name: db-client-container
711
+ image: myClientImage
712
+ ```
713
+
528
714
<!--
529
715
### References
530
716
-->
0 commit comments