Can I share a single file between containers in a pod?

8/24/2017

My pod has two containers - a primary container, and a sidecar container that monitors the /var/run/utmp file in the primary container and takes action when it changes. I'm trying to figure out how to make this file visible in the sidecar container.

This page describes how to use an emptyDir volume to share directories between containers in a pod. However, this only seems to work for directories, not single files. I also can't use this strategy to share the entire /var/run/ directory in the primary container, since mounting a volume there erases the contents of the directory, which the container needs to run.

I tried to work around this by creating a symlink to utmp in another directory and mounting that directory, but it doesn't look like symlinks in volumes are resolved in the way they would need to be for this to work.

Is there any way I can make one file in a container visible to other containers in the same pod? The manifest I'm experimenting with looks like this:

apiVersion: v1
kind: Pod
metadata:
  name: utmp-demo

spec:
  restartPolicy: Never

  containers:
  - name: main
    image: debian
    command: ["/bin/bash"]
    args: ["-c", "sleep infinity"]
    volumeMounts:
    - name: main-run
      mountPath: /var/run  # or /var/run/utmp, which crashes

  - name: helper
    image: debian
    command: ["/bin/bash"]
    args: ["-c", "sleep infinity"]
    volumeMounts:
    - name: main-run
      mountPath: /tmp/main-run

  volumes:
  - name: main-run
    emptyDir: {}
-- Erik Kuefler
kubernetes

1 Answer

8/25/2017

If you can move the file to be shared in an empty subfolder this could be a simple solution.

For example, move your file to /var/run/utmp/utmp and share /var/run/utmp folder with an emptydir.

-- whites11
Source: StackOverflow