secret
This commit is contained in:
55
learning/k8s-intermediate/config/secrets/create-kubectl.md
Normal file
55
learning/k8s-intermediate/config/secrets/create-kubectl.md
Normal file
@ -0,0 +1,55 @@
|
||||
---
|
||||
vssueId: 78
|
||||
layout: LearningLayout
|
||||
description: Kubernetes教程_在Kubernetes中_使用kubectl创建Secret
|
||||
---
|
||||
|
||||
# 使用 kubectl 创建 Secrets
|
||||
|
||||
假设某个 Pod 需要访问数据库。在您执行 kubectl 命令所在机器的当前目录,创建文件 `./username.txt` 文件和 `./password.txt` 暂存数据库的用户名和密码,后续我们根据这两个文件配置 kubernetes secrets。
|
||||
|
||||
```sh
|
||||
echo -n 'admin' > ./username.txt
|
||||
echo -n '1f2d1e2e67df' > ./password.txt
|
||||
```
|
||||
|
||||
执行命令 `kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt` 在 Kubernetes APIServer 中创建 Secret 对象,并将这两个文件中的内容存储到该 Secret 对象中,输出结果如下所示:
|
||||
|
||||
```
|
||||
secret "db-user-pass" created
|
||||
```
|
||||
|
||||
::: tip
|
||||
* 上述命令的执行效果与此命令执行效果相同:
|
||||
`kubectl create secret generic db-user-pass –from-literal=username=admin –from-literal=password=1f2d1e2e67df`
|
||||
* 如果您的密码中包含特殊字符需要转码(例如 `$`、`*`、`\`、`!`),请使用 `\\` 进行转码。例如:实际密码为 `S!B\*d$zDsb`,kubectl 命令应该写成 `kubectl create secret generic dev-db-secret –from-literal=username=devuser –from-literal=password=S\!B\\*d\$zDsb`。如果通过文件创建(--from-file),则无需对文件中的密码进行转码。
|
||||
:::
|
||||
|
||||
执行命令 `kubectl get secrets`,检查 Secret 的创建结果,输出信息如下所示:
|
||||
|
||||
```
|
||||
NAME TYPE DATA AGE
|
||||
db-user-pass Opaque 2 51s
|
||||
```
|
||||
|
||||
执行命令 `kubectl describe secrets/db-user-pass`,查看 Secret 详情,输出信息如下所示:
|
||||
|
||||
```
|
||||
Name: db-user-pass
|
||||
Namespace: default
|
||||
Labels: <none>
|
||||
Annotations: <none>
|
||||
|
||||
Type: Opaque
|
||||
|
||||
Data
|
||||
====
|
||||
password.txt: 12 bytes
|
||||
username.txt: 5 bytes
|
||||
```
|
||||
|
||||
::: tip
|
||||
默认情况下,`kubectl get` 和 `kubectl describe` 命令都避免展示 Secret 的内容。这种做法可以避免密码被偷窥,或者被存储到终端的日志中
|
||||
:::
|
||||
|
||||
参考 [解码](./decode-edit.html) 了解如何查看 Secret 中存储的内容
|
||||
64
learning/k8s-intermediate/config/secrets/decode-edit.md
Normal file
64
learning/k8s-intermediate/config/secrets/decode-edit.md
Normal file
@ -0,0 +1,64 @@
|
||||
---
|
||||
vssueId: 79
|
||||
layout: LearningLayout
|
||||
description: Kubernetes教程_在Kubernetes中_使用kubectl解码和编辑Secret
|
||||
---
|
||||
|
||||
# 解码和编辑Secret
|
||||
|
||||
## 解码Secret
|
||||
|
||||
Secret 中的信息可以通过 `kubectl get secret` 命令获取。例如,执行命令 `kubectl get secret mysecret -o yaml
|
||||
` 可获取前面章节中所创建的 Secret,输出信息如下:
|
||||
|
||||
```
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
creationTimestamp: 2016-01-22T18:41:56Z
|
||||
name: mysecret
|
||||
namespace: default
|
||||
resourceVersion: "164619"
|
||||
uid: cfee02d6-c137-11e5-8d73-42010af00002
|
||||
type: Opaque
|
||||
data:
|
||||
username: YWRtaW4=
|
||||
password: MWYyZDFlMmU2N2Rm
|
||||
```
|
||||
|
||||
执行命令 `echo 'MWYyZDFlMmU2N2Rm' | base64 --decode` 可解码密码字段,输出结果如下:
|
||||
|
||||
```
|
||||
1f2d1e2e67df
|
||||
```
|
||||
|
||||
执行命令 `echo 'YWRtaW4=' | base64 --decode` 可解码用户名字段,输出结果如下:
|
||||
|
||||
```
|
||||
admin
|
||||
```
|
||||
|
||||
## 编辑Secret
|
||||
|
||||
执行命令 `kubectl edit secrets mysecret` 可以编辑已经创建的 Secret,该命令将打开一个类似于 `vi` 的文本编辑器,您可以直接编辑已经进行 base64 编码的字段,如下所示:
|
||||
|
||||
``` yaml {7,8}
|
||||
# Please edit the object below. Lines beginning with a '#' will be ignored,
|
||||
# and an empty file will abort the edit. If an error occurs while saving this file will be
|
||||
# reopened with the relevant failures.
|
||||
#
|
||||
apiVersion: v1
|
||||
data:
|
||||
username: YWRtaW4=
|
||||
password: MWYyZDFlMmU2N2Rm
|
||||
kind: Secret
|
||||
metadata:
|
||||
annotations:
|
||||
kubectl.kubernetes.io/last-applied-configuration: { ... }
|
||||
creationTimestamp: 2016-01-22T18:41:56Z
|
||||
name: mysecret
|
||||
namespace: default
|
||||
resourceVersion: "164619"
|
||||
uid: cfee02d6-c137-11e5-8d73-42010af00002
|
||||
type: Opaque
|
||||
```
|
||||
@ -35,11 +35,11 @@ Kubernetes 自动创建包含访问 Kubernetes APIServer 身份信息的 Secret
|
||||
|
||||
您可以使用如下方式创建自己的 Secret:
|
||||
|
||||
* 使用 kubectl 创建 Secret
|
||||
* [使用 kubectl 创建 Secret](./create-kubectl.html)
|
||||
* 手动创建 Secret
|
||||
* 使用 Generator 创建 Secret
|
||||
* 使用 Kuboard 创建 Secret
|
||||
|
||||
## 解码和编辑
|
||||
|
||||
Kubenetes 中,Secret 使用 base64 编码存储,您可以将其 [解码](./decode_edit.html) 获得对饮信息的原文,创建 Secret 之后,您也可以再次 [编辑](./decode_edit.html) Secret
|
||||
Kubenetes 中,Secret 使用 base64 编码存储,您可以将其 [解码](./decode-edit.html) 获得对饮信息的原文,创建 Secret 之后,您也可以再次 [编辑](./decode-edit.html) Secret
|
||||
|
||||
Reference in New Issue
Block a user