两个容器之间共享的卷“忙碌或锁定”

时间:2020-07-09 21:46:52

标签: kubernetes

我有一个运行两个容器的部署。其中一个容器尝试构建(在部署期间)另一个容器nginx尝试提供服务的javascript包。

我想在构建JavaScript捆绑包后使用共享卷来放置它。

到目前为止,我有以下部署文件(已删除无关的部分):

apiVersion: apps/v1
kind: Deployment
metadata:
  ...
spec:
  ...
  template:
    ...
    spec:
      hostNetwork: true
      containers:
      - name: personal-site
        image: wheresmycookie/personal-site:3.1
        volumeMounts:
        - name: build-volume
          mountPath: /var/app/dist
      - name: nginx-server
        image: nginx:1.19.0
        volumeMounts:
        - name: build-volume
          mountPath: /var/app/dist
      volumes:
      - name: build-volume
        emptyDir: {}

尽我所能,我遵循了以下指南:

需要指出的另一件事是,我正在尝试使用minikube在本地atm上运行。

编辑:我用来构建该映像的Dockerfile是:

FROM node:alpine
WORKDIR /var/app

COPY . .
RUN npm install
RUN npm install -g @vue/cli@latest

CMD ["npm", "run", "build"]

我意识到我在实际运行映像时不需要构建该文件,但是我的下一个目标是将pod实例信息作为环境变量插入,因此不幸的是,使用javascript我只能在该信息对我可用时进行构建。

问题

personal-site容器中的日志显示:

-  Building for production...
 ERROR  Error: EBUSY: resource busy or locked, rmdir '/var/app/dist'
 Error: EBUSY: resource busy or locked, rmdir '/var/app/dist'

我不确定为什么要尝试删除/dist,但是我还是觉得这无关紧要。我可能会错吗?

我认为这可能与容器/卷的生命周期有关,但是文档建议“将Pod分配给Node时,首先创建emptyDir卷,并且只要该Pod在该节点”。

问题

在容器已经运行后,可能无法使用卷的原因有哪些?鉴于您可能比我在Kubernetes上拥有更多的经验,接下来您会看什么?

1 个答案:

答案 0 :(得分:2)

最好的方法是如下自定义图像的入口点:

  • 完成构建/var/app/dist文件夹后,将此文件夹复制(或移动)到另一个空路径(例如:/opt/dist

    cp -r /var/app/dist/* /opt/dist
    

注意:此步骤必须在ENTRYPOINT的脚本中完成,而不是在RUN层中。

  • 现在改用/opt/dist ..:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      ...
    spec:
      ...
      template:
        ...
        spec:
          hostNetwork: true
          containers:
          - name: personal-site
            image: wheresmycookie/personal-site:3.1
            volumeMounts:
            - name: build-volume
              mountPath: /opt/dist # <--- make it consistent with image's entrypoint algorithm
          - name: nginx-server
            image: nginx:1.19.0
            volumeMounts:
            - name: build-volume
              mountPath: /var/app/dist
    
          volumes:
          - name: build-volume
            emptyDir: {}
    

祝你好运!

如果不清楚如何自定义入口点,请与我们分享图像的入口点,我们将予以实施。