How to mount the same directory to multiple containers in a pod

3/13/2021

I'm running multiple containers in a pod. I have a persistence volume and mounting the same directories to containers.

My requirement is:

mount /opt/app/logs/app.log to container A where application writes data to app.log

mount /opt/app/logs/app.log to container B to read data back from app.log

- container-A
  image: nginx
  volumeMounts:
  - mountPath: /opt/app/logs/ => container A is writing data here to **app.log** file
    name: data
- container-B
  image: busybox
  volumeMounts:
  - mountPath: /opt/app/logs/ => container B read data from **app.log** 
    name: data

The issue I'm facing is - when I mount the same directory /opt/app/logs/ to container-B, I'm not seeing the app.log file.

Can someone help me with this, please? This can be achievable but I'm not sure what I'm missing here.

-- Jwary
kubernetes

3 Answers

3/13/2021

From your post it seems you‘re having two separate paths. Conatainer B ist mounted to /opt/app/logs/logs.

-- Michael Johann
Source: StackOverflow

3/13/2021

Have different file names for each of your containers and also fix the mount path from the container config. Please use this as an example :-

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data
      # this field is optional
      type: Directory
-- Amit
Source: StackOverflow

3/13/2021

According to your requirements, you need something like below:

- container-A
  image: nginx
  volumeMounts:
  - mountPath: /opt/app/logs
    name: data
- container-B
  image: busybox
  volumeMounts:
  - mountPath: /opt/app/logs 
    name: data

Your application running on container-A will create or write files on the given path(/opt/app/logs) say app.log file. Then from container-B you'll find app.log file in the given path (/opt/app/logs). You can use any path here.

In your given spec you actually tried to mount a directory in a file(app.log). I think that's creating the issue.

<b>Update-1:</b> Here I give a full yaml file from a working example. You can do it by yourself to see how things work.

  1. kubectl exec -ti test-pd -c test-container sh

  2. go to /test-path1

  3. create some file using touch command. say "touch a.txt"

  4. exit from test-container

  5. kubectl exec -ti test-pd -c test sh

  6. go to /test-path2

  7. you will find a.txt file here.

pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-pv-claim
spec:
  storageClassName: 
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: nginx
    name: test-container
    volumeMounts:
    - mountPath: /test-path1
      name: test-volume
  - image: pkbhowmick/go-rest-api:2.0.1 #my-rest-api-server                    
    name: test
    volumeMounts:
    - mountPath: /test-path2
      name: test-volume
  volumes:
  - name: test-volume
    persistentVolumeClaim:
        claimName: test-pv-claim
-- Pulak Kanti Bhowmick
Source: StackOverflow