Opaque类型的Secret,创建secret时传递的value值都是base64编码后的值,传到容器里面的值是base64解码后的值。
例如lzj通过base64编码后的值为bHpqCg==
,lzj@lzj通过base64编码后的值为bHpqQGx6ago=
,下面创建secret.yaml文件
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: bHpqCg==
password: bHpqQGx6ago=
创建secret,kubectl apply -f secret.yaml
首先创建一个user.conf文件,文件中添加内容:
lzj
lzj@lzj
通过文件创建secret
kubectl create secret generic mysecret2 --from-file=user.conf
查看创建的mysecret2,kubectl get secret mysecret2 -o yaml
,发现user.conf内容已经被编码base64进行传递
apiVersion: v1
data:
user.conf: bHpqCmx6akBsemoK
kind: Secret
metadata:
creationTimestamp: "2020-08-19T15:23:41Z"
name: mysecret2
namespace: default
resourceVersion: "8630508"
selfLink: /api/v1/namespaces/default/secrets/mysecret2
uid: de58bb77-3ce6-4930-a607-61fc6bb1e6d2
type: Opaque
将上面创建的mysecret2挂载到volume中,容器从volume卷中读取账户密码文件。首先创建pod为例,如下所示:
apiVersion: v1
kind: Pod
metadata:
name: secret-pod1
spec:
containers:
- name: mynginx1
image: nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: user
mountPath: /home/conf
readOnly: true
volumes:
- name: user
secret:
secretName: mysecret2
创建pod ,kubectl apply -f pod1.yaml
,然后进容器查看传递的mysecret2,发现传递到容器的user.conf已经被解码
[root@k8s-master01 pod]# kubectl exec -it secret-pod1 -- /bin/sh
# cd /home/conf
# ls
user.conf
# cat user.conf
lzj
lzj@lzj
将上面创建的mysecret导入到容器环境变量中,创建mysecret用的base64编码的值,导入到容器中后就是解码的值了。首先创建pod的yaml如下
apiVersion: v1
kind: Pod
metadata:
name: secret-pod2
spec:
containers:
- image: busybox
name: buysbox1
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c", "sleep 300"]
env:
- name: USER_NAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: USER_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
创建该pod,并进入容器查看环境变量,发现已经时解码后的值
[root@k8s-master01 pod]# kubectl apply -f pod2.yaml
pod/secret-pod2 created
[root@k8s-master01 pod]# kubectl exec secret-pod2 -it -- /bin/sh
/ # env
......
USER_PASSWORD=lzj@lzj
USER_NAME=lzj
......
Serice Account类型的Secret会默认挂载到所有容器中,Serice Account用来访问k8s API,由k8s自动创建,并且创建后会自动挂载到/var/run/secrets/kubernetes.io/serviceaccount目录中。对于每一个pod执行describe命令可以查看到挂载的default-token-sk5fk的secret。
[root@k8s-master01 pod]# kubectl describe pod secret-pod2
Name: secret-pod2
Namespace: default
......
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-sk5fk (ro)
Conditions:
Volumes:
default-token-sk5fk:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-sk5fk
Optional: false
[root@k8s-master01 pod]# kubectl get secret
NAME TYPE DATA AGE
default-token-sk5fk kubernetes.io/service-account-token 3 82d
mysecret Opaque 2 2d2h
mysecret2 Opaque 1 2d
创建docker仓库用来鉴权的registry,比如创建一个对docker hub的鉴权,在k8s中运行容器时,可以直接从docker hub上pull镜像。达到此功能,需创建一个docker-registry类型的secret,然后指定登录docker hub的用户名、密码、邮箱,然后在创建pod时运用该secret即可。
1、首先创建一个docker-registry类型的secret
kubectl create secret docker-registry myregistry --docker-server=hub.docker.com --docker-username=xxxxxxx --docker-password=xxxxxx --docker-email=xxxxx@163.com
2、创建pod,利用secret从docker hub中拉取镜像
apiVersion: v1
kind: Pod
metadata:
name: myhello
spec:
containers:
- name: myhello
image: softwarebird/my_hello
imagePullSecrets:
- name: myregistry
通过yaml创建pod,容器会从docker hub上拉取softwarebird/my_hello最新的镜像。