【问题标题】:How to mount the same directory to multiple containers in a pod如何将同一目录挂载到 pod 中的多个容器
【发布时间】:2021-03-13 12:23:43
【问题描述】:

我在一个 pod 中运行多个容器。我有一个持久化卷并将相同的目录挂载到容器中。

我的要求是:

将 /opt/app/logs/app.log 挂载到容器 A,应用程序将数据写入 app.log

将 /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/ 挂载到容器 B 时,我没有看到 app.log强> 文件。

有人可以帮我解决这个问题吗?这是可以实现的,但我不确定我在这里缺少什么。

【问题讨论】:

  • 容器 B 的挂载可能发生在容器 A 的安装之后,所以它什么都没有覆盖内容。

标签: kubernetes


【解决方案1】:

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

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

您在容器 A 上运行的应用程序将在给定路径(/opt/app/logs)上创建或写入文件,例如 app.log 文件。然后从容器 B 中,您将在给定路径 (/opt/app/logs) 中找到 app.log 文件。您可以在此处使用任何路径。

在您给定的规范中,您实际上尝试将目录挂载到文件(app.log)中。我认为这是造成问题的原因。

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

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

  2. 转到 /test-path1

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

  4. 从测试容器中退出

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

  6. 转到 /test-path2

  7. 你会在这里找到一个 .txt 文件。

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

【讨论】:

  • 我按照你提到的方法做了。但容器 B 无法找到 app.log 文件。当我做ls -l /opt/app/logs 它返回 0 个文件
  • 您是否在卷安装中使用了 hostPath?
  • 不,我正在使用持久化卷。
  • 我已经给出了一个使用本地类型集群的工作示例。希望你能找到你的问题。
  • 谢谢你的例子。我在容器 A 中注意到的是该目录具有读写权限,但归非 root 用户所有。而在容器 B 中,它显示与 root 用户相同的目录。这是否导致 app.log 文件在容器 B 中不可用。
【解决方案2】:

从您的帖子看来,您有两条不同的路径。 容器 B 已挂载到 /opt/app/logs/logs。

【讨论】:

    【解决方案3】:

    为每个容器设置不同的文件名,并从容器配置中修复挂载路径。请以此为例:-

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 2021-11-29
      • 2018-07-10
      • 2021-06-14
      • 1970-01-01
      相关资源
      最近更新 更多