Home > Mobile >  about kubernet configmap mountPath with subPath
about kubernet configmap mountPath with subPath

Time:05-21

The pod yaml

      containers:
        - name: kiada
          image: :kiada-0.1
          volumeMounts:
            - name: my-test
              subPath: my-app.conf
              mountPath: /html/my-app.conf
           
      volumes:
        - name: my-test
          configMap:
            name: kiada-config

the config map

➜  v5-kubernetes git:(master) ✗ k get cm kiada-config -oyaml
apiVersion: v1
data:
  key: value\n
  status-message: This status message is set in the kiada-config config map2\n
kind: ConfigMap
metadata:
  creationTimestamp: "2022-05-18T03:01:15Z"
  name: kiada-config
  namespace: default
  resourceVersion: "135185128"
  uid: 8c8875ce-47f5-49d4-8bc7-d8dbc2d7f7ba

the pod has my-app.conf

root@kiada2-7cc7bf55d8-m97tt:/# ls -al /html/my-app.conf/
total 12
drwxrwxrwx 3 root root 4096 May 21 02:29 .
drwxr-xr-x 1 root root 4096 May 21 02:29 ..
drwxr-xr-x 2 root root 4096 May 21 02:29 ..2022_05_21_02_29_41.554311630
lrwxrwxrwx 1 root root   31 May 21 02:29 ..data -> ..2022_05_21_02_29_41.554311630
lrwxrwxrwx 1 root root   10 May 21 02:29 key -> ..data/key
lrwxrwxrwx 1 root root   21 May 21 02:29 status-message -> ..data/status-message
root@kiada2-7cc7bf55d8-m97tt:/# ls -al /html/my-app.conf/

if i add subPath in pod yaml

spec:
      containers:
        - name: kiada
          image: kiada-0.1
          volumeMounts:
            - name: my-test
              subPath: my-app.conf
              mountPath: /html/my-app.conf
           
      volumes:
        - name: my-test
          configMap:
            name: kiada-config

the resutl

root@kiada2-c89749c8-x9qwq:/# ls -al html/my-app.conf/
total 8
drwxrwxrwx 2 root root 4096 May 21 02:36 .
drwxr-xr-x 1 root root 4096 May 21 02:36 ..

why i use subPath,the config map key is not exists ,what's wrong?

CodePudding user response:

In order to produce a file called my-app.config containing your application config in your Pod's file system, would have to ensure that this file exists in your config map:

apiVersion: v1
kind: ConfigMap
metadata:
  name: kiada-config
data:
  my-app.conf: |
    key: value
    status-message: This status message is set in the kiada-config config map2

Then, you can mount it into your Pod like this:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: kiada
  name: kiada
spec:
  containers:
    - name: kiada
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
      volumeMounts:
        - mountPath: /html/
          name: my-test
  volumes:
    - name: my-test
      configMap:
        name: kiada-config

The subPath field is not required in this scenario. It would be useful either if you want to remap the my-app.conf to a different name..

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: kiada
  name: kiada
spec:
  containers:
    - name: kiada
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
      volumeMounts:
        - mountPath: /html/my-app-new-name.conf
          name: my-test
          subPath: my-app.conf
  volumes:
    - name: my-test
      configMap:
        name: kiada-config

..or, if you had multiple config files in your ConfigMap and just wanted to map one of them into your Pod:

apiVersion: v1
kind: ConfigMap
metadata:
  name: kiada-config
data:
  my-app.conf: |
    key: value
    status-message: This status message is set in the kiada-config config map2
  my-second-app.conf: |
    error: not in use

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: kiada
  name: kiada
spec:
  containers:
    - name: kiada
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
      volumeMounts:
        - mountPath: /html/my-app.conf
          name: my-test
          subPath: my-app.conf
  volumes:
    - name: my-test
      configMap:
        name: kiada-config

CodePudding user response:

There is no file in your configmap i would suggest checking out the : https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume

configmap :

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm

POD deployment

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
  restartPolicy: Never

When the pod runs, the command ls /etc/config/ produces the output below:

SPECIAL_LEVEL
SPECIAL_TYPE

if you want to inject configmap with a different file name you can use items

      items:
        - key: SPECIAL_LEVEL
          path: keys

Example: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-specific-path-in-the-volume

  • Related