一.Secret概述
Secret 对象类型用来保存敏感信息,例如密码、OAuth 令牌和 ssh key。
敏感信息放在 secret 中比放在 Pod 的定义或者容器镜像中来说更加安全和灵活。
Pod 可以用两种方式使用 secret:
作为 volume 中的文件被挂载到 pod 中的一个或者多个容器里。
当 kubelet 为 pod 拉取镜像时使用。
Secret的类型:
Service Account:Kubernetes 自动创建包含访问 API 凭据的 secret,并自动修改 pod 以使用此类型的 secret。
Opaque:使用base64编码存储信息,可以通过base64 --decode解码获得原始数据,因此安全性弱。
kubernetes.io/dockerconfigjson:用于存储docker registry的认证信息。
二.Service Account
serviceaccout 创建时 Kubernetes 会默认创建对应的 secret。对应的 secret 会自动挂载到 Pod 的 /run/secrets/kubernetes.io/serviceaccount 目录中。
[root@server2 ~]# kubectl run nginx --image=nginx
pod/nginx created
[root@server2 ~]# kubectl exec -it nginx -- bash
root@nginx:/# cd /var/run/secrets/kubernetes.io/serviceaccount/
root@nginx:/var/run/secrets/kubernetes.io/serviceaccount# ls
ca.crt namespace token
每个namespace下有一个名为default的默认的ServiceAccount对象
[root@server2 ~]# kubectl get secrets
NAME TYPE DATA AGE
default-token-6k92n kubernetes.io/service-account-token 3 7d1h
kubectl get pod -o yaml
volumeMounts:
- mountPath: /etc/nginx/conf.d
name: config-volume
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-shcd7
readOnly: true
Opaque Secret
Opaque Secret 其value为base64编码后的值。
[root@server2 ~]# echo -n 'admin' > ./username.txt
[root@server2 ~]# echo -n 'westos' > ./password.txt
[root@server2 ~]# kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
secret/db-user-pass created
[root@server2 ~]# kubectl get secrets
NAME TYPE DATA AGE
db-user-pass Opaque 2 25s
默认情况下 kubectl get和kubectl describe 为了安全是不会显示密码的内容,可以通过以下方式查看:
[root@server2 ~]# kubectl get secrets db-user-pass -o yaml
apiVersion: v1
data:
password.txt: d2VzdG9z
username.txt: YWRtaW4=
kind: Secret
metadata:
creationTimestamp: "2021-08-01T03:31:57Z"
name: db-user-pass
namespace: default
resourceVersion: "860331"
uid: 364ad64c-5f9d-42d8-bc67-59f75fa06c63
type: Opaque
当然这样只是简单的base64加密 解码就会变成明文
secret应用
[root@server2 ~]# echo -n 'admin' | base64
YWRtaW4=
[root@server2 ~]# echo -n 'westos'
westos[root@server2 ~]# echo -n 'westos'| base64
d2VzdG9z
[root@server2 ~]# mkdir secret
[root@server2 ~]# cd secret/
[root@server2 secret]# ls
[root@server2 secret]# vim mysecret.yaml
[root@server2 secret]# cat mysecret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: d2VzdG9z
---
apiVersion: v1
kind: Pod
metadata:
name: mysecret
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: secrets
mountPath: "/secret"
readOnly: true
volumes:
- name: secrets
secret:
secretName: mysecret
[root@server2 secret]# kubectl apply -f mysecret.yaml
secret/mysecret created
pod/mysecret created
[root@server2 secret]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-b9b58dbdf-mzs92 1/1 Running 0 169m
mysecret 1/1 Running 0 8m44s
nginx 1/1 Running 0 164m
查看volume下的password文件
[root@server2 secret]# kubectl exec -it mysecret bash
root@mysecret:/# cat /secret/password
westos
[root@server2 secret]# kubectl delete -f mysecret.yaml
secret "mysecret" deleted
pod "mysecret" deleted
向指定路径映射 secret 密钥
指定路径/secret/my-group/my-username
[root@server2 secret]# kubectl apply -f mysecret.yaml
secret/mysecret created
pod/mysecret created
[root@server2 secret]# kubectl exec -it mysecret bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@mysecret:/# cat /secret/my-group/my-username
admin
[root@server2 secret]# cat mysecret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: d2VzdG9z
---
apiVersion: v1
kind: Pod
metadata:
name: mysecret
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: secrets
mountPath: "/secret"
readOnly: true
volumes:
- name: secrets
secret:
secretName: mysecret
items:
- key: username
path: my-group/my-username
将Secret设置为环境变量
环境变量读取Secret很方便,但无法支撑Secret动态更新。
[root@server2 secret]# kubectl apply -f secret.yaml
pod/secret-env created
[root@server2 secret]# kubectl get pod
secret-env 1/1 Running 0 14s
[root@server2 secret]# kubectl exec secret-env env
SECRET_USERNAME=admin
SECRET_PASSWORD=westos
[root@server2 secret]# cat secret.yaml
apiVersion: v1
kind: Pod
metadata:
name: secret-env
spec:
containers:
- name: nginx
image: nginx
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
docker registry
kubernetes.io/dockerconfigjson用于存储docker registry的认证信息
[root@server2 secret]# kubectl create secret docker-registry myregistrykey --docker-server=reg.westos.org --docker-username=admin --docker-password=westos --docker-email=root@westos.org
secret/myregistrykey created
[root@server2 secret]# cat mypod.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: reg.westos.org/westos/nginx
#imagePullSecrets:
#
# # - name: myregistrykey
[root@server2 secret]# kubectl apply -f mypod.yaml
pod/mypod created
[root@server2 secret]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 0/1 ContainerCreating 0 11s
你可以使用下面的命令执行体面地删除 Pod:
kubectl delete pods pod
[root@server2 secret]# kubectl apply -f mypod.yaml
pod/mypod created
[root@server2 secret]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 88s