目录
[二、Secret 类型](#二、Secret 类型)
[kubectl 创建类型](#kubectl 创建类型)
[三、Secret 使用](#三、Secret 使用)
[Opaque 类型 Secret 的使用](#Opaque 类型 Secret 的使用)
一、Secret概述
k8s secrets**用于存储和管理一些敏感数据,比如密码,token,密钥等敏感信息。**它把 Pod 想要访问的加密数据存放到 Etcd 中。然后用户就可以通过在 Pod 的容器里挂载 Volume 的方式或者环境变量的方式访问到这些 Secret 里保存的信息了。
Secret 类似于 ConfigMap,但专门用于保存机密数据。
二、Secret 类型
内置类型 | 用法 |
---|---|
Opaque | 用户定义的任意数据 |
kubernetes.io/service-account-tokensymotion | 服务账号令牌 |
kubernetes.io/dockercfg | ~/.dockercfg 文件的序列化形式 |
kubernetes.io/dockerconfigjson | ~/.docker/config.json 文件的序列化形式 |
kubernetes.io/basic-auth | 用于基本身份认证的凭据 |
kubernetes.io/ssh-auth | 用于 SSH 身份认证的凭据 |
kubernetes.io/tls | 用于 TLS 客户端或者服务器端的数据 |
bootstrap.kubernetes.io/token | 启动引导令牌数据 |
kubectl 创建类型
cpp
[root@k8s-master01 ~]# kubectl create secret dotfile -h
Create a secret using specified subcommand.
Available Commands:
docker-registry 创建一个给 Docker registry 使用的 secret
generic Create a secret from a local file, directory, or literal value
tls 创建一个 TLS secret
Usage:
kubectl create secret [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
-
docker-registry
: 连接私有镜像仓库的凭证(据) -
generic
: 常见 secret, 该类型 secret 与 configmap使用相同 -
tls
: 提供 tls 证书, 在 service mesh 中自动挂载
三、Secret 使用
使用场景:
-
设置容器的环境变量
-
向 Pod 提供 SSH 密钥或密码等凭据
-
允许 kubelet 从私有镜像仓库中拉取镜像
Opaque 类型 Secret 的使用
创建
cpp
[root@k8s-master ~]# kubectl get secrets -n calico-apiserver
NAME TYPE DATA AGE
calico-apiserver-certs Opaque 2 7d
[root@k8s-master ~]# kubectl describe secrets -n calico-apiserver
Name: calico-apiserver-certs
Namespace: calico-apiserver
Labels: <none>
Annotations: <none>
Type: Opaque
Data
====
tls.crt: 2530 bytes
tls.key: 1679 bytes
[root@k8s-master ~]# kubectl create secret
docker-registry (创建一个给 Docker registry 使用的 Secret)
generic (Create a secret from a local file, directory, or literal value)
tls (创建一个 TLS secret)
#它的选项跟configmap是一样的
[root@k8s-master ~]# kubectl create secret generic s1 --from-
--from-env-file (Specify the path to a file to read lines of key=val pairs to create a secret.)
--from-file (Key files can be specified using their file path, in which case a default name will b...)
--from-literal (Specify a key and literal value to insert in secret (i.e. mykey=somevalue))
#创建的secret的名字可以自定义,这里定义名称为s1
[root@k8s-master ~]# kubectl create secret generic s1 --from-literal k1=v1
secret/s1 created
[root@k8s-master ~]# kubectl describe secrets s1
Name: s1
Namespace: default
Labels: <none>
Annotations: <none>
Type: Opaque
Data #数据
====
k1: 2 bytes #所占字节数
#中文字符,一个字符等于三个字节
[root@k8s-master ~]# kubectl create secret generic s2 --from-literal k2=超哥
secret/s2 created
[root@k8s-master ~]# kubectl describe secrets s2
Name: s2
Namespace: default
Labels: <none>
Annotations: <none>
Type: Opaque
Data
====
k2: 6 bytes
明文加密,密文解密简单演示
secret它是可以自动解密的
在创建过程它解密中不可以使用明文,会报错,所以要用密文
cpp
#加密
[root@k8s-master ~]# echo 1234 | base64
MTIzNAo=
#解密
[root@k8s-master ~]# echo MTIzNAo= | base64 -d
1234
#像这种明文解密是会报错的
#所以不能使用明文加密
[root@k8s-master ~]# echo 12345 | base64 -d
�m�64: 无效的输入
#查看secret中的s1详细信息
[root@k8s-master ~]# kubectl get secrets s1 -o yaml
apiVersion: v1
data:
k1: djE= #把k1加密为这个
kind: Secret
metadata:
creationTimestamp: "2025-08-18T16:23:37Z"
name: s1
namespace: default
resourceVersion: "836505"
uid: 7f0863fc-82a2-409e-abf6-6de72a20c81b
type: Opaque
[root@k8s-master ~]# echo djE= | base64 -d
v1[root@k8s-master ~]# #之前我们定义了k1等于v1,这里base64解密出来后就是v1
yaml
以 yaml 方式创建需要你提前进行 base64
secret它是可以自动解密的
在创建过程它解密中不可以使用明文,会报错,所以要用密文
cpp
#yaml文件
以 yaml 方式创建需要你提前进行 base64
[root@k8s-master01 ~]# echo -n "admin" | base64
YWRtaW4=
[root@k8s-master01 ~]# echo -n "123456" | base64
MTIzNDU2
#创建yaml文件,添加下面这段就行
apiVersion: v1
kind: Secret
metadata:
name: secret-volume
namespace: default
type: Opaque
data:
password: MTIzNDU2
username: YWRtaW4=
immutable: true