diff --git a/content/zh/docs/tasks/inject-data-application/distribute-credentials-secure.md b/content/zh/docs/tasks/inject-data-application/distribute-credentials-secure.md index ce0b85820d..6683178d1d 100644 --- a/content/zh/docs/tasks/inject-data-application/distribute-credentials-secure.md +++ b/content/zh/docs/tasks/inject-data-application/distribute-credentials-secure.md @@ -1,201 +1,367 @@ --- title: 使用 Secret 安全地分发凭证 content_type: task +weight: 50 +min-kubernetes-server-version: v1.6 --- + 本文展示如何安全地将敏感数据(如密码和加密密钥)注入到 Pods 中。 - ## {{% heading "prerequisites" %}} - -{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} +{{< include "task-tutorial-prereqs.md" >}} + +Suppose you want to have two pieces of secret data: a username `my-app` and a password +`39528$vdg7Jb`. First, use a base64 encoding tool to convert your username and password to a base64 representation. Here's an example using the commonly available base64 program: +--> +### 将 secret 数据转换为 base-64 形式 -## 将 secret 数据转换为 base-64 形式 +假设用户想要有两条 Secret 数据:用户名 `my-app` 和密码 `39528$vdg7Jb`。 +首先使用 [Base64 编码](https://www.base64encode.org/) 将用户名和密码转化为 base-64 形式。 +下面是一个使用常用的 base64 程序的示例: -假设用户想要有两条 secret 数据:用户名 `my-app` 和密码 -`39528$vdg7Jb`。 首先使用 [Base64 编码](https://www.base64encode.org/) 将用户名和密码转化为 base-64 形式。 这里是一个 Linux 示例: - - ```shell - echo -n 'my-app' | base64 - echo -n '39528$vdg7Jb' | base64 - ``` +```shell +echo -n 'my-app' | base64 +echo -n '39528$vdg7Jb' | base64 +``` + 结果显示 base-64 形式的用户名为 `bXktYXBw`, base-64 形式的密码为 `Mzk1MjgkdmRnN0pi`。 + +{{< caution >}} +使用你的操作系统所能信任的本地工具以降低使用外部工具的风险。 +{{< /caution >}} + + + + ## 创建 Secret 这里是一个配置文件,可以用来创建存有用户名和密码的 Secret: {{< codenew file="pods/inject/secret.yaml" >}} -1. 创建 Secret +1. + 创建 Secret: - ```shell - kubectl create -f https://k8s.io/examples/pods/inject/secret.yaml - ``` + ```shell + kubectl apply -f https://k8s.io/examples/pods/inject/secret.yaml + ``` - {{< note >}} - 如果想要跳过 Base64 编码的步骤,可以使用 `kubectl create secret` 命令来创建 Secret: - {{< /note >}} +1. + 查看 Secret 相关信息: - ```shell - kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb' - ``` - -1. 查看 Secret 相关信息: - - kubectl get secret test-secret + ```shell + kubectl get secret test-secret + ``` + 输出: - ```shell - NAME TYPE DATA AGE - test-secret Opaque 2 1m - ``` + ```shell + NAME TYPE DATA AGE + test-secret Opaque 2 1m + ``` -1. 查看 Secret 相关的更多详细信息: +1. + 查看 Secret 相关的更多详细信息: - kubectl describe secret test-secret + ```shell + kubectl describe secret test-secret + ``` - 输出: + + 输出: - ```shell - Name: test-secret - Namespace: default - Labels: - Annotations: + ```shell + Name: test-secret + Namespace: default + Labels: + Annotations: - Type: Opaque + Type: Opaque - Data - ==== - password: 13 bytes - username: 7 bytes - ``` + Data + ==== + password: 13 bytes + username: 7 bytes + ``` -## 创建可以通过卷访问 secret 数据的 Pod + +### 直接用 kubectl 创建 Secret + +如果你希望略过 Base64 编码的步骤,你也可以使用 `kubectl create secret` +命令直接创建 Secret。例如: + +```shell +kubectl create secret generic test-secret --from-literal='username=my-app' --from-literal='password=39528$vdg7Jb' +``` + + +这是一种更为方便的方法。 +前面展示的详细分解步骤有助于了解究竟发生了什么事情。 + + +## 创建一个可以通过卷访问 secret 数据的 Pod 这里是一个可以用来创建 pod 的配置文件: {{< codenew file="pods/inject/secret-pod.yaml" >}} -1. 创建 Pod: +1. + 创建 Pod: - ```shell - kubectl create -f secret-pod.yaml - ``` + ```shell + kubectl create -f secret-pod.yaml + ``` -1. 确认 Pod 正在运行: +1. + 确认 Pod 正在运行: - ```shell - kubectl get pod secret-test-pod - ``` - - 输出: + ```shell + kubectl get pod secret-test-pod + ``` - ```shell - NAME READY STATUS RESTARTS AGE - secret-test-pod 1/1 Running 0 42m - ``` - -1. 在 Pod 中运行的容器中获取一个 shell: + 输出: - ```shell - kubectl exec -it secret-test-pod -- /bin/bash - ``` + ```shell + NAME READY STATUS RESTARTS AGE + secret-test-pod 1/1 Running 0 42m + ``` -1. secret 数据通过挂载在 `/etc/secret-volume` 目录下的卷暴露在容器中。 -在 shell 中,进入 secret 数据被暴露的目录: +1. + 获取一个 shell 进入 Pod 中运行的容器: - ```shell - root@secret-test-pod:/# cd /etc/secret-volume - ``` -1. 在 shell 中,列出 `/etc/secret-volume` 目录的文件: + ```shell + kubectl exec -it secret-test-pod -- /bin/bash + ``` - ```shell - root@secret-test-pod:/etc/secret-volume# ls - ``` +1. + Secret 数据通过挂载在 `/etc/secret-volume` 目录下的卷暴露在容器中。 - ```shell - password username - ``` + 在 shell 中,列举 `/etc/secret-volume` 目录下的文件: -1. 在 shell 中,显示 `username` 和 `password` 文件的内容: + ```shell + ls /etc/secret-volume + ``` - ```shell - root@secret-test-pod:/etc/secret-volume# cat username; echo; cat password; echo - ``` + + 输出包含两个文件,每个对应一个 Secret 数据条目: - 输出为用户名和密码: + ``` + password username + ``` - ```shell - my-app - 39528$vdg7Jb - ``` +1. + 在 Shell 中,显示 `username` 和 `password` 文件的内容: -## 创建通过环境变量访问 secret 数据的 Pod + ```shell + # 在容器中 Shell 运行下面命令 + echo "$(cat /etc/secret-volume/username)" + echo "$(cat /etc/secret-volume/password)" + ``` -这里是一个可以用来创建 pod 的配置文件: + + 输出为用户名和密码: -{{< codenew file="pods/inject/secret-envars-pod.yaml" >}} + ```shell + my-app + 39528$vdg7Jb + ``` -1. 创建 Pod: + +## 使用 Secret 数据定义容器变量 - ```shell - kubectl get pod secret-envars-test-pod - ``` +### 使用来自 Secret 中的数据定义容器变量 - 输出: + +* 定义环境变量为 Secret 中的键值偶对: - ```shell - NAME READY STATUS RESTARTS AGE - secret-envars-test-pod 1/1 Running 0 4m - ``` + ```shell + kubectl create secret generic backend-user --from-literal=backend-username='backend-admin' + ``` -1. 在 Pod 中运行的容器中获取一个 shell: + +* 在 Pod 规约中,将 Secret 中定义的值 `backend-username` 赋给 `SECRET_USERNAME` 环境变量 - ```shell - kubectl exec -it secret-envars-test-pod -- /bin/bash - ``` + {{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" >}} -1. 在 shell 中,显示环境变量: + +* 创建 Pod: - ```shell - root@secret-envars-test-pod:/# printenv - ``` + ```shell + kubectl create -f https://k8s.io/examples/pods/inject/pod-single-secret-env-variable.yaml + ``` - 输出包括用户名和密码: + +* 在 Shell 中,显示容器环境变量 `SECRET_USERNAME` 的内容: - ```shell - ... - SECRET_USERNAME=my-app - ... - SECRET_PASSWORD=39528$vdg7Jb - ``` + ```shell + kubectl exec -i -t env-single-secret -- /bin/sh -c 'echo $SECRET_USERNAME' + ``` + 输出为: + ``` + backend-admin + ``` + +### 使用来自多个 Secret 的数据定义环境变量 -## {{% heading "whatsnext" %}} + +* 和前面的例子一样,先创建 Secret: + ```shell + kubectl create secret generic backend-user --from-literal=backend-username='backend-admin' + kubectl create secret generic db-user --from-literal=db-username='db-admin' + ``` -* 了解更多关于 [Secrets](/docs/concepts/configuration/secret/)。 -* 了解 [Volumes](/docs/concepts/storage/volumes/)。 + +* 在 Pod 规约中定义环境变量: + {{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" >}} + + +* 创建 Pod: + + ```shell + kubectl create -f https://k8s.io/examples/pods/inject/pod-multiple-secret-env-variable.yaml + ``` + + +* 在你的 Shell 中,显示容器环境变量的内容: + + ```shell + kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c 'env | grep _USERNAME' + ``` + + 输出: + ``` + DB_USERNAME=db-admin + BACKEND_USERNAME=backend-admin + ``` + + +## 将 Secret 中的所有键值偶对定义为环境变量 + + +{{< note >}} +此功能在 Kubernetes 1.6 版本之后可用。 +{{< /note >}} + + +* 创建包含多个键值偶对的 Secret: + + ```shell + kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb' + ``` + + +* 使用 `envFrom` 来将 Secret 中的所有数据定义为环境变量。 + Secret 中的键名成为容器中的环境变量名: + + {{< codenew file="pods/inject/pod-secret-envFrom.yaml" >}} + + +* 创建 Pod: + + ```shell + kubectl create -f https://k8s.io/examples/pods/inject/pod-secret-envFrom.yaml + ``` + + +* 在 Shell 中,显示环境变量 `username` 和 `password` 的内容: + + ```shell + kubectl exec -i -t envfrom-secret -- /bin/sh -c 'echo "username: $username\npassword: $password\n"' + ``` + + 输出为: + + ``` + username: my-app + password: 39528$vdg7Jb + ``` + + ### 参考 * [Secret](/docs/api-reference/{{< param "version" >}}/#secret-v1-core) @@ -203,3 +369,13 @@ base-64 形式的密码为 `Mzk1MjgkdmRnN0pi`。 * [Pod](/docs/api-reference/{{< param "version" >}}/#pod-v1-core) +## {{% heading "whatsnext" %}} + + +* 进一步了解 [Secret](/zh/docs/concepts/configuration/secret/)。 +* 了解 [Volumes](/zh/docs/concepts/storage/volumes/)。 + +