volume是k8s数据卷,常见的数据卷有4种类型,即EmptyDir,HostDir,NFS,Secret
EmptyDir
EmptyDir是一个空目录,他的生命周期和所属的 Pod 是完全一致的,
可能读者会奇怪,那还要他做什么?EmptyDir的用处是,可以在同一 Pod 内的不同容器之间共享工作过程中产生的文件。
一旦这个pod离开了这个宿主机,EmptyDirr中的数据就会被永久删除
[root@k8s-master demon2]# cat test-emptypath.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
name: test-emptypath
role: master
name: test-emptypath
spec:
containers:
- name: test-emptypath
image: registry:5000/back_demon:1.0
volumeMounts:
- name: log-storage
mountPath: /home/laizy/test/
command:
- /run.sh
volumes:
- name: log-storage
emptyDir: {}
HostDir
HostDir属性的volume使得对应的容器能够访问当前宿主机上的指定目录。
[root@k8s-master demon2]# cat test-hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
name: test-hostpath
role: master
name: test-hostpath
spec:
containers:
- name: test-hostpath
image: registry:5000/back_demon:1.0
volumeMounts:
- name: ssl-certs
mountPath: /home/laizy/test/cert
readOnly: true
command:
- /run.sh
volumes:
- name: ssl-certs
hostPath:
path: /etc/ssl/certs
NFS
支持网络存储
[root@k8s-master demon2]# cat test-nfspath.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
name: test-nfspath
role: master
name: test-nfspath
spec:
containers:
- name: test-nfspath
image: registry:5000/back_demon:1.0
volumeMounts:
- name: nfs-storage
mountPath: /home/laizy/test/
command:
- /run.sh
volumes:
- name: nfs-storage
nfs:
server: 192.168.20.47
path: "/data/disk1"
Secret
Secret:Kubemetes提供了Secret来处理敏感数据
[root@k8s-master demon2]# cat secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: emhlbnl1
password: eWFvZGlkaWFv
[root@k8s-master demon2]# cat test-secret.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
name: test-secret
role: master
name: test-secret
spec:
containers:
- name: test-secret
image: registry:5000/back_demon:1.0
volumeMounts:
- name: secret
mountPath: /home/laizy/secret
readOnly: true
command:
- /run.sh
volumes:
- name: secret
secret:
secretName: mysecret
关于Secret 在我另外一篇有详细说明
https://www.jianshu.com/p/3bf692ee092e
网友评论