Home > Mobile >  Do Kubernetes pods replicas share read-only file system from the underlying image?
Do Kubernetes pods replicas share read-only file system from the underlying image?

Time:08-04

Let's say I deployed 2 pods to Kubernetes and they both have the same underlying image which includes some read-only file system.

By default, do the pods share this file system? Or each pod copies the file system and hence has a separate copy of it?

I would appreciate any answer and especially would love to see some documentation or resources I can read in order to delve deeper into this issue.

Thanks in advance!

CodePudding user response:

In short, it depends on where the pods are running. If they are running on the same node, then yes, they share the same read-only copy of the image, and if on separate nodes, then they have their own read-only copy of the image. Keep reading if you are interesting in knowing more technical details of this.

Inside Kubernetes Pods

A pod can be viewed as a set of containers bound together. It is a construct provided by Kubernetes to be able to have certain benefits out of the box. We can understand your question better if we zoom into a single node that is part of a Kubernetes cluster.

This node will have a kubelet binary running on it, which will receive certain "instructions" from the api-server on running pods. These "instructions" will be passed onto the cri (Container Runtime Interface) running on your node (let's assume it is the docker-engine). And this cri will be responsible for actually running the needed containers and report back to the kubelet which will report back to the api-server ultimately reporting to the pod-controller that the pod containers are Running.

Now, the question becomes, do multiple pods share the same image? I said the answer is yes for pods on the same node and this is how it works.

  1. Say you run the first pod, the docker daemon running on your k8s node pulls this image from the configured registry and stores it in the local cache of the node. It then starts a container using this image. Note that a container that runs, utilizes the image as simply a read-only file-system, and depending on the storage driver configured in docker, you can have a "writeable layer" on top of this read-only filesystem that is used to allow you to read/write on the file-system of your container. This writeable layer is temporary and vanishes when you delete the container.
  2. When you run the second pod, the daemon finds that the image is already available locally, and simply creates the small writeable layer for your container, on top of an existing image from the cache and provides this as a "writeable file system" to your container. This speeds things up.

Now, in case of docker, these read-only layers of the image (as one 'file-system') are shared across all containers running on the host. This makes sense since there is no need to copy a read-only file system and sharing it with multiple containers is safe. And each container can maintain its uniqueness by storing its data in the thin writeable layer that it has.

References

For further reading, you can use:

  1. Read about storage drivers in docker. It explains how multiple containers share the r/o layer of the image.
  2. Read details about different storage driver types to see how this "thin writeable layer" is implemented in practice by docker.
  3. Read about container runtimes in Kubernetes to understand that docker isn't the only supported runtime. There are others but more or less, the same will hold true for them as well, as it makes sense to cache images locally and re-use the read-only image file system for multiple containers.
  4. Read more about the kubelet component of Kubernetes to understand how it can support multiple run-times and how it helps the pod-controller setup and manage containers.
  5. And of course, finally you can find more details about pods here. This will make a lot more sense after you've read the material above.

Hope this helps!

  • Related