使用emptyDir本地存储来实现pod中的多个容器数据共享
- 1、应用场景
在同一个pod中同时部署多个容器,多个容器间需要在运行期间公用某份数据。但是在pod销毁后上面存储的东西不再需要的场景 - 2、存储特点
随着pod被删除,该卷也会被删除 - 3、具体示例:
创建如下一个pod容器,volume-emptydir.yml文件如下:
apiVersion: v1
kind: Pod
metadata:
name: volume-emptydir
spec:
containers:
- name: write
image: centos:centos7
imagePullPolicy: IfNotPresent
command: ["bash", "-c", "echo haha > /data/1.txt ;sleep 600000" ]
volumeMounts:
- name: data
mountPath: /data
- name: read
image: centos:centos7
imagePullPolicy: IfNotPresent
command: ["bash", "-c", "cat /data/1.txt ; sleep 600000"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {
}
运行容器并且查看容器的运行情况
[root@k8s-master1 volume]# kubectl apply -f volume-emptydir.yml
pod/volume-emptydir created
[root@k8s-master1 volume]# kubectl get pods
NAME READY STATUS RESTARTS AGE
details-v1-79f774bdb9-jt6sj 2/2 Running 2 35h
nginx-web-2jljc 1/1 Running 1 47h
nginx-web-ldtwj 1/1 Running 1 47h
productpage-v1-6b746f74dc-9wg76 2/2 Running 2 35h
ratings-v1-b6994bb9-mtvm7 2/2 Running 2 35h
reviews-v1-545db77b95-jqq5g 2/2 Running 2 35h
reviews-v2-7bf8c9648f-mw4xb 2/2 Running 2 35h
reviews-v3-84779c7bbc-jt6qx 2/2 Running 2 35h
volume-emptydir 3/3 Running 0 6s
通过上面的查看,我们已经成功运行了pod。然后我们分别进入到write容器和read容器上查看挂在/data目录的下文件
- 查看write容器里面的数据
[root@k8s-master1 volume]# kubectl exec -it volume-emptydir -c write -- bash
[root@volume-emptydir /]# cd /data
[root@volume-emptydir data]# ls
1.txt
[root@volume-emptydir data]# cat 1.txt
haha
[root@volume-emptydir data]#
- 查看read容器里面的数据
[root@k8s-master1 volume]# kubectl exec -it volume-emptydir -c read -- bash
[root@volume-emptydir /]# cd /data
[root@volume-emptydir data]# ls
1.txt
[root@volume-emptydir data]# cat 1.txt
haha
从上面可以看出来,read和write容器中均可以看到存储emptyDir里面的数据