ConfigMap volume is not mounting as volume along with secret

5/12/2021

I have references to both Secrets and ConfigMaps in my Deployment YAML file. The Secret volume is mounting as expected, but not the ConfigMap.

I created a ConfigMap with multiple files and when I do a kubectl get configmap ... it shows all of the expected values. Also, when I create just ConfigMaps it's mounting the volume fine, but not along with a Secret.

I have tried different scenarios of having both in same directory to separating them but dont seem to work.

Here is my YAML. Am I referencing the ConfigMap correctly?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deployment
spec:
  selector:
    matchLabels:
      app: hello
  replicas: 1 # tells deployment to run 1 pod
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - name: hello
        image: XXX/yyy/image-50
        ports:
        - containerPort: 9009
          protocol: TCP      
        volumeMounts:
        - mountPath: /shared
          name: configs-volume
          readOnly: true
        volumeMounts:
        - mountPath: data/secrets
          name: atp-secret
          readOnly: true
      volumes:
      - name: configs-volume
        configMap:
          name: prompts-config
          key: application.properties
      volumes:
      - name: atp-secret
        secret:
          defaultMode: 420
          secretName: dev-secret
      restartPolicy: Always
      imagePullSecrets:
      - name: ocirsecrets
-- mamidala86
configmap
kubernetes

1 Answer

5/12/2021

You have two separate lists of volumes: and also two separate lists of volumeMounts:. When Kubernetes tries to find the list of volumes: in the Pod spec, it finds the last matching one in each set.

volumes:
- name: configs-volume
volumes: # this completely replaces the list of volumes
- name: atp-secret

In both cases you need a single volumes: or volumeMounts: key, and then multiple list items underneath those keys.

volumes:
- name: configs-volume
  configMap: { ... }
- name: atp-secret                 # do not repeat volumes: key
  secret: { ... }
containers:
  - name: hello
    volumeMounts:
      - name: configs-volume
        mountPath: /shared
      - name: atp-secret           # do not repeat volumeMounts: key
        mounthPath: /data/secrets
-- David Maze
Source: StackOverflow