關於k8s中subpath的使用詳解

有兩種情況:

1.做為volumes使用時,subPath代表存儲卷的子路徑:

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的子路徑
        mountPath: /opt/model
        subPath: model
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: test-data

2.作為configmap/secret使用時,subPath代表configmap/secret的子路徑:

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-test
data:
  config.ini: "hello"
  config.conf: "nihao"

單獨掛載一個key為文件

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為文件:

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的名稱
        mountPath: /etc/config.conf   # 最終在容器中的文件名
        subPath: config.conf  #要掛載的confmap中的key的名稱
  volumes:
    - name: config-test
      configMap:
        name: config-test

多個container掛載不同的key:

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

摘自

https://soulchild.cn/1911.html

到此這篇關於關於k8s中subpath的使用詳解的文章就介紹到這瞭,更多相關k8s subpath使用內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: