Secret 解决了密码、token、密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者 Pod Spec
中。Secret 可以以 Volume 或者环境变量的方式使用。
Secret 有三种类型:
/run/secrets/kubernetes.io/serviceaccount
目录中Service Account 用来访问 Kubernetes API,由 Kubernetes 自动创建,并且会自动挂载到 Pod的 /run/secrets/kubernetes.io/serviceaccount
目录中
进入容器查看
[root@k8s-master01 ~]# kubectl exec -it kube-proxy-826j6 -n kube-system /bin/sh
# cd /run/secrets/kubernetes.io/serviceaccount
# ls
ca.crt namespace token
# cat ca.crt
-----BEGIN CERTIFICATE-----
MIICyDCCAbCgAwIBAgIBADANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwprdWJl
...
N4yMcP/7+bvfwoklBhzO187HyUtc/iwru5PPxXnnHGXn7jIncjVBbZRJ9+I=
-----END CERTIFICATE-----
# cat namespace
kube-system
cat token
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiw
...
GiWpdCefg
Opaque 类型的数据是一个 map 类型,要求 value 是 base64 编码格式:
[root@k8s-master01 ~]# echo -n "admin" | base64
YWRtaW4=
[root@k8s-master01 ~]# echo -n "1f2d1e2e67df" | base64
MWYyZDFlMmU2N2Rm
secrets.yml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: MWYyZDFlMmU2N2Rm
username: YWRtaW4=
查看secrets
kubectl get secrets
1、将 Secret 挂载到 Volume 中
apiVersion: v1
kind: Pod
metadata:
labels:
name: secret-test
name: secret-test
spec:
volumes:
- name: secrets
secret:
secretName: mysecret
containers:
- image: hub.atguigu.com/library/myapp:v1
name: db
volumeMounts:
- name: secrets
mountPath: "/etc/secrets"
readOnly: true
创建好pod,进入容器查看。
[root@k8s-master01 ~]# kubectl exec -it secret-test /bin/sh
# cd /etc/secrets
# ls
username password
2、将 Secret 导出到环境变量中
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: pod-deployment
spec:
replicas: 2
template:
metadata:
labels:
app: pod-deployment
spec:
containers:
- name: pod-1
image: hub.atguigu.com/library/myapp:v1
ports:
- containerPort: 80
env:
- name: TEST_USER
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: TEST_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
创建好pod,进入容器查看变量。
[root@k8s-master01 ~]# kubectl exec -it pod-deployment /bin/sh
# echo $TEST_USER
admin
# echo $TEST_PASSWORD
1f2d1e2e67df
使用 Kuberctl 创建 docker registry 认证的 secret
kubectl create secret docker-registry myregistrykey --docker-server=hub.atguigu.com --docker-username=admin --docker-password=Harbor12345 --docker-email=123@163.com
创建pod进行测试。
apiVersion: v1
kind: Pod
metadata:
name: foo
spec:
containers:
- name: foo
image: hub.atguigu.com/library/myapp:v1
imagePullSecrets:
- name: myregistrykey
将Harbor上的仓库设置为私有。
将节点上的镜像 hub.atguigu.com/library/myapp:v1
删除,并且使用 docker logout hub.atguigu.com
进行退出,资源清单中可以先不加入 imagePullSecrets
创建pod,此时会创建失败,镜像因为没有密钥无法下载。删除失败的pod,加入 imagePullSecrets
后再次创建pod,此时可以创建成功!
# 删除节点上的镜像
[root@k8s-node01 ~]# docker rmi hub.atguigu.com/library/myapp:v1
Untagged: hub.atguigu.com/library/myapp:v1
Untagged: hub.atguigu.com/library/myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e
# docker退出登录
[root@k8s-node01 ~]# docker logout hub.atguigu.com
Removing login credentials for hub.atguigu.com
# 尝试下载镜像(失败)
[root@k8s-node01 ~]# docker pull hub.atguigu.com/library/myapp:v1
Error response from daemon: pull access denied for hub.atguigu.com/library/myapp, repository does not exist or may require 'docker login': denied: requested access to the resource is denied