Docker卷与Kubernetes持久卷 [英] Docker volume vs Kubernetes persistent volume

查看:73
本文介绍了Docker卷与Kubernetes持久卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到的最接近的答案是. /p>

但是我想知道的是,Kubernetes是否会完全忽略Dockerfile VOLUME命令?还是数据将被保留在两个地方?一个用于docker卷(在运行哪个pod的主机中),另一个是Kubernetes的PV?

问这个的原因是因为我从docker hub部署了一些包含VOLUME命令的容器.同时,我还将PVC贴到我的吊舱上.我在考虑是否在节点中创建本地卷(docker卷,而不是K8 PV)?如果我的Pod已安排到另一个节点,那么是否创建了另一个新卷?


最重要的是,感谢@Rico指出-v命令和Kubernetes的安装将优先于dockerfile VOLUME命令,但是如果出现以下情况,该怎么办:

  • dockerfile VOLUME到'/myvol'

  • Kubernetes将PVC安装到'/anotherMyVol'

在这种情况下,myvol是否可以安装到我的本地节点硬盘上?并导致不知道的数据在本地持久保存?

解决方案

除非您在Kubernetes pod规范中覆盖它,否则它将不会被忽略.例如,如果您遵循Docker文档中的示例:

$ docker run -it container bash
root@7efcf5ef12a2:/# mount | grep myvol
/dev/nvmeXnXpX on /myvol type ext4 (rw,relatime,discard,data=ordered)
root@7efcf5ef12a2:/#

您将看到它已安装在运行容器的主机的根驱动器上. Docker实际上在/var/lib/docker/volumes下的主机文件系统上创建了一个卷(/var/lib/docker是您的Docker图形目录):

$ pwd
/var/lib/docker/volumes
$ find . | grep greeting
./d0bc20d085243c39c4f386dce2f6cafcd8146128d6b0c8f9dcb27cfb61a7ecab/_data/greeting

您可以在Docker中使用 -v 选项覆盖此参数:

$ docker run -it -v /mnt:/myvol container bash
root@1c7211cf43d0:/# cd /myvol/
root@1c7211cf43d0:/myvol# touch hello
root@1c7211cf43d0:/myvol# exit
exit
$ pwd # <= on the host
/mnt
$ ls
hello

因此,在Kubernetes上,您可以在pod规范中覆盖它:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mycontainer
    image: container
    volumeMounts:
    - name: storage
      mountPath: /myvol
  volumes:
    - name: storage
      hostPath:
        path: /mnt
        type: Directory

The closest answer I found is this.

But I want to know is that, will the Dockerfile VOLUME command be totally ignored by Kubernetes? Or data will be persisted into two places? One for docker volume (in the host which pod running) and another is Kubernetes's PV?

The reason of asking this is because I deploy some containers from docker hub which contain VOLUME command. Meanwhile I also attach PVC to my pod. I am thinking whether local volume (docker volume, not K8 PV) will be created in the node? If my pod scheduled to another node, then another new volume created?


On top of this, thanks for @Rico to point out that -v command and Kubernetes's mount will take precedence over dockerfile VOLUME command, but what if as scenario below:

  • dockerfile VOLUME onto '/myvol'

  • Kubernetes mount PVC to '/anotherMyVol'

In this case, will myvol mount to my local node harddisk? and cause unaware data persisted locally?

解决方案

It will not be ignored unless you override it on your Kubernetes pod spec. For example, if you follow this example from the Docker documentation:

$ docker run -it container bash
root@7efcf5ef12a2:/# mount | grep myvol
/dev/nvmeXnXpX on /myvol type ext4 (rw,relatime,discard,data=ordered)
root@7efcf5ef12a2:/#

You'll see that it's mounted on the root drive of the host where the container is running on. Docker actually creates a volume on the host filesystem under /var/lib/docker/volumes (/var/lib/docker is your Docker graph directory):

$ pwd
/var/lib/docker/volumes
$ find . | grep greeting
./d0bc20d085243c39c4f386dce2f6cafcd8146128d6b0c8f9dcb27cfb61a7ecab/_data/greeting

You can override this with the -v option in Docker:

$ docker run -it -v /mnt:/myvol container bash
root@1c7211cf43d0:/# cd /myvol/
root@1c7211cf43d0:/myvol# touch hello
root@1c7211cf43d0:/myvol# exit
exit
$ pwd # <= on the host
/mnt
$ ls
hello

So on Kubernetes you can override it in the pod spec:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mycontainer
    image: container
    volumeMounts:
    - name: storage
      mountPath: /myvol
  volumes:
    - name: storage
      hostPath:
        path: /mnt
        type: Directory

这篇关于Docker卷与Kubernetes持久卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆