Kubernetes 如何将同一目录装载到pod中的多个容器

Kubernetes 如何将同一目录装载到pod中的多个容器,kubernetes,Kubernetes,我在一个吊舱里运行多个集装箱。我有一个持久性卷,并将相同的目录装载到容器中 我的要求是: 将/opt/app/logs/app.log装载到容器A中,应用程序在容器A中将数据写入app.log - container-A image: nginx volumeMounts: - mountPath: /opt/app/logs/ => container A is writing data here to **app.log** file name: data - co

我在一个吊舱里运行多个集装箱。我有一个持久性卷,并将相同的目录装载到容器中

我的要求是:

将/opt/app/logs/app.log装载到容器A中,应用程序在容器A中将数据写入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
将/opt/app/logs/app.log装载到容器B以从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
我面临的问题是——当我将同一目录/opt/app/logs/装载到container-B时,我没有看到app.log文件


有人能帮我吗?这是可以实现的,但我不确定我在这里遗漏了什么。

从你的帖子来看,你似乎有两条不同的道路。
Conatainer B已装载到/opt/app/logs/logs。

每个容器都有不同的文件名,并且还修复了容器配置中的装载路径。请以此为例:-

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

根据您的要求,您需要以下内容:

- container-A
  image: nginx
  volumeMounts:
  - mountPath: /opt/app/logs
    name: data
- container-B
  image: busybox
  volumeMounts:
  - mountPath: /opt/app/logs 
    name: data
在container-A上运行的应用程序将在给定路径(/opt/app/logs)上创建或写入文件,例如app.log文件。然后从container-B中,您将在给定路径(/opt/app/logs)中找到app.log文件。你可以在这里使用任何路径

在给定的规范中,您实际上尝试在文件(app.log)中装入目录。我认为这就是问题的根源

更新-1: 这里,我给出了一个工作示例中的完整yaml文件。你可以自己做,看看事情是如何运作的

  • kubectl exec-ti测试pd-c测试容器sh

  • 转到/test-path1

  • 使用touch命令创建一些文件。说“touch a.txt”

  • 从测试容器中退出

  • kubectl exec-ti测试pd-c测试sh

  • 转到/test-path2

  • 您将在此处找到一个.txt文件

  • pvc.yaml

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

    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
    

    修正了挂载路径。我按照你提到的方法做了。但是container-B无法找到app.log文件。当我执行
    ls-l/opt/app/logs时,它返回0个文件。你在卷装载中使用hostPath吗?不,我使用的是持久性卷。我已经给出了一个使用本地类群集的工作示例。希望您能在您的案例中发现问题。谢谢您的示例。我在container-A中注意到,目录具有读写权限,但属于非root用户。而在container-B中,它显示与root用户相同的目录。这是否导致app.log文件在container-B中不可用。可能是container-B装载发生在container A moint之后,因此它会以零覆盖内容。