K8s系列之:数据卷挂载–emptyDir

含义: 是一个临时存储卷,与Pod生命周期绑定一起,如果
Pod删除了卷也会被删除

适用场景:Pod中容器之间数据共享

------------yaml文件(此行不要复制) vim my-pod-dirtest.yaml-------------------
apiVersion: v1
kind: Pod
metadata:
  name: my-pod-emptyDir
spec:
  containers:
  - name: write
    image: centos
    command: ["bash","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
    volumeMounts:
      - name: data
        mountPath: /data
  - name: read
    image: centos
    command: ["bash","-c","tail -f /data/hello"]
    volumeMounts:
      - name: data
        mountPath: /data
  volumes:
  - name: data
    emptyDir: {}

执行yaml文件

shell>kubectl apply -f my-pod-dirtest.yaml

查看效果:

1、查看目录(kubectl get pod -A -o wide)

----------1、首先查看pod的临时目录---------------------
shell>docker ps |grep dir
[root@master ~]# docker ps |grep dir
5431c11f4ab8   k8s.gcr.io/pause:3.2   "/pause"                 20 seconds ago   Up 19 seconds             k8s_POD_my-pod-dir_default_c6d
-----------2、进入目录-------------------------------
[root@master ~]# cd /var/lib/kubelet/pods/c6d92106-a33f-4950-83a2-c8b9b52e444f/
[root@master c6d92106-a33f-4950-83a2-c8b9b52e444f]# ll
total 4
drwxr-x--- 4 root root  31 Sep 15 20:34 containers
-rw-r--r-- 1 root root 206 Sep 15 20:37 etc-hosts
drwxr-x--- 3 root root  37 Sep 15 20:33 plugins
drwxr-x--- 4 root root  65 Sep 15 20:33 volumes
[root@master c6d92106-a33f-4950-83a2-c8b9b52e444f]# cd volumes/
[root@master volumes]# ll
total 0
drwxr-xr-x 3 root root 18 Sep 15 20:33 kubernetes.io~empty-dir
drwxr-xr-x 3 root root 33 Sep 15 20:33 kubernetes.io~secret
[root@master volumes]# 
[root@master volumes]# 
[root@master volumes]# cd kubernetes.io~empty-dir/
[root@master kubernetes.io~empty-dir]# ll
total 0
drwxrwxrwx 2 root root 19 Sep 15 20:34 data
[root@master kubernetes.io~empty-dir]# cd data
[root@master data]# 
[root@master data]# ll
total 4
-rw-r--r-- 1 root root 999 Sep 15 20:41 hello
---------------------3、动态获取hello----------
[root@master data]# tail -f hello 
61
62
63
-----------------挂卷成功-------------------
k8s、新兴技术能力K8s系列之:数据卷挂载–emptyDir插图
k8s、新兴技术能力K8s系列之:数据卷挂载–emptyDir插图1

2、进入K8s容器查看/data/hello

--------------1、查看NAME-----------------
[root@master ~]# kubectl get pod -A -o wide
NAMESPACE       NAME                                    READY   STATUS             RESTARTS   AGE    IP             NODE     NOMINATED NO
cattle-system   cattle-cluster-agent-76cbdd8888-x78sl   1/1     Running            122        19d    10.244.1.76    node1    <none>      
default         hello-deploy-58b46fbf84-k9wgv           1/1     Running            1          4d3h   10.244.0.65    master   <none>      
default         my-pod-dir                              2/2     Running            5          12m    10.244.0.67    master   <none>     

--------------2、进入容器pod-----------------
[root@master ~]# kubectl exec -it  my-pod-dir -n default -- bash
Defaulting container name to write.
Use 'kubectl describe pod/my-pod-dir -n default' to see all of the containers in this pod.
[root@my-pod-dir /]# 

----------------3、查看文件hello
[root@my-pod-dir /]# cd /data/
[root@my-pod-dir data]# ll
bash: ll: command not found
[root@my-pod-dir data]# ls
hello
[root@my-pod-dir data]# tail hello 

k8s、新兴技术能力K8s系列之:数据卷挂载–emptyDir插图2