|
| 1 | +--- |
| 2 | +title: 使用配置文件管理 Secret |
| 3 | +content_type: task |
| 4 | +weight: 20 |
| 5 | +description: 使用资源配置文件创建 Secret 对象 |
| 6 | +--- |
| 7 | +<!-- |
| 8 | +title: Managing Secret using Configuration File |
| 9 | +content_type: task |
| 10 | +weight: 20 |
| 11 | +description: Creating Secret objects using resource configuration file. |
| 12 | +--> |
| 13 | + |
| 14 | +<!-- overview --> |
| 15 | + |
| 16 | +## {{% heading "prerequisites" %}} |
| 17 | + |
| 18 | +{{< include "task-tutorial-prereqs.md" >}} |
| 19 | + |
| 20 | +<!-- steps --> |
| 21 | + |
| 22 | +<!-- ## Create the Config file --> |
| 23 | +## 创建配置文件 {#create-the-config-file} |
| 24 | + |
| 25 | +<!-- |
| 26 | +You can create a Secret in a file first, in JSON or YAML format, and then |
| 27 | +create that object. The |
| 28 | +[Secret](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#secret-v1-core) |
| 29 | +resource contains two maps: `data` and `stringData`. |
| 30 | +The `data` field is used to store arbitrary data, encoded using base64. The |
| 31 | +`stringData` field is provided for convenience, and it allows you to provide |
| 32 | +Secret data as unencoded strings. |
| 33 | +The keys of `data` and `stringData` must consist of alphanumeric characters, |
| 34 | +`-`, `_` or `.`. |
| 35 | +--> |
| 36 | +你可以先用 JSON 或 YAML 格式在文件中创建 Secret,然后创建该对象。 |
| 37 | +[Secret](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#secret-v1-core) |
| 38 | +资源包含2个键值对: `data` 和 `stringData`。 |
| 39 | +`data` 字段用来存储 base64 编码的任意数据。 |
| 40 | +提供 `stringData` 字段是为了方便,它允许 Secret 使用未编码的字符串。 |
| 41 | +`data` 和 `stringData` 的键必须由字母、数字、`-`,`_` 或 `.` 组成。 |
| 42 | + |
| 43 | +<!-- |
| 44 | +For example, to store two strings in a Secret using the `data` field, convert |
| 45 | +the strings to base64 as follows: |
| 46 | +--> |
| 47 | +例如,要使用 Secret 的 `data` 字段存储两个字符串,请将字符串转换为 base64 ,如下所示: |
| 48 | + |
| 49 | +```shell |
| 50 | +echo -n 'admin' | base64 |
| 51 | +``` |
| 52 | + |
| 53 | +<!-- The output is similar to: --> |
| 54 | +输出类似于: |
| 55 | + |
| 56 | +``` |
| 57 | +YWRtaW4= |
| 58 | +``` |
| 59 | + |
| 60 | +```shell |
| 61 | +echo -n '1f2d1e2e67df' | base64 |
| 62 | +``` |
| 63 | + |
| 64 | +<!-- The output is similar to: --> |
| 65 | +输出类似于: |
| 66 | + |
| 67 | +``` |
| 68 | +MWYyZDFlMmU2N2Rm |
| 69 | +``` |
| 70 | + |
| 71 | +<!-- Write a Secret config file that looks like this: --> |
| 72 | +编写一个 Secret 配置文件,如下所示: |
| 73 | + |
| 74 | +```yaml |
| 75 | +apiVersion: v1 |
| 76 | +kind: Secret |
| 77 | +metadata: |
| 78 | + name: mysecret |
| 79 | +type: Opaque |
| 80 | +data: |
| 81 | + username: YWRtaW4= |
| 82 | + password: MWYyZDFlMmU2N2Rm |
| 83 | +``` |
| 84 | +
|
| 85 | +<!-- |
| 86 | +Note that the name of a Secret object must be a valid |
| 87 | +[DNS subdomain name](/docs/concepts/overview/working-with-objects/names#dns-subdomain-names). |
| 88 | +--> |
| 89 | +注意,Secret 对象的名称必须是有效的 [DNS 子域名](/zh/docs/concepts/overview/working-with-objects/names#dns-subdomain-names). |
| 90 | +
|
| 91 | +{{< note >}} |
| 92 | +<!-- |
| 93 | +The serialized JSON and YAML values of Secret data are encoded as base64 |
| 94 | +strings. Newlines are not valid within these strings and must be omitted. When |
| 95 | +using the `base64` utility on Darwin/macOS, users should avoid using the `-b` |
| 96 | +option to split long lines. Conversely, Linux users *should* add the option |
| 97 | +`-w 0` to `base64` commands or the pipeline `base64 | tr -d '\n'` if the `-w` |
| 98 | +option is not available. |
| 99 | +--> |
| 100 | +Secret 数据的 JSON 和 YAML 序列化结果是以 base64 编码的。 |
| 101 | +换行符在这些字符串中无效,必须省略。 |
| 102 | +在 Darwin/macOS 上使用 `base64` 工具时,用户不应该使用 `-b` 选项分割长行。 |
| 103 | +相反地,Linux 用户 *应该* 在 `base64` 地命令中添加 `-w 0` 选项, |
| 104 | +或者在 `-w` 选项不可用的情况下,输入 `base64 | tr -d '\n'`。 |
| 105 | +{{< /note >}} |
| 106 | + |
| 107 | +<!-- |
| 108 | +For certain scenarios, you may wish to use the `stringData` field instead. This |
| 109 | +field allows you to put a non-base64 encoded string directly into the Secret, |
| 110 | +and the string will be encoded for you when the Secret is created or updated. |
| 111 | +--> |
| 112 | +对于某些场景,你可能希望使用 `stringData` 字段。 |
| 113 | +这字段可以将一个非 base64 编码的字符串直接放入 Secret 中, |
| 114 | +当创建或更新该 Secret 时,此字段将被编码。 |
| 115 | + |
| 116 | +<!-- |
| 117 | +A practical example of this might be where you are deploying an application |
| 118 | +that uses a Secret to store a configuration file, and you want to populate |
| 119 | +parts of that configuration file during your deployment process. |
| 120 | +--> |
| 121 | +上述用例的实际场景可能是这样:当你部署应用时,使用 Secret 存储配置文件, |
| 122 | +你希望在部署过程中,填入部分内容到该配置文件。 |
| 123 | + |
| 124 | +<!-- For example, if your application uses the following configuration file: --> |
| 125 | +例如,如果你的应用程序使用以下配置文件: |
| 126 | + |
| 127 | +```yaml |
| 128 | +apiUrl: "https://my.api.com/api/v1" |
| 129 | +username: "<user>" |
| 130 | +password: "<password>" |
| 131 | +``` |
| 132 | + |
| 133 | +<!-- You could store this in a Secret using the following definition: --> |
| 134 | +你可以使用以下定义将其存储在 Secret 中: |
| 135 | + |
| 136 | +```yaml |
| 137 | +apiVersion: v1 |
| 138 | +kind: Secret |
| 139 | +metadata: |
| 140 | + name: mysecret |
| 141 | +type: Opaque |
| 142 | +stringData: |
| 143 | + config.yaml: | |
| 144 | + apiUrl: "https://my.api.com/api/v1" |
| 145 | + username: <user> |
| 146 | + password: <password> |
| 147 | +``` |
| 148 | + |
| 149 | +<!-- ## Create the Secret object --> |
| 150 | +## 创建 Secret 对象 {#create-the-secret-object} |
| 151 | + |
| 152 | +<!-- Now create the Secret using [`kubectl apply`](/docs/reference/generated/kubectl/kubectl-commands#apply): --> |
| 153 | +现在使用 [`kubectl apply`](/zh/docs/reference/generated/kubectl/kubectl-commands#apply) 创建 Secret: |
| 154 | + |
| 155 | +```shell |
| 156 | +kubectl apply -f ./secret.yaml |
| 157 | +``` |
| 158 | + |
| 159 | +<!-- The output is similar to: --> |
| 160 | +输出类似于: |
| 161 | + |
| 162 | +``` |
| 163 | +secret/mysecret created |
| 164 | +``` |
| 165 | +
|
| 166 | +<!-- ## Check the Secret --> |
| 167 | +## 检查 Secret {#check-the-secret} |
| 168 | +
|
| 169 | +<!-- |
| 170 | +The `stringData` field is a write-only convenience field. It is never output when |
| 171 | +retrieving Secrets. For example, if you run the following command: |
| 172 | +--> |
| 173 | +`stringData` 字段是只写的。获取 Secret 时,此字段永远不会输出。 |
| 174 | +例如,如果你运行以下命令: |
| 175 | +
|
| 176 | +
|
| 177 | +```shell |
| 178 | +kubectl get secret mysecret -o yaml |
| 179 | +``` |
| 180 | + |
| 181 | +<!-- The output is similar to: --> |
| 182 | +输出类似于: |
| 183 | + |
| 184 | +```yaml |
| 185 | +apiVersion: v1 |
| 186 | +kind: Secret |
| 187 | +metadata: |
| 188 | + creationTimestamp: 2018-11-15T20:40:59Z |
| 189 | + name: mysecret |
| 190 | + namespace: default |
| 191 | + resourceVersion: "7225" |
| 192 | + uid: c280ad2e-e916-11e8-98f2-025000000001 |
| 193 | +type: Opaque |
| 194 | +data: |
| 195 | + config.yaml: YXBpVXJsOiAiaHR0cHM6Ly9teS5hcGkuY29tL2FwaS92MSIKdXNlcm5hbWU6IHt7dXNlcm5hbWV9fQpwYXNzd29yZDoge3twYXNzd29yZH19 |
| 196 | +``` |
| 197 | +
|
| 198 | +<!-- |
| 199 | +The commands `kubectl get` and `kubectl describe` avoid showing the contents of a `Secret` by |
| 200 | +default. This is to protect the `Secret` from being exposed accidentally to an onlooker, |
| 201 | +or from being stored in a terminal log. |
| 202 | +To check the actual content of the encoded data, please refer to |
| 203 | +[decoding secret](/docs/tasks/configmap-secret/managing-secret-using-kubectl/#decoding-secret). |
| 204 | +--> |
| 205 | +命令 `kubectl get` 和 `kubectl describe` 默认情况不显示 `Secret` 的内容。 |
| 206 | +这是为了防止 `Secret` 意外地暴露给旁观者,或者保存在终端日志中。 |
| 207 | +检查编码数据的实际内容,请参考 [解码 secret](/zh/docs/tasks/configmap-secret/managing-secret-using-kubectl/#decoding-secret). |
| 208 | + |
| 209 | +<!-- |
| 210 | +If a field, such as `username`, is specified in both `data` and `stringData`, |
| 211 | +the value from `stringData` is used. For example, the following Secret definition: |
| 212 | +--> |
| 213 | +如果在 `data` 和 `stringData` 中都指定了一个字段,比如 `username`,字段值来自 `stringData`。 |
| 214 | +例如,下面的 Secret 定义: |
| 215 | + |
| 216 | +```yaml |
| 217 | +apiVersion: v1 |
| 218 | +kind: Secret |
| 219 | +metadata: |
| 220 | + name: mysecret |
| 221 | +type: Opaque |
| 222 | +data: |
| 223 | + username: YWRtaW4= |
| 224 | +stringData: |
| 225 | + username: administrator |
| 226 | +``` |
| 227 | + |
| 228 | +<!-- Results in the following Secret: --> |
| 229 | +结果有以下 Secret: |
| 230 | + |
| 231 | +```yaml |
| 232 | +apiVersion: v1 |
| 233 | +kind: Secret |
| 234 | +metadata: |
| 235 | + creationTimestamp: 2018-11-15T20:46:46Z |
| 236 | + name: mysecret |
| 237 | + namespace: default |
| 238 | + resourceVersion: "7579" |
| 239 | + uid: 91460ecb-e917-11e8-98f2-025000000001 |
| 240 | +type: Opaque |
| 241 | +data: |
| 242 | + username: YWRtaW5pc3RyYXRvcg== |
| 243 | +``` |
| 244 | + |
| 245 | +<!-- Where `YWRtaW5pc3RyYXRvcg==` decodes to `administrator`. --> |
| 246 | +其中 `YWRtaW5pc3RyYXRvcg==` 解码成 `administrator`。 |
| 247 | + |
| 248 | +<!-- ## Clean Up --> |
| 249 | +## 清理 {#clean-up} |
| 250 | + |
| 251 | +<!-- To delete the Secret you have just created: --> |
| 252 | +删除你刚才创建的 Secret: |
| 253 | + |
| 254 | +```shell |
| 255 | +kubectl delete secret db-user-pass |
| 256 | +``` |
| 257 | + |
| 258 | +## {{% heading "whatsnext" %}} |
| 259 | + |
| 260 | +<!-- |
| 261 | +- Read more about the [Secret concept](/docs/concepts/configuration/secret/) |
| 262 | +- Learn how to [manage Secret with the `kubectl` command](/docs/tasks/configmap-secret/managing-secret-using-kubectl/) |
| 263 | +- Learn how to [manage Secret using kustomizae](/docs/tasks/configmap-secret/managing-secret-using-kustomize/) |
| 264 | +--> |
| 265 | +- 阅读更多有关于 [Secret 概念](/zh/docs/concepts/configuration/secret/) |
| 266 | +- 了解如何 [使用 `kubectl` 命令管理 Secret](/zh/docs/tasks/configmap-secret/managing-secret-using-kubectl/) |
| 267 | +- 了解如何 [使用 kustomize 管理 Secret](/zh/docs/tasks/configmap-secret/managing-secret-using-kustomize/) |
| 268 | + |
0 commit comments