@@ -13,6 +13,14 @@ description: Creating Secret objects using kubectl command line.
13
13
14
14
<!-- overview -->
15
15
16
+ <!--
17
+ This page shows you how to create, edit, manage, and delete Kubernetes
18
+ {{<glossary_tooltip text="Secrets" term_id="secret">}} using the `kubectl`
19
+ command-line tool.
20
+ -->
21
+ 本页向你展示如何使用 ` kubectl ` 命令行工具来创建、编辑、管理和删除。
22
+ Kubernetes {{<glossary_tooltip text="Secrets" term_id="secret">}}
23
+
16
24
## {{% heading "prerequisites" %}}
17
25
18
26
{{< include "task-tutorial-prereqs.md" >}}
@@ -23,118 +31,134 @@ description: Creating Secret objects using kubectl command line.
23
31
## 创建 Secret {#create-a-secret}
24
32
25
33
<!--
26
- A `Secret` can contain user credentials required by pods to access a database.
27
- For example, a database connection string consists of a username and password.
28
- You can store the username in a file `./username.txt` and the password in a
29
- file `./password.txt` on your local machine.
30
- -->
31
- 一个 ` Secret ` 可以包含 Pod 访问数据库所需的用户凭证。
32
- 例如,由用户名和密码组成的数据库连接字符串。
33
- 你可以在本地计算机上,将用户名存储在文件 ` ./username.txt ` 中,将密码存储在文件 ` ./password.txt ` 中。
34
+ A `Secret` object stores sensitive data such as credentials
35
+ used by Pods to access services. For example, you might need a Secret to store
36
+ the username and password needed to access a database.
37
+ -->
38
+ ` Secret ` 对象用来存储敏感数据,如 Pod 用于访问服务的凭据。例如,为访问数据库,你可能需要一个
39
+ Secret 来存储所需的用户名及密码。
34
40
35
- ``` shell
36
- echo -n ' admin' > ./username.txt
37
- echo -n ' 1f2d1e2e67df' > ./password.txt
38
- ```
41
+ <!--
42
+ You can create the Secret by passing the raw data in the command, or by storing
43
+ the credentials in files that you pass in the command. The following commands
44
+ create a Secret that stores the username `admin` and the password `S!B\*d$zDsb=`.
45
+ -->
46
+ 你可以通过在命令中传递原始数据,或将凭据存储文件中,然后再在命令行中创建 Secret。以下命令
47
+ 将创建一个存储用户名 ` admin ` 和密码 ` S!B\*d$zDsb= ` 的 Secret。
39
48
40
- <!--
41
- In these commands, the `-n` flag ensures that the generated files do not have
42
- an extra newline character at the end of the text. This is important because
43
- when `kubectl` reads a file and encodes the content into a base64 string, the
44
- extra newline character gets encoded too.
49
+ <!--
50
+ ### Use raw data
45
51
-->
46
- 在这些命令中,` -n ` 标志确保生成的文件在文本末尾不包含额外的换行符。
47
- 这一点很重要,因为当 ` kubectl ` 读取文件并将内容编码为 base64 字符串时,多余的换行符也会被编码。
52
+ ### 使用原始数据
48
53
49
- <!--
50
- The `kubectl create secret` command packages these files into a Secret and creates
51
- the object on the API server.
54
+ <!--
55
+ Run the following command:
52
56
-->
53
- ` kubectl create secret ` 命令将这些文件打包成一个 Secret 并在 API 服务器上创建对象。
57
+ 执行以下命令:
54
58
55
59
``` shell
56
60
kubectl create secret generic db-user-pass \
57
- --from-file=./username.txt \
58
- --from-file=./password.txt
59
- ```
60
-
61
- <!-- The output is similar to: -->
62
- 输出类似于:
63
-
64
- ```
65
- secret/db-user-pass created
61
+ --from-literal=username=devuser \
62
+ --from-literal=password=' S!B\*d$zDsb='
66
63
```
67
64
68
- <!--
69
- The default key name is the filename. You can optionally set the key name using
70
- `--from-file=[key=]source`. For example:
65
+ <!--
66
+ You must use single quotes `''` to escape special characters such as `$`, `\`,
67
+ `*`, `=`, and `!` in your strings. If you don't, your shell will interpret these
68
+ characters.
71
69
-->
72
- 默认密钥名称是文件名。 你可以选择使用 ` --from-file=[key=]source ` 来设置密钥名称。例如:
73
-
74
- ``` shell
75
- kubectl create secret generic db-user-pass \
76
- --from-file=username=./username.txt \
77
- --from-file=password=./password.txt
78
- ```
70
+ 你必须使用单引号 ` '' ` 转义字符串中的特殊字符,如 ` $ ` 、` \ ` 、` * ` 、` = ` 和` ! ` 。否则,你的 shell
71
+ 将会解析这些字符。
79
72
80
- <!--
81
- You do not need to escape special characters in password strings that you
82
- include in a file.
73
+ <!--
74
+ ### Use source files
83
75
-->
84
- 你不需要对文件中包含的密码字符串中的特殊字符进行转义。
76
+ ### 使用源文件
85
77
86
- <!--
87
- You can also provide Secret data using the `--from-literal=<key>=<value>` tag.
88
- This tag can be specified more than once to provide multiple key-value pairs.
89
- Note that special characters such as `$`, `\`, `*`, `=`, and `!` will be
90
- interpreted by your [shell](https://en.wikipedia.org/wiki/Shell_(computing))
91
- and require escaping.
78
+ <!--
79
+ 1. Store the credentials in files with the values encoded in base64:
80
+ -->
81
+ 1 . 对凭证的取值作 base64 编码后保存到文件中:
82
+
83
+ ``` shell
84
+ echo -n ' admin' | base64 > ./username.txt
85
+ echo -n ' S!B\*d$zDsb=' | base64 > ./password.txt
86
+ ```
87
+ < ! --
88
+ The ` -n` flag ensures that the generated files do not have an extra newline
89
+ character at the end of the text. This is important because when ` kubectl`
90
+ reads a file and encodes the content into a base64 string, the extra
91
+ newline character gets encoded too. You do not need to escape special
92
+ characters in strings that you include in a file.
93
+ -->
94
+ ` -n` 标志用来确保生成文件的文末没有多余的换行符。这很重要,因为当 ` kubectl`
95
+ 读取文件并将内容编码为 base64 字符串时,额外的换行符也会被编码。
96
+ 你不需要对文件中包含的字符串中的特殊字符进行转义。
92
97
93
- In most shells, the easiest way to escape the password is to surround it with
94
- single quotes (`'`). For example, if your password is `S!B\*d$zDsb=`,
95
- run the following command:
98
+ < ! --
99
+ 2. Pass the file paths in the ` kubectl` command:
96
100
-->
97
- 你还可以使用 ` --from-literal=<key>=<value> ` 标签提供 Secret 数据。
98
- 可以多次使用此标签,提供多个键值对。
99
- 请注意,特殊字符(例如:` $ ` ,` \ ` ,` * ` ,` = ` 和 ` ! ` )由你的 [ shell] ( https://en.wikipedia.org/wiki/Shell_(computing) )
100
- 解释执行,而且需要转义。
101
+ 2. 在 ` kubectl` 命令中传递文件路径:
102
+
103
+ ` ` ` shell
104
+ kubectl create secret generic db-user-pass \
105
+ --from-file=./username.txt \
106
+ --from-file=./password.txt
107
+ ` ` `
108
+ < ! --
109
+ The default key name is the file name. You can optionally set the key name
110
+ using ` --from-file=[key= ]source` . For example:
111
+ -->
112
+ 默认键名为文件名。你也可以通过 ` --from-file=[key= ]source` 设置键名,例如:
113
+
114
+ ` ` ` shell
115
+ kubectl create secret generic db-user-pass \
116
+ --from-file=username=./username.txt \
117
+ --from-file=password=./password.txt
118
+ ` ` `
101
119
102
- 在大多数 shell 中,转义密码最简便的方法是用单引号括起来。
103
- 比如,如果你的密码是 ` S!B\*d$zDsb= ` ,
104
- 可以像下面一样执行命令:
120
+ < ! --
121
+ With either method, the output is similar to:
122
+ -->
123
+ 无论使用哪种方法,输出都类似于:
105
124
106
- ``` shell
107
- kubectl create secret generic db-user-pass \
108
- --from-literal=username=devuser \
109
- --from-literal=password=' S!B\*d$zDsb='
125
+ ```
126
+ secret/db-user-pass created
110
127
```
111
128
112
- <!-- ## Verify the Secret -->
113
- ## 验证 Secret {#verify-the-secret}
129
+ <!--
130
+ ### Verify the Secret {#verify-the-secret}
131
+ -->
132
+ ## 验证 Secret {#verify-the-secret}
114
133
115
- <!-- Check that the Secret was created: -->
116
- 检查 secret 是否已创建:
134
+ <!--
135
+ Check that the Secret was created:
136
+ -->
137
+ 检查 Secret 是否已创建:
117
138
118
139
```shell
119
140
kubectl get secrets
120
141
```
121
142
122
- <!-- The output is similar to: -->
123
143
输出类似于:
124
144
125
145
```
126
146
NAME TYPE DATA AGE
127
147
db-user-pass Opaque 2 51s
128
148
```
129
149
130
- <!-- You can view a description of the `Secret`: -->
131
- 你可以查看 ` Secret ` 的描述:
150
+ <!--
151
+ View the details of the Secret:
152
+ -->
153
+ 查看 Secret 的细节:
132
154
133
155
``` shell
134
- kubectl describe secrets/ db-user-pass
156
+ kubectl describe secret db-user-pass
135
157
```
136
158
137
- <!-- The output is similar to: -->
159
+ <!--
160
+ The output is similar to:
161
+ -->
138
162
输出类似于:
139
163
140
164
```
@@ -159,76 +183,123 @@ accidentally, or from being stored in a terminal log.
159
183
` kubectl get ` 和 ` kubectl describe ` 命令默认不显示 ` Secret ` 的内容。
160
184
这是为了防止 ` Secret ` 被意外暴露或存储在终端日志中。
161
185
162
- <!--
163
- To check the actual content of the encoded data, refer to [Decoding the Secret]( #decoding-secret).
186
+ <!--
187
+ ### Decode the Secret { #decoding-secret}
164
188
-->
165
- 查看编码数据的实际内容,请参考 [ 解码 Secret] ( #decoding-secret ) 。
189
+ ### 解码 Secret { #decoding-secret}
166
190
167
- <!-- ## Decoding the Secret {#decoding-secret} -->
168
- ## 解码 Secret {#decoding-secret}
169
-
170
- <!--
171
- To view the contents of the Secret you created, run the following command:
191
+ <!--
192
+ 1. View the contents of the Secret you created:
172
193
-->
173
- 要查看创建的 Secret 的内容,运行以下命令:
194
+ 1 . 查看你所创建的 Secret 内容
174
195
175
- ``` shell
176
- kubectl get secret db-user-pass -o jsonpath=' {.data}'
177
- ```
196
+ ``` shell
197
+ kubectl get secret db-user-pass -o jsonpath=' {.data}'
198
+ ```
178
199
179
200
< ! -- The output is similar to: -->
180
- 输出类似于:
201
+ 输出类似于:
181
202
182
- ``` json
183
- {"password" :" MWYyZDFlMmU2N2Rm " ,"username" :" YWRtaW4=" }
184
- ```
203
+ ` ` ` json
204
+ {" password" :" UyFCXCpkJHpEc2I9 " ," username" :" YWRtaW4=" }
205
+ ` ` `
185
206
186
- <!--
187
- Now you can decode the `password` data:
188
- -->
189
- 现在你可以解码 ` password ` 的数据:
207
+ < ! -- 2. Decode the ` password` data: -->
208
+ 2. 解码 ` password` 数据:
190
209
191
- ``` shell
192
- # 这是一个用于文档说明的示例。
193
- # 如果你这样做,数据 'MWYyZDFlMmU2N2Rm' 可以存储在你的 shell 历史中。
194
- # 可以进入你电脑的人可以找到那个记住的命令并可以在你不知情的情况下 base-64 解码这个 Secret。
195
- # 通常最好将这些步骤结合起来,如页面后面所示。
196
- echo ' MWYyZDFlMmU2N2Rm' | base64 --decode
197
- ```
210
+ ` ` ` shell
211
+ echo ' UyFCXCpkJHpEc2I9' | base64 --decode
212
+ ` ` `
198
213
199
214
< ! -- The output is similar to: -->
200
- 输出类似于:
215
+ 输出类似于:
216
+
217
+ ```
218
+ S! B\* d$zDsb =
219
+ ```
220
+
221
+ < ! --
222
+ {{< caution> }}This is an example for documentation purposes. In practice,
223
+ this method could cause the command with the encoded data to be stored in
224
+ your shell history. Anyone with access to your computer could find the
225
+ command and decode the secret. A better approach is to combine the view and
226
+ decode commands.{{< /caution> }}
227
+ -->
228
+ {{< caution> }}
229
+ 这是一个出于文档编制目的的示例。实际上,该方法可能会导致包含编码数据的命令存储在
230
+ Shell 的历史记录中。任何可以访问你的计算机的人都可以找到该命令并对 Secret 进行解码。
231
+ 更好的办法是将查看和解码命令一同使用。{{< /caution> }}
232
+
233
+ ` ` ` shell
234
+ kubectl get secret db-user-pass -o jsonpath=' {.data.password}' | base64 --decode
235
+ ` ` `
201
236
202
- ```
203
- 1f2d1e2e67df
204
- ```
237
+ < ! --
238
+ # # Edit a Secret {#edit-secret}
239
+ -->
240
+ # # 编辑 Secret {#edit-secret}
205
241
206
- <!--
207
- In order to avoid storing a secret encoded value in your shell history, you can
208
- run the following command:
242
+ < ! --
243
+ You can edit an existing ` Secret` object unless it is
244
+ [immutable](/docs/concepts/configuration/secret/# secret-immutable). To edit a
245
+ Secret, run the following command:
209
246
-->
210
- 为了避免在 shell 历史记录中存储 Secret 的编码值,可以执行如下命令:
247
+ 你可以编辑一个现存的 ` Secret` 对象,除非它是[不可改变的](/zh-cn/docs/concepts/configuration/secret/# secret-immutable)。
248
+ 要想编辑一个 Secret,请执行以下命令:
211
249
212
250
` ` ` shell
213
- kubectl get secret db-user-pass -o jsonpath= ' {.data.password} ' | base64 --decode
251
+ kubectl edit secrets < secret-name >
214
252
` ` `
215
253
254
+ < ! --
255
+ This opens your default editor and allows you to update the base64 encoded
256
+ Secret values in the ` data` field, such as in the following example:
257
+ -->
258
+ 这将打开默认编辑器,并允许你更新 ` data` 字段中的 base64 编码的 Secret 值,示例如下:
216
259
< ! --
217
- The output shall be similar as above.
260
+ # Please edit the object below. Lines beginning with a '#' will be ignored,
261
+ # and an empty file will abort the edit. If an error occurs while saving this file, it will be
262
+ # reopened with the relevant failures.
263
+ #
218
264
-->
219
- 输出应与上述类似。
220
265
221
- <!-- ## Clean Up -->
266
+ ` ` ` yaml
267
+
268
+ # 请编辑下面的对象。以“#”开头的行将被忽略,
269
+ # 空文件将中止编辑。如果在保存此文件时发生错误,
270
+ # 则将重新打开该文件并显示相关的失败。
271
+ #
272
+ apiVersion: v1
273
+ data:
274
+ password: UyFCXCpkJHpEc2I9
275
+ username: YWRtaW4=
276
+ kind: Secret
277
+ metadata:
278
+ creationTimestamp: " 2022-06-28T17:44:13Z"
279
+ name: db-user-pass
280
+ namespace: default
281
+ resourceVersion: " 12708504"
282
+ uid: 91becd59-78fa-4c85-823f-6d44436242ac
283
+ type: Opaque
284
+ ` ` `
285
+
286
+ < ! --
287
+ # # Clean up
288
+ -->
222
289
# # 清理 {#clean-up}
223
290
224
- <!-- Delete the Secret you created: -->
225
- 删除创建的 Secret:
291
+ < ! --
292
+ To delete a Secret, run the following command:
293
+ -->
294
+ 要想删除一个 Secret,请执行以下命令:
226
295
227
296
` ` ` shell
228
297
kubectl delete secret db-user-pass
229
298
` ` `
230
299
231
- <!-- discussion -->
300
+ < ! --
301
+ discussion
302
+ -->
232
303
233
304
# # {{% heading "whatsnext" %}}
234
305
0 commit comments