如何在Kubernetes中模仿“ - 体积” [英] How to mimic '--volumes-from' in Kubernetes

查看:188
本文介绍了如何在Kubernetes中模仿“ - 体积”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种允许在Kubernetes的同一个pod上运行的两个容器之间共享卷的模式。



我的用例是:
我有一个运行在Docker容器内的Ruby on Rails应用程序。
Docker图像包含 / app /< app-name> / public 目录中的静态资源,我需要从运行的nginx容器访问这些资源在同一个pod中。



在'vanilla'码头工具中,我将使用 - 卷从标志来共享这个目录:

  docker run --name app -v / app /< app-dir> / public< app-图像> 
docker run --volumes - from app nginx

阅读此文档后:



注意:initContainer仍然是一个 beta f特征,所以这个yaml的工作版本实际上就像: http://kubernetes.io/docs/user-guide/production-pods/#handling-initialization ,请注意 pod.beta.kubernetes.io/init-containers part。



---原始答案开始---



<实际上,你可以。您需要使用容器生命周期处理程序来控制要与其他容器共享的文件/目录。喜欢:

  --- 
apiVersion:v1
kind:Pod
元数据:
名称:服务器
规范:
restartPolicy:OnFailure
容器:
- image:resouer / sample:v2
名称:war
生命周期:
postStart:
exec:
命令:
- cp
- /sample.war
- / app
VolumeMounts:
- mountPath:/ app
name:hostv1
- name:peer
image:busybox
命令:[tail,-f / dev / null]
volumeMounts:
- 名称:hostv2
mountPath:/app/sample.war
卷:
- 名称:hostv1
hostPath:
路径:/ tmp
- 名称:hostv2
hostPath:
路径:/tmp/sample.war

请查看我的要点了解更多详情:



https://gist.github.com/resouer/378bcdaef1d9601ed6aa



当然可以使用emptyDir。因此,war容器可以将/sample.war共享到对等容器,而不需要使用mess peer / app目录。



如果我们可以容忍/应用被覆盖,那将会更简单:

  --- 
apiVersion:v1
kind:Pod
元数据:
名称:javaweb-2
规格:
restartPolicy:OnFailure
容器:
- image:resouer / sample:v2
名称:war
生命周期:
postStart:
exec:
命令:
- cp
- /sample.war
- / app
volumeMounts:
- mountPath:/ app
名称:app-volume
- image:resouer / mytomcat:7.0
名称:tomcat
命令:[sh , - c,/ root / apache-tomcat-7.0.42-v2 / bin / start.sh]
volumeMounts:
- mountPath:/root/apache-tomcat-7.0.42 -v2 / webapps
名称:app-volume
ports:
- containerPort:8080
hostPort:8001
卷:
- 名称: app-volume
emptyDir:{}


I'm looking for a pattern that allows to share volumes between two containers running on the same pod in Kubernetes.

My use case is: I have a Ruby on Rails application running inside a docker container. The docker image contains static assets in /app/<app-name>/public directory, and I need to access those assets from the nginx container running alongside in the same pod.

In 'vanilla' docker I would have used --volumes-from flag to share this directory:

docker run --name app -v /app/<app-dir>/public <app-image>
docker run --volumes-from app nginx

After reading this doc: https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/volumes.md I tried this (only relevant entries presented):

spec:
  containers:
    - image: <app-image>
      name: <app-name>
      volumeMounts:
        - mountPath: /app/<app-name>/public
          name: assets
    - image: nginx
      name: nginx
      volumeMounts:
        - mountPath: /var/www/html
          name: assets
          readOnly: true
    volumes:
      - name: assets
        hostPath:
          path: /tmp/assets

But:

  • Even though /tmp/assets on the node exists, it's empty
  • /app/<app-name>/public inside the app container is also empty

As a workaround I'm gonna try to populate the shared directory when the application container is up (simply cp /app/<app-name>/public/* to shared directory), but I really dislike this idea.

Question: how to mimic --volumes-from in Kubernetes, or if there is no direct counterpart, how can I share files from one container to other running in the same pod ?

apiVersion: v1beta3

Client Version: version.Info{Major:"0", Minor:"17", GitVersion:"v0.17.0", GitCommit:"82f8bdac06ddfacf493a9ed0fedc85f5ea62ebd5", GitTreeState:"clean"}
Server Version: version.Info{Major:"0", Minor:"17", GitVersion:"v0.17.0", GitCommit:"82f8bdac06ddfacf493a9ed0fedc85f5ea62ebd5", GitTreeState:"clean"}

解决方案

[update-2016-8] In latest Kubernetes release, you can use a very nice feature named init-container to replace the postStart part in my answer below, which will make sure the container order.

NOTE: initContainer is still a beta feature so the work version of this yaml is actually like: http://kubernetes.io/docs/user-guide/production-pods/#handling-initialization, please notice the pod.beta.kubernetes.io/init-containers part.

---original answer begin---

Actually, you can. You need to use container life cycle handler to control what files/dirs you want to share with other containers. Like:

---
apiVersion: v1
kind: Pod
metadata:
    name: server
spec:
    restartPolicy: OnFailure
    containers:
    - image: resouer/sample:v2
      name: war
      lifecycle:
        postStart:
          exec:
            command:
              - "cp"
              - "/sample.war"
              - "/app"
      volumeMounts:
      - mountPath: /app
        name: hostv1 
    - name: peer
      image: busybox
      command: ["tail", "-f", "/dev/null"]
      volumeMounts:
      - name: hostv2
        mountPath: /app/sample.war
    volumes:
    - name: hostv1
      hostPath:
          path: /tmp
    - name: hostv2
      hostPath:
          path: /tmp/sample.war

Please check my gist for more details:

https://gist.github.com/resouer/378bcdaef1d9601ed6aa

And of course you can use emptyDir. Thus, war container can share its /sample.war to peer container without mess peer's /app directory.

If we can tolerate /app been overridden, it will be much simpler:

---
apiVersion: v1
kind: Pod
metadata:
  name: javaweb-2
spec:
  restartPolicy: OnFailure
  containers:
  - image: resouer/sample:v2
    name: war
    lifecycle:
      postStart:
        exec:
          command:
            - "cp"
            - "/sample.war"
            - "/app"
    volumeMounts:
    - mountPath: /app
      name: app-volume
  - image: resouer/mytomcat:7.0
    name: tomcat
    command: ["sh","-c","/root/apache-tomcat-7.0.42-v2/bin/start.sh"]
    volumeMounts:
    - mountPath: /root/apache-tomcat-7.0.42-v2/webapps
      name: app-volume
    ports:
    - containerPort: 8080
      hostPort: 8001 
  volumes:
  - name: app-volume
    emptyDir: {}

这篇关于如何在Kubernetes中模仿“ - 体积”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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