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

时间:2018-06-04 16:53:53

标签: kubernetes configmap

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 .

For example, if my config map is named tc-configs and contains 2 xml files named stripe1.xml and strip2.xml, if I mount this config map to /configs in my container, I will have, in my container :

bash-4.4# ls -al /configs/ total 12 drwxrwxrwx 3 root root 4096 Jun 4 14:47 . drwxr-xr-x 1 root root 4096 Jun 4 14:47 .. drwxr-xr-x 2 root root 4096 Jun 4 14:47 ..2018_06_04_14_47_03.291041453 lrwxrwxrwx 1 root root 31 Jun 4 14:47 ..data -> ..2018_06_04_14_47_03.291041453 lrwxrwxrwx 1 root root 18 Jun 4 14:47 stripe1.xml -> ..data/stripe1.xml lrwxrwxrwx 1 root root 18 Jun 4 14:47 stripe2.xml -> ..data/stripe2.xml

I guess Kubernetes requires those symlinks and ../data and ..timestamp/ folders, but I know some applications that can fail to startup if they see non expected files or folders

Is there a way to tell Kubernetes not to generate all those symlinks and directly mount the files ?

2 个答案:

答案 0 :(得分:2)

我认为此解决方案令人满意:在mountPath中指定确切的文件路径,将删除符号链接到..data and ..2018_06_04_19_31_41.860238952

所以如果我申请这样的清单:

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>

显然,我正在隐含地使用subpath,而且它们不是ConfigMaps中“自动更新魔法”的一部分,我不会再看到符号链接了:

$ 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

小心不要忘记subPath,否则users.xml将成为目录!

回到我的初始清单:

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

我会看到那些符号链接回来了:

$ 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

非常感谢psycotica0上的K8s Canada slack让我使用subpath(我们很快在configmap documentation中提及 < / p>

答案 1 :(得分:0)

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.