node_modules的kubernetes卷 [英] kubernetes volume for node_modules

查看:111
本文介绍了node_modules的kubernetes卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在docker-compose中,我习惯于以这种方式创建卷:

In docker-compose I was used to create volumes in this way:

volumes:
  - ./server:/home/app
  - /home/app/node_modules

以解决node_modules问题.

如何在kubernetes中解决问题?

How could I approach the problem in kubernetes?

我已经创建了以下配置

  spec:
    containers:
    - env: []
      image: "image"
      volumeMounts:
      - mountPath: /home/app
        name: vol-app
      - mountPath: /home/app/node_modules
        name: vol-node-modules
      name: demo
    volumes:
    - name: vol-app
      hostPath:
        path: /Users/me/server
    - name: vol-node-modules
      emptyDir: {}

但是它不起作用. node_modules为空

but it doesn't work. the node_modules is empty

推荐答案

我刚刚从Docker Swarm过渡到Kubernetes,并遇到了同样的问题.我确定的解决方案使用了初始化容器( https://kubernetes. io/docs/concepts/workloads/pods/init-containers/).

I just transitioned from Docker Swarm to Kubernetes and had the same question. The solution I settled on makes use of init containers (https://kubernetes.io/docs/concepts/workloads/pods/init-containers/).

通常的想法是,初始化容器使您可以将映像中的node_modules目录复制到一个空卷中,然后将该卷安装在应用程序的pod中.

The general idea is that init containers allow you to copy the node_modules directory from your image into an empty volume, then mount that volume in your application's pod.

kind: Deployment
apiVersion: apps/v1
metadata:
  ...
spec:
  ...
  template:
    ...
    spec:
      initContainers:
        - name: frontend-clone
          image: YOUR_REGISTRY/YOUR_IMAGE
          command:
            - cp
            - -a
            - /app/node_modules/.
            - /node_modules/
          volumeMounts:
            - name: node-modules-volume
              mountPath: /node_modules
      containers:
        - name: frontend
          image: YOUR_REGISTRY/YOUR_IMAGE
          volumeMounts:
            - name: source-volume
              mountPath: /app
            - name: node-modules-volume
              mountPath: /app/node_modules
      volumes:
        - name: source-volume
          hostPath:
            path: /YOUR_HOST_CODE_DIRECTORY/frontend
        - name: node-modules-volume
          emptyDir: {}

请注意,用于复制node_modules目录的命令是cp -a /SOURCE/. /DEST/,而不是更常见的cp -r /SOURCE/* /DEST/.后一个命令中的星号将被解释为文字*而不是逻辑*,并且它不会按预期匹配每个文件/目录.

Note that the command to copy the node_modules directory is cp -a /SOURCE/. /DEST/ and not the more common cp -r /SOURCE/* /DEST/. The asterisk in the latter command would be interpreted as a literal * instead of a logical * and it wouldn't match every file/directory as intended.

您可以使用init容器执行任何操作,甚至可以在初始化时通过脚本创建node_modules目录(尽管这会增加很大的延迟).

You can do whatever you want with an init container, even create the node_modules directory from a script at initialization (though it'd add a significant delay).

这篇关于node_modules的kubernetes卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆