挂载整个configmap

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
apiVersion: v1
kind: ConfigMap
metadata:
name: busybox-configmap
data:
1.txt: |
11111111111111111
2.txt: |
22222222222222222

---

apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
spec:
selector:
matchLabels:
app: busybox
replicas: 1
template:
metadata:
labels:
app: busybox
spec:
volumes:
- name: busybox-volume
configMap:
name: busybox-configmap

containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- sleep infinity

volumeMounts:
- name: busybox-volume # volumes的名字
mountPath: /root/test/ # 容器内的目录

此时pod内/root/test/目录下会有两个文件 1.txt 和 2.txt。另外,如果/root/test/ 目录原来是有数据的,那么挂载后里面也就只有 1.txt 和 2.txt, 看不到原来的文件了。(和挂载类似)

挂载指定的key到目录

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
apiVersion: v1
kind: ConfigMap
metadata:
name: busybox-configmap
data:
1.txt: |
11111111111111111
2.txt: |
22222222222222222

---

apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
spec:
selector:
matchLabels:
app: busybox
replicas: 1
template:
metadata:
labels:
app: busybox
spec:
volumes:
- name: busybox-volume
configMap:
name: busybox-configmap
items:
- key: 1.txt # 对应的configmap的key
path: logstash.yml # 挂载到容器内的文件名
- key: 2.txt
path: jvm.options

containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- sleep infinity

volumeMounts:
- name: busybox-volume # volumes的名字
mountPath: /root/test/ # 容器内的目录

如上配置, 在容器中文件即为:

/root/test/logstash.yml –> 11111111111111111
/root/test/jvm.options –> 22222222222222222

使用subpath挂载configmap

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
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
spec:
selector:
matchLabels:
app: busybox
replicas: 1
template:
metadata:
labels:
app: busybox
spec:
volumes:
- name: busybox-volume
configMap:
name: busybox-configmap

containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- sleep infinity
volumeMounts:
- name: busybox-volume # volume的名字
mountPath: /root/busybox-volume/1.txt # 容器内的路径
subPath: 1.txt # 指定configmap里面的key
- name: busybox-volume
mountPath: /root/busybox-volume/2.txt
subPath: 2.txt

这种挂载方式不会影响到/root/test/目录内原始的文件,只是挂载了这两个文件。