Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker volume vs Kubernetes persistent volume

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?

like image 762
Sam YC Avatar asked Oct 30 '18 10:10

Sam YC


People also ask

What is the difference between volume and persistent volume in Kubernetes?

Filesystem vs Volume vs Persistent VolumeVolumes let your pod write to a filesystem that exists as long as the pod exists. Volumes also let you share data between containers in the same pod. But, data in that volume will be destroyed when the pod is restarted. To solve this, Kubernetes has persistent volumes.

Is Docker volume persistent?

Docker has two options for containers to store files on the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts. Docker also supports containers storing files in-memory on the host machine. Such files are not persisted.

How were persistent volumes different from volumes used by containers?

A volume exists in the context of a pod, that is, you can't create a volume on its own. A persistent volume on the other hand is a first class object with its own lifecycle, which you can either manage manually or automatically.

What is the difference between persistent volume and persistent volume claim?

PVCs are requests for those resources and also act as claim checks to the resource. So a persistent volume (PV) is the "physical" volume on the host machine that stores your persistent data. A persistent volume claim (PVC) is a request for the platform to create a PV for you, and you attach PVs to your pods via a PVC.


2 Answers

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
like image 166
Rico Avatar answered Oct 19 '22 13:10

Rico


You need to explicitly define a PersistentVolumeClaim and/or PersistentVolume. This is not done for you.

like image 40
yomateo Avatar answered Oct 19 '22 15:10

yomateo