Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 stripe2.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 ?

like image 692
Anthony Dahanne Avatar asked Jun 04 '18 16:06

Anthony Dahanne


People also ask

What does config map do in Kubernetes?

A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable.

What is the difference between Kubernetes Engine config maps and secrets?

Secrets in Kubernetes Both ConfigMaps and secrets store the data the same way, with key/value pairs, but ConfigMaps are meant for plain text data, and secrets are meant for data that you don't want anything or anyone to know about except the application.

How do I read a ConfigMap in Kubernetes?

If you want to view the binaryData keys (and their values) in a ConfigMap, you can run kubectl get configmap -o jsonpath='{. binaryData}' <name> . Starting with Kubernetes v1. 23, kubectl supports the --from-env-file argument to be specified multiple times to create a ConfigMap from multiple data sources.


1 Answers

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)

like image 177
Anthony Dahanne Avatar answered Sep 21 '22 15:09

Anthony Dahanne