数据卷
- 容器中的文件时临时存放在磁盘上的。
- 当容器崩溃和升级时,kubectl会进行容器的重建,会导致容器内的文件会丢失。
- 一个pod中运行多个容器需要共享文件。
常见的数据卷
- 节点本地(hostPath,emptyDir)
- 网络 (NFS,Ceph,GlusterFS)
- 共有云 (AWS,EBS)
- K8S资源 (configMap,secret)
emptyDir 数据卷
- emptyDir卷是一个临时存储卷(Pod所在的节点),与Pod生命周期捆绑在一起,当pod被删除,卷也会跟着被删除
- Pod中容器之间数据共享(应用场景)
emptyDir 案例
1、创建2个容器,将2个容器间的/usr/share/nginx/html和/tmp 目录做共享
[root@k8s-master ~]# vim emptydir.yaml
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: nginx1
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: share
- name: tomcat2
image: tomcat
volumeMounts:
- mountPath: "/tmp"
name: share
volumes:
- name: share
emptyDir: {}
[root@k8s-master ~]# kubect apply -f emptydir.yaml
[root@k8s-master ~]# kubectl exec -it test -c nginx1 -- bash
root@test:/usr/share/nginx/html# touch 88888888
root@test:/usr/share/nginx/html# exit
[root@k8s-master ~]# kubectl exec -it test -c tomcat2 -- bash
root@test:/tmp# ls
88888888
root@test:/tmp# mkdir ./test
[root@k8s-master ~]# kubectl exec -it test -c nginx1 -- bash
root@test:/# ls /usr/share/nginx/html/
88888888 test
hostPath 案例
本地tmp目录下的文件
[root@k8s-master tmp]# vim /root/hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
name: feng
spec:
containers:
- name: http
image: nginx
volumeMounts:
- name: mount
mountPath: /usr/local
volumes:
- name: mount
hostPath:
path: /tmp
type: Directory
[root@k8s-master tmp]# kubectl apply -f /root/hostpath.yaml
[root@k8s-master tmp]# kubectl exec -it feng -- bash
root@feng:/usr/local# ls
vmware-root_699-3979839557 vmware-root_700-2730627996 vmware-root_701-3979708482 vmware-root_709-4248287236 vmware-root_712-2957059153