如何将初始文件同步到持久卷(例如,NFS)?

huangapple go评论94阅读模式
英文:

how to sync initial files to persistent volume (e.g., NFS)?

问题

一个 web 应用程序(Docker 镜像)在运行时访问一个名为 "/usr/local/myapp" 的目录,该目录包含以下初始文件:

   /usr/local/myapp/
                    conf/
                        foo.xml
                    web-files/
                        index.html
                        images/

在独立容器中,该 web 应用程序可以访问目录下的文件,包括读取配置文件、创建新文件和删除文件。

对于 Kubernetes 集群,需要在集群中共享目录 "/usr/local/myapp"。如果使用 NFS 挂载到所有的 Pod 上,共享目录下的所有现有文件(例如 conf/foo.xml)将无法访问(被卷隐藏,不会同步到卷中)。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my_web
spec:
  template:
    spec:
      containers:
        - image: my-web-app:1.0
          volumeMounts:
            - name: my-volume
              mountPath: /usr/local/myapp

      volumes:
        - name: my-volume
          persistentVolumeClaim:
            claimName: my-pv-claim

对于 Kubernetes,从 Docker 镜像同步初始现有内容到共享卷(例如 NFS)的正确方法是什么?对于 Docker 卷,如果卷为空,初始现有内容将会同步到卷中。

英文:

A webapp (docker image) accesses a directory "/usr/local/myapp" at runtime that has initial files:

   /usr/local/myapp/
                    conf/
                        foo.xml
                    web-files/
                        index.html
                        images/

In standalone container, the webapp access files under the directory, including reading configuration files, creating new files, deleting files.

For kubernetes cluster, the directory "/usr/local/myapp" needs to be shared in cluster. If using NFS mounting to all pods, all existing files(e.g. conf/foo.xml) under the shared directory will not be accessible (hidden by volume, not sync-ed to volume).

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my_web
spec:
  template:
    spec:
      containers:
        - image: my-web-app:1.0
          volumeMounts:
            - name: my-volume
              mountPath: /usr/local/myapp

      volumes:
        - name: my-volume
          persistentVolumeClaim:
            claimName: my-pv-claim

For k8s, what is the right way to sync initial existing content from docker image to shared volume(e.g., NFS)? For docker volume, the initial existing content will be sync-ed to the volume if the volume is empty.

答案1

得分: 0

通常情况下,当涉及将数据移动到已挂载的文件夹或卷中时,您会运行一个初始化容器来处理这个任务。不幸的是,目前没有更好的方法来做这件事。当挂载新卷时,如果路径已存在,其中的每个文件和文件夹都会消失得无影无踪。对于持久卷声明(PVC)也是如此。然而,如果PVC本身已经存在并且包含文件,则在挂载卷时不会删除这些文件。

您也可以在容器初始化时运行启动命令,但为了保持简单并且只有一个初始化命令,将其运行在初始化容器中似乎是最好的选择。

注意:此示例仅在这些文件不会更新并且因此不需要持久性时才有效,因此我建议删除PVC以避免浪费资源。而是使用emptyDir类型的卷。

另外,我认为Kubernetes中的NFS不适用于PVC。您可以在这里了解更多关于K8s中NFS的信息。

英文:

Usually, when it comes to moving data into mounted folders or volumes, you run an init container that takes care of it. Sadly, there's not really a better way to do this at the moment. When mounting new volumes, if the path exists, every file and folder in it will disappear into thin air. This is true for PVCs as well. However, if the PVC itself already existed and had files, said files won't be deleted upon mounting the volume.

You could also run the startup command on container init, but in order to keep it simple and with only one init command, running it in an init container seems best.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my_web
spec:
  template:
    spec:
      initContainers:
        - image: my-web-app:1.0
          volumeMounts:
            - name: my-volume
              mountPath: /usr/local/myapp
          command:
            - /bin/sh
            - -c
            - cp -R <source-dir> /usr/local/myapp
      containers:
        - image: my-web-app:1.0
          volumeMounts:
            - name: my-volume
              mountPath: /usr/local/myapp
      volumes:
        - name: my-volume
          persistentVolumeClaim:
            claimName: my-pv-claim

Note: This example only works if these files will not be updated and thus you don't need persistence, thus I would remove the PVC so as to not waste resources. Instead, use a volume of type emptyDir.

Also, I don't think NFS in Kubernetes works with PVCs. You can read more about NFS with K8s here.

huangapple
  • 本文由 发表于 2023年5月28日 07:53:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定