Can we use same Configmap for different volume mounts?

2/11/2019

Two pods are running and have different volume mounts, but there is a need of using the same configmap in the 2 running pods.

-- Nancy
configmap
kubernetes

1 Answer

2/11/2019

Sure you can do that. You can mount same ConfigMap into different volume. You can take a look into configure-pod-configmap.

Say, your ConfigMap is like following:

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

And two pods:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod-01
spec:
  containers:
    - name: test-container
      image: busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never
---
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod-02
spec:
  containers:
    - name: test-container
      image: busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never

Now see the logs after creating the above ConfigMap and two Pods:

# for 1st Pod
$ kubectl logs -f dapi-test-pod-01
SPECIAL_LEVEL
SPECIAL_TYPE

# for 2nd Pod
$ kubectl logs -f dapi-test-pod-02
SPECIAL_LEVEL
SPECIAL_TYPE
-- Shudipta Sharma
Source: StackOverflow