zhangguanzhang's Blog

kubernetes的configMap文件挂载不同的路径且不覆盖目录的解决方法

字数统计: 362阅读时长: 1 min
2018/04/29 Share

看到configMap这个资源对象,突发奇想

  • 如何让一个configMap里文件挂载到容器的不同路径

书上和网上都找不到对应例子

最终github上搜索到了有个subPath选项除了支持路径还不会覆盖容器内部的整个目录
参考链接https://github.com/dshulyak/kubernetes.github.io/commit/d58ba7b075bb4848349a2c920caaa08ff3773d70
这里记录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[root@k8s-m1 k8s]# grep -Pv '#' test-node.yml 
apiVersion: v1
kind: ConfigMap
metadata:
name: testmap
data:
test.file: |
this is a test strings
test.file2: |
this is the 2nd strings
---
apiVersion: v1
kind: Pod
metadata:
name: pod-test
spec:
containers:
- name: test
image: busybox
tty: true
command: ["sh"]
volumeMounts:
- name: testdir
mountPath: /root/test.map1
subPath: test.map1
- name: testdir
mountPath: /etc/test.map2
subPath: test.map2
volumes:
- name: testdir
configMap:
name: testmap
items:
- key: test.file
path: test.map1
- key: test.file2
path: test.map2
[root@k8s-m1 k8s]# kubectl apply -f test-node.yml
configmap "testmap" created
pod "pod-test" created
[root@k8s-m1 k8s]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod-test 1/1 Running 0 5s
[root@k8s-m1 k8s]# kubectl exec -ti pod-test ls /etc
group hosts mtab passwd shadow
hostname localtime network resolv.conf test.map2
[root@k8s-m1 k8s]# kubectl exec -ti pod-test ls /root
test.map1
[root@k8s-m1 k8s]# kubectl exec -ti pod-test cat /root/test.map1
this is a test strings
[root@k8s-m1 k8s]# kubectl exec -ti pod-test cat /etc/test.map2
this is the 2nd strings

上面可以看出不会把一个文件挂载到容器路径里会把容器的那个目录给掩盖掉,不过必须指定到路径到文件名才行
不使用subPath下只能指定到目录,文件名使用path的名字,切会掩盖住容器的目录

CATALOG