简体   繁体   中英

How to merge data from docker image with kubernetes volume (NFS)

I have a docker image that contains data in directors /opt/myfiles, Lets say the following: /opt/myfiles/file1.txt /opt/myfiles/file2.dat

I want to deploy that image to kubernetes and mount an NFS volume to that directory so that the changes to these files are persisted when I delete the pod.

When I do it in docker swarm I simply mount an empty NFS volume to /opt/myfiles/ and then my docker swarm service is started, the volume is populated with the files from the image and I can then work with my service and when I delete the service, I still have the files on my NFS server, so on next start of the service, I have my previous state back.

In kubernetes, when I mount an empty NFS volume to /opt/myfiles/, the pod is started and /opt/myfiles/ is overwritten with an empty directory, so my pod does not see the files from the image anymore.

My volme mount and volume definition:

[...]
volumeMounts:
  - name: myvol
    mountPath: /opt/myfiles
[...]
volumes:
  - name: myvol
    nfs:  
      server: nfs-server.mydomain.org
      path: /srv/shares/myfiles

I read some threads about similar problems (for example K8s doesn't mount files on Persistent Volume ) and tried some stuff using subPath and subPathExpr as in the documentation ( https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath ) but none of my changes does, what docker swarm does by default.

The behaviour of kubernetes seems strange to my as I have worked with docker swarm for quite a while now and I am familiar with the way docker swarm handles that. But I am sure that there is a reason why kubernetes handles that in another way and that there are some possibilities to get, what I need.

So please, can someone have a look at my problem and help me find a way to get the following behaviour?

  • I have files in my image in some directory
  • I want to mount an NFS volume to that directory, because I need to have the data persisted, when for example my pod crashes and moves to another host or when my pod is temporarily shut down for whatever reason.
  • When my pod starts, I want the volume to be populated with the files from the image

And of course I would be really happy if someone could explain me, why kubernetes and docker swarm behave so different.

Thanks a lot in advance

This can be usually achieved in Kubernetes with init-containers. Let's have an image with files stored in /mypath folder. If you are able to reconfigure the container to use a different path (like /persistent ) you can use init container to copy files from /mypath to /persistent on pod's startup.

containers:
  - name: myapp-container
    image: myimage
    env:
    - name: path
      value: /persistent 
    volumeMounts: 
    - name: myvolume
      path: /persistent
  initContainers:
  - name: copy-files
    image: myimage
    volumeMounts: 
    - name: myvolume
      path: /persistent
    command: ['sh', '-c', 'cp /mypath/*.* /persistent']

In this case you have a main container myapp-container using files from /persistent folder from the NFS volume and each time when container starts the files from /mypath will be copied into that folder by init container copy-files .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM