[root@k8s01 ~]# kubectl apply -f emptyDir-example.yaml pod/test-pd created [root@k8s01 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE test-pd 2/2 Running 0 3s
[root@k8s01 ~]# kubectl exec test-pd -c test-container01 -it -- /bin/bash # 进入test-container01 下的/cache 目录 root@test-pd:/# cd cache/ root@test-pd:/cache# date >> index.txt root@test-pd:/cache# cat index.txt Mon Aug 31 17:03:13 UTC 2020
### 新开一个窗口 [root@k8s01 ~]# kubectl exec test-pd -c test-container02 -it -- /bin/sh # 进入test-container02 下的 /test 目录 / # cd test /test # ls # 发现与 test-container01 下的 /cache 目录 文件内容相同 index.txt /test # cat index.txt Mon Aug 31 17:03:13 UTC 2020 /test # date >> index.txt /test # cat index.txt Mon Aug 31 17:03:13 UTC 2020 Mon Aug 31 17:05:43 UTC 2020
### 第一个窗口 root@test-pd:/cache# cat index.txt # 内容也同步更新 Mon Aug 31 17:03:13 UTC 2020 Mon Aug 31 17:05:43 UTC 2020
[root@k8s03 ~]# mkdir /data [root@k8s01 ~]# kubectl describe pod test-pd02 # 节选 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 4m42s default-scheduler Successfully assigned default/test-pd02 to k8s03 Warning FailedMount 3m38s (x8 over 4m41s) kubelet, k8s03 MountVolume.SetUp failed for volume "test-volume" : hostPath type check failed: /data is not a directory Warning FailedMount 2m39s kubelet, k8s03 Unable to attach or mount volumes: unmounted volumes=[test-volume], unattached volumes=[test-volume default-token-rr77c]: timed out waiting for the condition Normal Pulled 2m26s kubelet, k8s03 Container image "nginx:1.7.9" already present on machine Normal Created 2m26s kubelet, k8s03 Created container test-container Normal Started 2m26s kubelet, k8s03 Started container test-container
[root@k8s01 ~]# kubectl exec test-pd02 -it -- /bin/bash root@test-pd02:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin selinux srv sys test-pd tmp usr var root@test-pd02:/# cd test-pd/ root@test-pd02:/test-pd# date >> index.txt root@test-pd02:/test-pd# cat index.txt Mon Aug 31 17:25:09 UTC 2020
[root@k8s03 ~]# cat /data/index.txt # /data 下已有 index.txt 文件,内容相同 Mon Aug 31 17:25:09 UTC 2020 [root@k8s03 ~]# date >> /data/index.txt # /data/index.txt 写入 [root@k8s03 ~]# cat /data/index.txt Mon Aug 31 17:25:09 UTC 2020 Mon Aug 31 13:27:03 EDT 2020
root@test-pd02:/test-pd# cat index.txt # 容器内也是相同内容 Mon Aug 31 17:25:09 UTC 2020 Mon Aug 31 13:27:03 EDT 2020
[root@k8s01 ~]# kubectl delete pod test-pd02 # 删除 Pod pod "test-pd02" deleted
[root@k8s03 ~]# cat /data/index.txt # 文件还在 k8s03 节点存在 Mon Aug 31 17:25:09 UTC 2020 Mon Aug 31 13:27:03 EDT 2020
Volume 目录与挂载流程
当一个 Pod 调度到一个节点上之后,kubelet 就要负责为这个 Pod 创建它的 Volume 目录。默认情况下,kubelet 为 Volume 创建的目录是如下所示的一个宿主机上的路径: