gpt4 book ai didi

Kubernetes在部署内的容器之间共享卷

转载 作者:行者123 更新时间:2023-12-01 10:27:20 28 4
gpt4 key购买 nike

在发布此问题之前,我遵循了这个答案How to mimic '--volumes-from' in Kubernetes,但它对我不起作用。

我有2个容器:

  • 节点:其图像包含与应用程序相关的所有文件(在/var/www内部)
  • nginx :它需要访问节点图像中的文件(尤其是我拥有所有 Assets 的/clientBuild文件夹)

  • 节点镜像中的内容是什么:
    $ docker run node ls -l
    > clientBuild/
    > package.json
    > ...
    nginx.prod.conf的一部分:
    location ~* \.(jpeg|jpg|gif|png|ico|css|js|gz|map|json)$ {
    include /etc/nginx/mime.types;
    root /usr/local/nginx/html/clientBuild/;
    }

    以及部署设置:
    kind: Deployment
    apiVersion: extensions/v1beta1
    metadata:
    name: pwa-app-production
    labels:
    app: MyApp
    spec:
    replicas: 1
    template:
    metadata:
    name: app
    labels:
    app: MyApp
    env: production
    spec:
    containers:
    - name: nginx
    image: nginx
    command: [nginx, -c, /nginx.prod.conf, -g, 'daemon off;']
    resources:
    limits:
    memory: "500Mi"
    cpu: "100m"
    imagePullPolicy: Always
    volumeMounts:
    - mountPath: /usr/local/nginx/html
    name: pwa-disk
    readOnly: true
    ports:
    - name: nginx
    containerPort: 80
    initContainers:
    - name: node
    image: node
    command: [npm, start]
    resources:
    limits:
    memory: "500Mi"
    cpu: "100m"
    imagePullPolicy: Always
    volumeMounts:
    - mountPath: /var/www
    name: pwa-disk
    ports:
    - name: app
    containerPort: 3000
    - name: api
    containerPort: 3001
    volumes:
    - name: pwa-disk
    emptyDir: {}

    我首先尝试将两个图像都放在相同的 containers key 中,但得到了: /var/www/package.json not found上的 npm start
    然后我将其移到 initContainers内,但现在我只注意到它失败了,但没有告诉我原因。查看日志也不显示任何详细信息。

    请注意,当我删除音量部分时, npm start起作用了。

    enter image description here

    最佳答案

    我假设您的 Assets 已经打包在/var/www的镜像中。如果在该路径上安装emptyDir卷,那么那里的所有内容都会被emptyDir卷的内容覆盖-最初没有任何内容。这意味着您的所有 Assets 都将通过该挂载被删除-这就是您的节点服务器最有可能发生故障的原因。

    您要做的是将emptyDir卷挂载到其他路径上,例如/data。然后,您使用cp -r /var/www/* /data覆盖节点容器cmd,以将 Assets 复制到pwa-disk卷中。现在,您可以将该卷安装到您的nginx容器中。

    我认为initContainers的工作方式存在误解。它们注定要终止。它们会在启动之前运行,然后再启动其他容器-在您的initContainers成功终止之前,不会启动容器中的其他容器。因此,很可能您不想以initContainer的形式启动节点服务器。我猜您的节点服务器不应该终止,在这种情况下,您的nginx容器将永远不会启动。相反,您可能想在containers部分中将您的节点服务器与nginx一起声明。此外,您还可以将带有覆盖cmd(cp -r /var/www/* /data)的节点容器添加到initContainers部分,以将 Assets 复制到卷。整个事情可能看起来像这样:

    kind: Deployment
    apiVersion: extensions/v1beta1
    metadata:
    name: pwa-app-production
    labels:
    app: MyApp
    spec:
    replicas: 1
    template:
    metadata:
    name: app
    labels:
    app: MyApp
    env: production
    spec:
    containers:
    - name: nginx
    image: nginx
    command: [nginx, -c, /nginx.prod.conf, -g, 'daemon off;']
    resources:
    limits:
    memory: "500Mi"
    cpu: "100m"
    imagePullPolicy: Always
    volumeMounts:
    - mountPath: /usr/local/nginx/html
    name: pwa-disk
    readOnly: true
    ports:
    - name: nginx
    containerPort: 80
    - name: node
    image: node
    command: [npm, start]
    resources:
    limits:
    memory: "500Mi"
    cpu: "100m"
    imagePullPolicy: Always
    ports:
    - name: app
    containerPort: 3000
    - name: api
    containerPort: 3001

    initContainers:
    - name: assets
    image: node
    command: [bash, -c]
    args: ["cp -r /var/www/* /data"]
    imagePullPolicy: Always
    volumeMounts:
    - mountPath: /data
    name: pwa-disk
    volumes:
    - name: pwa-disk
    emptyDir: {}

    关于Kubernetes在部署内的容器之间共享卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46389817/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com