在同一个pod中的两个容器之间共享文件夹内容

时间:2017-11-13 17:33:44

标签: ruby-on-rails nginx kubernetes google-kubernetes-engine

我有一个带有两个容器的Pod,Nginx和Rails。我想将rails中的公共文件夹共享到nginx容器,但是public已包含文件,我不希望该文件夹为空。

共享卷有没有办法?

我试过了:

- name: rails-assets
    hostPath:
      path: /app/public

但我得到了这个错误:

Error: failed to start container "nginx": Error response from daemon: {"message":"error while creating mount source path '/app/public': mkdir /app: read-only file system"}
Error syncing pod
Back-off restarting failed container

谢谢,

2 个答案:

答案 0 :(得分:1)

我修复了在rails应用上创建共享权限shared-assets/的问题。在rails dockerfile上,我创建了一个带有bash脚本的ENTRYPOINT,用于复制public/文件夹中的shared-assets/文件。有了这个,我现在可以在Nginx容器上看到文件。

---
kind: Deployment
apiVersion: apps/v1beta1
metadata:
  name: staging-deployment
spec:
  replicas: 1
  revisionHistoryLimit: 2
  template:
    metadata:
      labels:
        app: staging
    spec:
      containers:
      - name: staging
        image: some/container:v5
        volumeMounts:
        - mountPath: /var/run/
          name: rails-socket
        - mountPath: /app/shared-assets
          name: rails-assets
      - name: nginx
        image: some/nginx:latest
        volumeMounts:
        - mountPath: /var/run/
          name: rails-socket
        - mountPath: /app
          name: rails-assets
      imagePullSecrets:
      - name: app-backend-secret
      volumes:
      - name: rails-socket
        emptyDir: {}
      - name: rails-assets
        emptyDir: {}

脚本ENTRYPOINT:

cp -r /app/public/ /app/shared-assets/

答案 1 :(得分:0)

一种可能的选择是使用ConfigMaps。您可以将文件放入ConfigMaps and mount them into a Pod

$ kubectl create configmap my-config --from-file=hello/world/

yaml看起来像这样:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  restartPolicy: Never
  containers:
    - name: my-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh", "-c", "ls /hello/world" ]
      volumeMounts:
      - name: hello-world
        mountPath: '/hello/world'
  volumes:
    - name: hello-world
      configMap:
        name: my-config

但是,Kubernetes有时会重新加载ConfigMaps,因此/hello/world目录中的更改可能会丢失...

在底部,卷基本上只是一些mount s,具有所有限制。特别是如果您将任何内容安装到/hello/world中,那么当该目录用作挂载点时,该目录中的所有文件都将不可见。