创建 Secret#

sudo kubectl create secret -h

Create a secret with specified type.

A docker-registry type secret is for accessing a container registry. – docker 仓库加密(常用)

A generic type secret indicate an Opaque secret type. – 普通文本加密

A tls type secret holds TLS certificate and its associated key.

Available Commands:
docker-registry Create a secret for use with a Docker registry
generic Create a secret from a local file, directory, or literal
value
tls Create a TLS secret

Usage:
kubectl create secret (docker-registry | generic | tls) [options]

Use “kubectl create secret –help” for more information about a given
command.

普通文本加密#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
hhhyc@hhhyc:~$ sudo kubectl create secret generic test-secret --from- literal=username=admin --from-literal=password=ds@!3-

sudo kubectl create secret generic test-secret --from-literal=username=admin --from-literal=password=ds@cd

secret/test-secret created

hhhyc@hhhyc:~$ sudo kubectl describe secret/test-secret #必须为 secret/secretName
Name: test-secret
Namespace: default
Labels: <none>
Annotations: <none>

Type: Opaque

Data
====
password: 5 bytes
username: 5 bytes
  1. 密码被和谐了 ds@!3- 变成了 ds@cd
  2. 展示密码的是否数据只是显示为字节数,因此 describe 没啥意义
  • 加密形式

    1
    2
    3
    4
    5
    6
    7
    8
    hhhyc@hhhyc:~$ echo 'admin' | base64
    YWRtaW4K
    hhhyc@hhhyc:~$ echo 'YWRtaW4K' | base64 --decode
    admin
    hhhyc@hhhyc:~$ echo 'ds@cd' | base64
    ZHNAY2QK
    hhhyc@hhhyc:~$ echo 'ZHNAY2QK' | base64 --decode
    ds@cd

    只是用 Base64编码方式去创建,安全性不高

Docker Registy 加密#

1
2
3
4
5
6
7
sudo kubectl create secret docker-registry -h

# 帮助信息中有使用方法:
Usage:
kubectl create secret docker-registry NAME --docker-username=user
--docker-password=password --docker-email=email [--docker-server=string]
[--from-file=[key=]source] [--dry-run=server|client|none] [options]

创建 Secret :

1
2
3
4
5
6
7
8
9
10
11
sudo kubectl create secret docker-registry docker-secret --docker-
username=username --docker-password=password --docker-email=email
secret/docker-secret created

hhhyc@hhhyc:~$ sudo kubectl get secret docker-secret
NAME TYPE DATA AGE
docker-secret kubernetes.io/dockerconfigjson 1 82s
hhhyc@hhhyc:~$ sudo kubectl describe secret docker-secret
Name: docker-secret
Namespace: default
Labels: <none>

拿到我加密后的数据 并 解密

  • 拿到我加密后的数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
kubectl edit secret docker-secret
-----------------------------------------------------
# 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 fil# reopened with the relevant failures.
#
apiVersion: v1
data:
.dockerconfigjson: eyJhdXRocyI6eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsidXNl
kind: Secret
metadata:
creationTimestamp: "2024-07-10T15:03:02Z"
name: docker-secret
namespace: default
resourceVersion: "40782"
uid: 731a0968-1261-4de2-9265-67c2930fcf45
type: kubernetes.io/dockerconfigjson
~
~
~
~
~
~
~
~
~
~
~
~
- /tmp/kubectl-edit-3846701641.yaml 1/15 6%

加密后的数据为: eyJhdXRocyI6eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsidXNl

  • 解密一下
1
2
hhhyc@hhhyc:~$ echo 'eyJhdXRocyI6eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsidXNl' | base64 --decode
{"auths":{"https://index.docker.io/v1/":{"usehhhyc@hhhyc:~$

yaml文件加密#

好像还可以用yaml文件加密,GPT字节说的:

  1. 创建 YAML 文件
    • 将您的数据定义为一个 YAML 文件。您可以使用 data 字段以 Base64 编码格式存储数据,或者使用 stringData 字段直接存储字符串数据。
  2. Base64 编码(如果使用 data 字段):
    • 将您的敏感信息进行 Base64 编码。可以使用 base64 命令行工具进行编码。
  3. 编写 Secret YAML
    • 使用以下格式编写 YAML 文件:
1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque # 根据需要选择 Secret 类型,例如 "kubernetes.io/service-account-token"
data: # 使用 Base64 编码的数据
username: <base64-encoded-username>
password: <base64-encoded-password>
# 或者使用 stringData 直接存储字符串(不推荐存储敏感信息)
# stringData:
# username: "my-username"
# password: "my-password"
  • 假设您有一个用户名 myuser 和密码 mypassword,您可以这样创建 Secret:
1
2
3
4
5
6
7
8
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
username: bXl1c2Vy # "myuser" Base64 编码后的结果
password: bXlwYXNzd29yZA== # "mypassword" Base64 编码后的结果

创建 Secret

  • 使用 kubectl 命令行工具应用 YAML 文件来创建 Secret:
1
kubectl apply -f my-secret.yaml

验证 Secret 是否创建成功

  • 使用以下命令检查 Secret 是否已正确创建:
1
kubectl get secret my-secret

这个我就不验证了,反正都是gpt的东西,不大会错