Kubernetes config map symlinks (..data/) : is there a way to avoid them?

后端 未结 2 459
无人共我
无人共我 2021-02-05 09:03

I have noticed that when I create and mount a config map that contains some text files, the container will see those files as symlinks to ../data/myfile.txt .

相关标签:
2条回答
  • 2021-02-05 09:32

    I am afraid I don't know if you can tell Kubernetes not to generate those symlinks although I think that it is a native behaviour.

    If having those files and links is an issue, a workaround that I can think of is to mount the configmap on one folder and copy the files over to another folder when you initialise the container:

      initContainers:
        - name: copy-config
          image: busybox
          command: ['sh', '-c', 'cp /configmap/* /configs']
          volumeMounts:
            - name: configmap
              mountPath: /configmap
            - name: config
              mountPath: /configs
    

    But you would have to declare two volumes, one for the configMap (configmap) and one for the final directory (config):

      volumes:
        - name: config
          emptyDir: {}
        - name: configmap
          configMap:
            name: myconfigmap
    

    Change the type of volume for the config volume as you please obviously.

    0 讨论(0)
  • 2021-02-05 09:47

    I think this solution is satisfactory : specifying exact file path in mountPath, will get rid of the symlinks to ..data and ..2018_06_04_19_31_41.860238952

    So if I apply such a manifest :

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-lamp-site
    spec:
        containers:
        - name: php
          image: php:7.0-apache
          volumeMounts:
          - mountPath: /var/www/html/users.xml
            name: site-data
            subPath: users.xml
        volumes:
        - name: site-data
          configMap:
            name: users
    
    ---
    
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: users
    data:
      users.xml: |
          <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
          <users>
          </users>
    

    Apparently, I'm making use of subpath explicitly, and they're not part of the "auto update magic" from ConfigMaps, I won't see any more symlinks :

    $ kubectl exec  my-lamp-site -c php -- ls -al /var/www/html
    total 12
    drwxr-xr-x 1 www-data www-data 4096 Jun  4 19:18 .
    drwxr-xr-x 1 root     root     4096 Jun  4 17:58 ..
    -rw-r--r-- 1 root     root       73 Jun  4 19:18 users.xml
    

    Be careful to not forget subPath, otherwise users.xml will be a directory !

    Back to my initial manifest :

    spec:
        containers:
        - name: php
          image: php:7.0-apache
          volumeMounts:
          - mountPath: /var/www/html
            name: site-data
        volumes:
        - name: site-data
          configMap:
            name: users
    

    I'll see those symlinks coming back :

    $ kubectl exec  my-lamp-site -c php -- ls -al /var/www/html
    total 12
    drwxrwxrwx 3 root root 4096 Jun  4 19:31 .
    drwxr-xr-x 3 root root 4096 Jun  4 17:58 ..
    drwxr-xr-x 2 root root 4096 Jun  4 19:31 ..2018_06_04_19_31_41.860238952
    lrwxrwxrwx 1 root root   31 Jun  4 19:31 ..data -> ..2018_06_04_19_31_41.860238952
    lrwxrwxrwx 1 root root   16 Jun  4 19:31 users.xml -> ..data/users.xml
    

    Many thanks to psycotica0 on K8s Canada slack for putting me on the right track with subpath (they are quickly mentioned in configmap documentation)

    0 讨论(0)
提交回复
热议问题