linux运维、架构之路-K8s Secret & Configmap

一、介绍

       应用启动过程中可能需要一些敏感信息,比如访问数据库的用户名、密码或者秘钥等。这些信息保存在镜像中不太好。Secret会以密文方式存储数据,避免直接在配置文件中保存敏感信息,Secret会以Volume的形式被mount到Pod,容器通过文件的方式使用Secret中的敏感数据;此外,容器也可以通过环境变量的方式使用这些数据。

二、Secret

1、创建Secret

①通过 --from-litera

kubectl create secret generic mysecret --from-literal=username=admin --from-literal=password=123456
[root@k8s-node1 ~]# kubectl get secret 
NAME                  TYPE                                  DATA      AGE
default-token-ff5wq   kubernetes.io/service-account-token   3         13d
mysecret              Opaque                                2         6s

②通过 --from-file

kubectl create secret generic mysecret --from-file=./username --from-file=./password

③通过 --from-env-file

[root@k8s-node1 ~]# cat <<EOF >env.txt
> username=admin
> password=123456
> EOF
[root@k8s-node1 ~]# cat env.txt 
username=admin
password=123456
[root@k8s-node1 ~]# kubectl create secret generic mysecret --from-env-file=env.txt

④通过YAML配置文件

#文件中的敏感数据通过base64编码后#
[root@k8s-node1 ~]# echo -n admin|base64
YWRtaW4=
[root@k8s-node1 ~]# echo -n 123456|base64
MTIzNDU2

猜你喜欢

转载自www.cnblogs.com/yanxinjiang/p/12172536.html