k8s卷挂载覆盖

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

k8s volume mount overwrite

问题

Docker镜像的工作目录已设置为"/opt",其中包含jar文件和配置文件等。我尝试在K8s中挂载它到PVC,但发现PVC会覆盖/opt目录。是否有一种方法可以将非空的Docker镜像目录挂载?谢谢。

英文:

The work directory of my docker image is set to "/opt",and it will contain the jar file and config files,etc. I am trying to mount it pvc in k8s, but find the pvc will overwrite the /opt directory.Is there any way to mount the directory in docker image which is not empty , Thanks.

答案1

得分: 1

当一个永久卷声明(PVC)被挂载到一个目录时,它会覆盖您指定的目录,而不是添加新文件。对于您的情况,当PVC被挂载时,目录“/opt”将被替换。为了防止这种情况发生,您需要在/opt目录内创建一个新目录,或者您可以将PVC挂载到与当前目录不同的目录中。

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: standard
  volumeMode: Filesystem
  volumeName: my-pvc
  mountPath: "/opt/my-pvc"

这个YAML将在/opt目录内创建一个名为'my-pvc'的新目录,并将PVC挂载到它。已经存在于/opt目录中的任何文件不会被覆盖。

或者:

mountOptions:
    - path: /mnt

这将把PVC挂载到/mnt目录,而不是/opt,因此/opt中的数据不会被覆盖。

这个Iman Tumorang的博客将帮助您进行参考。

英文:

When a PVC is mounted to a directory, instead of adding a new file it will overwrite the directory you specify. For your case when the PVC is mounted the directory “/opt” will be replaced. To prevent this you need to create a new directory within the /opt directory or you can mount PVC to a different directory other than the current directory.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: standard
  volumeMode: Filesystem
  volumeName: my-pvc
  mountPath: "/opt/my-pvc"

This YAML will create a new directory named 'my-pvc' within the '/opt' directory and mount the PVC to it. Any files that are already in the '/opt' directory will not be overwritten.

Or else:

mountOptions:
    - path: /mnt

This will mount the PVC to the /mnt directory instead of /opt, so the data in /opt will not be overwritten.

This blog by Iman Tumorang will help you for reference.

答案2

得分: 0

如果Docker镜像的/opt目录中存在任何文件,它们将在将PVC挂载到该目录时被覆盖。为了避免这种情况,您可能希望在容器启动期间将/opt目录中的文件复制到/opt内的子目录中,然后将PVC挂载到父目录。

英文:

there are any files in the /opt directory in the Docker image, they will be overridden by the contents of the PVC when the PVC is mounted to the directory. To avoid this, you may want to copy the files from the /opt directory to a subdirectory within /opt during container startup, and then mount the PVC to the parent directory.

huangapple
  • 本文由 发表于 2023年2月14日 09:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442593.html
匿名

发表评论

匿名网友

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

确定