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

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

问题描述

我正在使用版本为1.11和CephFS的k8s作为存储。

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可以访问该配置。文件。例如,应该可以在CephFS上的/ cfs / data / conf位置访问位于/ opt / myapplication / conf位置的pod中已经存在的文件。

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的卷系统非常不同。无法直接进行。如果只有一个文件(或数量很少),则可以使用以下子路径投影:

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天全站免登陆