【问题标题】:Is it possible to to define a volume usable both in k8s and docker?是否可以定义在 k8s 和 docker 中都可用的卷?
【发布时间】:2019-06-02 22:23:43
【问题描述】:

如果我想在容器 A 中运行容器 B(重用 Docker 守护程序),我可以绑定挂载 /var/run/docker.sock/usr/bin/docker 并且可以愉快地在 A 中调用 docker run。 现在我想在AB 之间共享一个k8s 卷。为此,我想在A 中创建一个emptyDir 卷,并使用docker run -v 将其传递给B。但这不起作用,因为 emptyDir 卷似乎不是 Docker 卷(运行 docker volume ls 时不会出现)。

下面的 sn-p 打印出cache-volume 没有出现的卷列表:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: alpine
    name: test-container
    command: [ash]
    args: ["-c", "docker volume ls"]
    volumeMounts:
    - name: dockersock
      mountPath: "/var/run/docker.sock"
    - name: dockerlib
      mountPath: "/usr/bin/docker"
    - name: cache-volume
      mountPath: /cache
  volumes:
  - name: dockersock
    hostPath:
      path: /var/run/docker.sock
  - name: dockerlib
    hostPath:
      path: /usr/bin/docker
  - name: cache-volume
    emptyDir: {}

所以问题是:有什么方法可以定义在 k8s 和 docker 中都可用的卷?

【问题讨论】:

    标签: docker kubernetes docker-volume


    【解决方案1】:

    作为记录,我找到了一种解决方法,但如果您有更好的建议,请随时分享。

    解决上述问题的方法是挂载一个hostPath目录D,并将这个目录绑定挂载到容器B中。

    apiVersion: v1 kind: Pod metadata: name: test-pd3 spec: containers: - image: alpine name: test-container command: [ash] args: ["-c", "echo blabla > /home/test.txt ; docker run -v /home:/home --entrypoint ls alpine /home"] volumeMounts: - name: dockersock mountPath: "/var/run/docker.sock" - name: dockerlib mountPath: "/usr/bin/docker" - name: home mountPath: "/home" volumes: - name: dockersock hostPath: path: /var/run/docker.sock - name: dockerlib hostPath: path: /usr/bin/docker - name: home hostPath: path: /home

    上面sn-p的结果是B现在有了/home/test.txt,这就是我们想要的。

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 2018-04-04
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多