Kubernetes在部署内的容器之间共享卷 [英] Kubernetes share volume between containers inside a Deployment
问题描述
在发布此问题之前,我遵循了此答案如何模仿'--volumes -from'在Kubernetes中,但对我来说不起作用.
Before posting this question I followed this answer How to mimic '--volumes-from' in Kubernetes but it didn't work for me.
我有2个容器:
- 节点:其图像包含与该应用相关的所有文件(在
/var/www
内) - nginx :它需要访问节点图像内的文件(特别是我拥有所有资产的
/clientBuild
文件夹)
- node: its image contains all the files related to the app ( inside
/var/www
) - nginx: it needs to access the files inside the node image (especially the
/clientBuild
folder where I have all the assets)
节点图像中的内容:
$ 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
键中,但我在npm start
上得到了/var/www/package.json not found
I first attempt to put both images in the same containers
key, but i got: /var/www/package.json not found
on npm start
然后我将其移到了initContainers
内,但是现在我只注意到它失败了,但它没有告诉我原因.查看日志也没有显示任何详细信息.
Then I moved it inside the initContainers
but now I only have a notice that it failed, but it does not tell me why. View logs does not show any details too.
请注意,当我删除音量部分时,npm start
起作用.
Notice that when I remove volume part, the npm start
works.
推荐答案
我假定您的资产已经打包在/var/www
的映像中.如果在该路径上安装emptyDir
卷,则其中的所有内容都会被emptyDir
卷的内容覆盖-最初没有任何内容.这意味着您的所有资产都将通过该挂载被删除-这就是您的节点服务器很可能出现故障的原因.
I assume your assets are already packaged inside the image at /var/www
. If you mount an emptyDir
volume at that path, then everything there gets overriden with the content of the emptyDir
volume - which initially is nothing. That means all your assets are deleted through that mount - which is why your node server is most likely failing.
您要执行的操作是将emptyDir
卷挂载到其他路径上,例如/data
.然后,用cp -r /var/www/* /data
覆盖节点容器cmd,以将资产复制到您的pwa-disk
卷中.现在,您可以将该卷安装到您的nginx容器中.
What you want to do is mount the emptyDir
volume at some other path, say /data
. Then you override your node containers cmd with cp -r /var/www/* /data
to copy the assets into yourpwa-disk
volume. Now, you can mount this volume into your nginx container.
我认为对initContainers
的工作方式存在误解.它们注定要终止.它们运行之前,然后启动任何其他容器-在您的initContainers
成功终止之前,不会启动容器中的其他容器.因此,很可能您不想将您的节点服务器启动为initContainer
.我猜您的节点服务器不应该终止,在这种情况下,您的nginx容器将永远不会启动.相反,您可能想在containers
部分中将节点服务器与nginx一起声明.此外,还将带有覆盖cmd(cp -r /var/www/* /data
)的节点容器添加到initContainers
部分,以将资产复制到卷.整个事情可能看起来像这样:
I think there is a misunderstanding on how initContainers
work. They are meant to terminate. They run BEFORE any other container is started - no other container inside your pod is started until your initContainers
have successfully terminated. So most likely you do not want to start your node server as an initContainer
. I guess your node server is not supposed to terminate, in which case your nginx container will never start up. Instead, you might want to declare your node server together with your nginx inside the containers
section. Additionally, you also add your node container with an overridden cmd (cp -r /var/www/* /data
) to the initContainers
section, to copy the assets to a volume. The whole thing might look sth like that:
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在部署内的容器之间共享卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!