k8s中subpath的使用

警告
本文最后更新于 2020-08-04 13:38,文中内容可能已过时。

有两种情况:

1.做为volumes使用时,subPath代表存储卷的子路径:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Pod
metadata:
  name: testpod0
spec:
  containers:
  - name: testc
    image: busybox
    command: ["/bin/sleep","10000"]
    volumeMounts:
      - name: data
        mountPath: /opt/data    # 挂载的路径
        subPath: data           # volume的子路径
      - name: data
        mountPath: /opt/model
        subPath: model
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: test-data

2.作为configmap/secret使用时,subPath代表configmap/secret的子路径: configmap:

1
2
3
4
5
6
7
apiVersion: v1
kind: ConfigMap
metadata:
  name: config-test
data:
  config.ini: "hello"
  config.conf: "nihao"

单独挂载一个key为文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: Pod
metadata:
  name: testpod
spec:
  containers:
  - name: testc
    image: busybox
    command: ["/bin/sleep","10000"]
    volumeMounts:
      - name: config-test
        mountPath: /etc/config.ini   # 最终在容器中的文件名
        subPath: config.ini  #要挂载的confmap中的key的名称
  volumes:
    - name: config-test
      configMap:
        name: config-test

挂载多个key为文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Pod
metadata:
  name: testpod2
spec:
  containers:
  - name: testc
    image: busybox
    command: ["/bin/sleep","10000"]
    volumeMounts:
      - name: config-test
        mountPath: /etc/config.ini   # 最终在容器中的文件名
        subPath: config.ini  #要挂载的confmap中的key的名称
      - name: config-test
        mountPath: /etc/config.conf   # 最终在容器中的文件名
        subPath: config.conf  #要挂载的confmap中的key的名称
  volumes:
    - name: config-test
      configMap:
        name: config-test

多个container挂载不同的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
apiVersion: v1
kind: Pod
metadata:
  name: testpod1
spec:
  containers:
  - name: testc
    imagePullPolicy: Never
    image: busybox
    command: ["/bin/sleep","10000"]
    volumeMounts:
      - name: config-test
        mountPath: /etc/config/config.ini
        subPath: config.ini
  - name: testc1
    imagePullPolicy: Never
    image: busybox
    command: ["/bin/sleep","10000"]
    volumeMounts:
      - name: config-test
        mountPath: /etc/config/config.conf
        subPath: config.conf
  volumes:
    - name: config-test
      configMap:
        name: config-test
        items:
        - key: config.ini
          path: config.ini
        - key: config.conf
          path: config.conf
请我喝杯水
SoulChild 微信号 微信号
SoulChild 微信打赏 微信打赏
0%