Use emptyDir to share data among multiple containers in a pod

Use emptyDir local storage to share data among multiple containers in a pod

  • 1. Application Scenarios
    Multiple containers are deployed in the same pod at the same time, and certain data needs to be shared between multiple containers during operation. But the scenario where the things stored above are no longer needed after the pod is destroyed
  • 2. Storage characteristics
    When the pod is deleted, the volume will also be deleted
  • 3. Specific example:
    Create the following pod container, and the volume-emptydir.yml file is as follows:
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: {
    
    }

Run the container and view the running status of the container

[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

Through the above inspection, we have successfully run the pod. Then we enter the write container and the read container to view the files hanging in the /data directory

  • View the data in the write container
[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]# 
  • View the data in the read container
[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

As can be seen from the above, the data stored in emptyDir can be seen in both the read and write containers

Guess you like

Origin blog.csdn.net/zhangshenglu1/article/details/130876367