Kubernetes 在现有目录上挂载卷,容器内有文件 [英] Kubernetes mount volume on existing directory with files inside the container

查看:56
本文介绍了Kubernetes 在现有目录上挂载卷,容器内有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 1.11 版的 k8s 和 CephFS 作为存储.

I am using k8s with version 1.11 and CephFS as storage.

我正在尝试挂载在 pod 中的 CephFS 上创建的目录.为了达到同样的目的,我编写了以下卷和卷挂载配置在部署配置中

I am trying to mount the directory created on the CephFS in the pod. To achieve the same I have written the following volume and volume mount config in the deployment configuration

音量

{
  "name": "cephfs-0",
  "cephfs": {
    "monitors": [
      "10.0.1.165:6789",
      "10.0.1.103:6789",
      "10.0.1.222:6789"
    ],
    "user": "cfs",
    "secretRef": {
      "name": "ceph-secret"
    },
    "readOnly": false,
    "path": "/cfs/data/conf"
  }
}

volumeMounts

volumeMounts

{
  "mountPath": "/opt/myapplication/conf",
  "name": "cephfs-0",
  "readOnly": false
} 

安装工作正常.我可以看到 ceph 目录,即/cfs/data/conf 挂载在/opt/myapplication/conf 上,但以下是我的问题.

Mount is working properly. I can see the ceph directory i.e. /cfs/data/conf getting mounted on /opt/myapplication/conf but following is my issue.

我的配置文件已经作为 docker 镜像的一部分出现在/opt/myapplication/conf 位置.当部署尝试挂载 ceph 卷时,位于/opt/myapplication/conf 位置的所有文件都会消失.我知道这是挂载操作的行为,但是有什么方法可以将容器中已经存在的文件保存在我正在挂载的卷上,以便挂载相同卷的其他 pod 可以访问配置文件.即,Pod 中位于/opt/myapplication/conf 位置的文件应该可以在位于/cfs/data/conf 位置的 CephFS 上访问.

I have configuration files already present as a part of docker image at the location /opt/myapplication/conf. When deployment tries to mount the ceph volume then all the files at the location /opt/myapplication/conf gets disappear. I know it's the behavior of the mount operation but is there any way by which I would be able to persist the already existing files in the container on the volume which I am mounting so that other pod which is mounting the same volume can access the configuration files. i.e. the files which are already there inside the pod at the location /opt/myapplication/conf should be accessible on the CephFS at location /cfs/data/conf.

有可能吗?

我浏览了 docker 文档,它提到了

I went through the docker document and it mentions that

使用容器填充卷如果你启动一个容器,创建一个新的卷,如上,并且容器在要挂载的目录中有文件或目录(如上面的/app/),目录的内容被复制到卷中.然后容器安装并使用该卷,其他使用该卷的容器也可以访问预先填充的内容.

Populate a volume using a container If you start a container which creates a new volume, as above, and the container has files or directories in the directory to be mounted (such as /app/ above), the directory’s contents are copied into the volume. The container then mounts and uses the volume, and other containers which use the volume also have access to the pre-populated content.

这符合我的要求,但如何使用 k8s 卷来实现它?

This matches with my requirement but how to achieve it with k8s volumes?

推荐答案

不幸的是,Kubernetes 的卷系统与 Docker 的卷系统非常不同,因此无法直接实现.如果只有一个文件(或少量文件),您可以像这样使用 subPath 投影:

Unfortunately Kubernetes' volume system is very different from Docker's so this is not possible directly. If there is a single file (or a small number) you can use subPath projection like this:

volumeMounts:
- name: cephfs-0
  mountPath: /opt/myapplication/conf/foo.conf
  subPath: foo.conf

对每个文件重复此操作.但是,如果您有很多文件,或者它们可能会有所不同,那么您必须在运行时处理此问题或使用模板工具.通常这意味着在主进程开始之前将其安装在其他地方并设置符号链接.

Repeat that for each file. But if you have a lot of files, or if they can vary, then you have to handle this at runtime or use templating tools. Usually that means mounting it somewhere else and setting up symlinks before your main process starts.

这篇关于Kubernetes 在现有目录上挂载卷,容器内有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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