Does every container in same pod has independent file system?

Viewed 1457

Try to share a file between two containers within a pod, I must use volume and create file in mouth path for this volume.

apiVersion: v1
kind: Pod
metadata:
  name: pod-5
spec:
  containers:
  - name: busybox
    image: busybox
    command: ["sh", "-c", "if [ -f /workdir/foo ]; then sleep 3600; else exit; fi"]
    volumeMounts:
    - name: workdir
      mountPath: "/workdir"
  initContainers:
  - name: install
    image: busybox
    command: ["sh", "-c", "touch /workdir/foo; hostname > /workdir/foo"]
    volumeMounts:
    - name: workdir
      mountPath: "/workdir"
  volumes:
  - name: workdir
    emptyDir: {}

If I don't use volume and create a file in init container and try to read it from the other container, it will not work.

apiVersion: v1
kind: Pod
metadata:
  name: pod-5
spec:
  containers:
  - name: busybox
    image: busybox
    command: ["sh", "-c", "if [ -f /workdir/foo ]; then sleep 3600; else exit; fi"]
  initContainers:
  - name: install
    image: busybox
    command: ["sh", "-c", "touch /workdir/foo; hostname > /workdir/foo"]

Why? I thought all containers within a pod should share both network and file system.

2 Answers

Containers within same pod share network namespace and IPC namespace but they have separate mount namespace and filesystem.Hence we use volumes for sharing mounts. To know more about namespaces check the linux namespace doc.

Let’s start by explaining what a Pod is in the first place. A Pod is is the smallest unit that can be deployed and managed by Kubernetes. In other words, if you need to run a single container in Kubernetes, then you need to create a Pod for that container. At the same time, a Pod can contain more than one container, usually because these containers are relatively tightly coupled. How tightly coupled? Well, think of it this way: the containers in a pod represent processes that would have run on the same server in a pre-container world.

Now think of pod as in your local machine where you are trying to run you containers.
Lets say for example you have your two container initcontainer (container 1) and the main container (container 2) running in the same network.They both are running on your local environment.Now if you create a file in one container and expect the file to be present in the other container thats simply not true.The file is present in a different container in its own file system and there is no way other container can have access to it.But to share the file system between two container you can create a volume mount from your local to container 1 and then mount the same path to container 2. Thus both the container can share the file system.

The same thing applies for Pod in kubernetes environment as well.
In Kubernetes, you can use a shared Kubernetes Volume as a simple and efficient way to share data between containers in a Pod. For most cases, it is sufficient to use a directory on the host that is shared with all containers within a Pod

Related