Secret 能够用来保存密码、密钥等敏感信息,避免密钥直接放在 Pod 的YAML定义文件或容器镜像中致使的泄露问题。html
密钥使用 Base64 编码形式存储于 Secret 对象中,Pod 挂载后自动解码为明文。linux
建立用户名/密码文件redis
echo -n 'username' > ./username.txt echo -n 'password' > ./password.txt
写入 Secret 对象中api
# kubectl create secret generic db-info --from-file=./username.txt --from-file=./password.txt secret/db-info created
检查 Secret安全
# kubectl get secret NAME TYPE DATA AGE db-info Opaque 2 106s
查看刚写入的 db-info 的详细信息服务器
# kubectl describe secret db-info Name: db-info Namespace: default Labels: <none> Annotations: <none> Type: Opaque Data ==== username.txt: 8 bytes password.txt: 8 bytes
查看密钥的值微信
# kubectl get secret db-info -o yaml
先将要保存的值进行 Base64 编码app
# echo -n 'username' | base64 dXNlcm5hbWU= # echo -n 'password' | base64 cGFzc3dvcmQ=
apiVersion: v1 kind: Secret metadata: name: mysecret type: Opaque data: username: base64编码 password: base64编码
建立 secret编码
# kubectl apply -f secret.yaml secret/mysecret created
查看加密
# kubectl get secret NAME TYPE DATA AGE mysecret Opaque 2 2m5s
查看密钥的值
# kubectl get secret mysecret -o yaml
编辑 secret
kubectl edit secrets mysecret
apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: mypod image: redis volumeMounts: - name: foo mountPath: "/etc/foo" readOnly: true volumes: - name: foo secret: secretName: mysecret
以上示例中,
将名为 mysecret 的 secret 对象映射为卷,卷名为 foo,
将名为 foo 的卷挂载到 Pod 中的路径 /etc/foo 下面,
mysecret 中username/password两个key,分别映射为文件。
多个 Pod 能够共享一个卷。
能够进入 Pod 中查看这两个文件内容
# kubectl exec -it mypod -- ls /etc/foo/ password username # kubectl exec -it mypod -- cat /etc/foo/username admin # kubectl exec -it mypod -- cat /etc/foo/password password
Secret 对象将重要性高的秘钥和 Pod 进行了解耦处理。
它有以下特色:
官方安全建议:
微信公众号:zuolinux_com