Kubernetes volume数据卷
Volume是kubernetes Pod中多个容器访问的共享目录
Kubernetes提供了众多的volume类型,包括:emptyDir、hostPath、NFS、GlusterFS、configMap、Cephfs
1、emptyDir
emptyDir类型的vloume在Pod分配到node上时被创建,kubernetes会在node上自动分配一个目录,所以无须指定宿主机node上对应的目录文件,该目录初始化内容为空,当Pod从node上被移除时,emptyDir中的数据会被永久删除
1.编辑emptyDir.yaml
文件
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: web-demo-empty spec: replicas: 2 selector: matchLabels: app: app-demo-empty template: metadata: labels: app: app-demo-empty spec: containers: - name: tomcat-demo image: tomcat imagePullPolicy: IfNotPresent ports: - containerPort: 8080 volumeMounts: # 将/mydata-data目录挂载到共享仓库 - mountPath: /mydata-data name: datavol - name: nginx-demo image: nginx imagePullPolicy: IfNotPresent ports: - containerPort: 80 volumeMounts: # 将/mydata-data目录挂载到共享仓库 - mountPath: /mydata-data name: datavol # emptyDir是一个共享仓库(中央仓库) volumes: - name: datavol emptyDir: {}
2.执行kubectl create
$ kubectl create -f emptyDir.yaml deployment.extensions/web-demo-empty created
3.先进入tomcat-demo
容器中创建文件
$ kubectl exec -it web-demo-empty-9b5644c45-7bpn5 -c tomcat-demo bash $ touch /mydata-data/data.txt $ ls /mydata-data/ data.txt
4.在进入nginx-demo
容器查看是否存在data.txt
文件
$ kubectl exec -it web-demo-empty-9b5644c45-7bpn5 -c nginx-demo bash $ ls /mydata-data/ data.txt
经过试验证明emptyDir是一个共享空目录,可以让Pod中的多个容器之间共享该目录
2、hostPath
hostPath类型的vloume为Pod挂载到宿主机上的目录或文件,使得容器可以使用宿主机的文件系统进行存储,但是在kubernetes中,Pod都是基于schedule
组件来动态调度在不同的node节点上,当一个Pod在当前node节点上启动并通过hostPath
存储了文件到本地后,下次在调度另外一个节点上启动时,就无法使用之前节点上存储的文件了。
1.编辑hostPath.yaml
文件
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: web-demo-hostpath spec: replicas: 2 selector: matchLabels: app: app-demo-hostpath template: metadata: labels: app: app-demo-hostpath spec: containers: containers: - name: nginx-demo image: nginx imagePullPolicy: IfNotPresent ports: - containerPort: 80 volumeMounts: - mountPath: /mydata-data name: datavol volumes: - name: datavol hostPath: path: "/data"
2.执行kubectl create
$ kubectl create -f hostPath.yaml deployment.extensions/web-demo-hostpath created
3.需要在每个node节点创建/data
目录,并创建测试文件
$ mkdir /data $ touch /data/test.txt
4.进入容器里查看是否将/mydata-data
挂载到了宿主机上
$ kubectl exec -it web-demo-hostpath-7866c644c4-7f8fk bash $ ls /mydata-data/ test.txt
相关推荐
远远的山 2020-11-09
kaidiphp 2020-10-13
JustHaveTry 2020-09-29
cloudvtech 2020-09-18
BigDataMining 2020-08-21
朱培知浅ZLH 2020-11-16
cdbdqn00 2020-11-12
达观数据 2020-11-11
JustinChia 2020-11-11
jingtao 2020-11-08
大叔比较胖 2020-10-30
gracecxj 2020-10-30
onepiecedn 2020-10-29
kunyus 2020-10-28
JustHaveTry 2020-10-27
锋锋 2020-10-26
hubanbei00的家园 2020-10-25
谢恩铭 2020-10-23
btqszl 2020-10-21