Skip to content

Commit a1e4da1

Browse files
authored
Merge pull request #42478 from windsonsea/cresec
[zh] sync distribute-credentials-secure.md
2 parents 0847b07 + 4d665a2 commit a1e4da1

File tree

1 file changed

+205
-19
lines changed

1 file changed

+205
-19
lines changed

content/zh-cn/docs/tasks/inject-data-application/distribute-credentials-secure.md

Lines changed: 205 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
---
2-
title: 使用 Secret 安全地分发凭证
2+
title: 使用 Secret 安全地分发凭据
33
content_type: task
44
weight: 50
55
min-kubernetes-server-version: v1.6
66
---
7+
<!--
8+
title: Distribute Credentials Securely Using Secrets
9+
content_type: task
10+
weight: 50
11+
min-kubernetes-server-version: v1.6
12+
-->
713

814
<!-- overview -->
915
<!--
@@ -59,7 +65,7 @@ username and password:
5965

6066
这里是一个配置文件,可以用来创建存有用户名和密码的 Secret:
6167

62-
{{< codenew file="pods/inject/secret.yaml" >}}
68+
{{% code file="pods/inject/secret.yaml" %}}
6369

6470
<!--
6571
1. Create the Secret
@@ -148,7 +154,7 @@ Here is a configuration file you can use to create a Pod:
148154

149155
这里是一个可以用来创建 Pod 的配置文件:
150156

151-
{{< codenew file="pods/inject/secret-pod.yaml" >}}
157+
{{% code file="pods/inject/secret-pod.yaml" %}}
152158

153159
1. <!-- Create the Pod:-->
154160
创建 Pod:
@@ -233,8 +239,8 @@ Secret `data` 映射中的每个键都成为该目录中的文件名。
233239
<!--
234240
### Project Secret keys to specific file paths
235241
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:
238244
-->
239245
### 映射 Secret 键到特定文件路径 {#project-secret-keys-to-specific-file-paths}
240246

@@ -392,7 +398,7 @@ secrets change.
392398
-->
393399
- 在 Pod 规约中,将 Secret 中定义的值 `backend-username` 赋给 `SECRET_USERNAME` 环境变量。
394400

395-
{{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" >}}
401+
{{% code file="pods/inject/pod-single-secret-env-variable.yaml" %}}
396402

397403
<!--
398404
- Create the Pod:
@@ -404,18 +410,19 @@ secrets change.
404410
```
405411

406412
<!--
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.
408414
-->
409415
- 在 Shell 中,显示容器环境变量 `SECRET_USERNAME` 的内容:
410416

411417
```shell
412418
kubectl exec -i -t env-single-secret -- /bin/sh -c 'echo $SECRET_USERNAME'
413-
```
419+
```
414420

415421
<!--
416-
The output is
422+
The output is similar to:
417423
-->
418-
输出为:
424+
输出类似于:
425+
419426
```
420427
backend-admin
421428
```
@@ -440,7 +447,7 @@ secrets change.
440447
-->
441448
- 在 Pod 规约中定义环境变量:
442449

443-
{{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" >}}
450+
{{% code file="pods/inject/pod-multiple-secret-env-variable.yaml" %}}
444451

445452
<!--
446453
- Create the Pod:
@@ -452,17 +459,19 @@ secrets change.
452459
```
453460

454461
<!--
455-
- In your shell, display the container environment variables
462+
- In your shell, display the container environment variables.
456463
-->
457464
- 在你的 Shell 中,显示容器环境变量的内容:
458465

459466
```shell
460467
kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c 'env | grep _USERNAME'
461468
```
469+
462470
<!--
463-
The output is
471+
The output is similar to:
464472
-->
465-
输出:
473+
输出类似于:
474+
466475
```
467476
DB_USERNAME=db-admin
468477
BACKEND_USERNAME=backend-admin
@@ -490,12 +499,13 @@ This functionality is available in Kubernetes v1.6 and later.
490499
```
491500

492501
<!--
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.
494504
-->
495505
- 使用 `envFrom` 来将 Secret 中的所有数据定义为环境变量。
496506
Secret 中的键名成为容器中的环境变量名:
497507

498-
{{< codenew file="pods/inject/pod-secret-envFrom.yaml" >}}
508+
{{% code file="pods/inject/pod-secret-envFrom.yaml" %}}
499509

500510
<!--
501511
- Create the Pod:
@@ -507,7 +517,7 @@ This functionality is available in Kubernetes v1.6 and later.
507517
```
508518

509519
<!--
510-
- In your shell, display `username` and `password` container environment variables
520+
- In your shell, display `username` and `password` container environment variables.
511521
-->
512522
- 在 Shell 中,显示环境变量 `username``password` 的内容:
513523

@@ -516,15 +526,191 @@ This functionality is available in Kubernetes v1.6 and later.
516526
```
517527

518528
<!--
519-
The output is
529+
The output is similar to:
520530
-->
521-
输出为
531+
输出类似于
522532

523533
```
524534
username: my-app
525535
password: 39528$vdg7Jb
526536
```
527537

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+
528714
<!--
529715
### References
530716
-->

0 commit comments

Comments
 (0)