-1

Requirement is to load properties from 2 config maps: one service specific config map and the other will have properties common to the application.

Can someone provide a sample configuration for this and also how do I determine the order in which config maps will be loaded/ read. Thanks

Ken White
  • 123,280
  • 14
  • 225
  • 444
Sushant Bokde
  • 17
  • 1
  • 6
  • It's not clear to me exactly what it is you're trying to do. You create the config maps and either mount them as a volume and store the keys as files or you can set the keys as environment variables in the containers of your pods. The order is irrelevant because they'll be set before your containers start. – Peyman Jul 21 '21 at 13:03
  • 1
    @Peyman of course the order is relevant. What if two config maps contain identical keys? – wvxvw Jul 21 '21 at 14:05
  • I don't believe there's a way to provide multiple configmaps as a source for a environment variable. So, if your idea was to load one configmap and if the variable isn't there, then load the other one, I'm afraid this isn't possible in Kubernetes. You will have to load both of them separately and do the merging yourself. – wvxvw Jul 21 '21 at 14:11
  • @peyman Idea is to create the config maps and mount them as a volume i tried the following approach and it did not work https://stackoverflow.com/questions/49287078/how-to-merge-two-configmaps-using-volume-mount-in-kubernetes – Sushant Bokde Jul 21 '21 at 14:26
  • @wvxvw the mount path has to be unique so you can't put contents from two config maps into the same directory as far as I understand so you wouldn't run into that conflict. – Peyman Jul 21 '21 at 16:20

1 Answers1

2

Here's an example of mounting config map entries as files:

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-1
data:
  file1.txt: FILE1
  file2.txt: FILE2
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: config-2
data:
  boo1: BOO1
  boo3: BOO3
---
apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
    - image: nginx:stable-alpine
      name: nginx
      volumeMounts:
        - name: a
          mountPath: /etc/configs/a
        - name: b
          mountPath: /etc/configs/b
  volumes:
    - name: a
      configMap:
        name: config-1  # mount all keys in the ConfigMap config-1 as files.
    - name: b
      configMap:
        name: config-2
        items:  # you can pick and choose which files to bring.
          - key: boo1
            path: boo1.txt # you can rename them as well, we'll be mounting boo1 from config-2 ConfigMap but we'll be mounting it as boo1.txt
Peyman
  • 3,059
  • 1
  • 33
  • 68
  • Yes, I was able to implement a solution with @peyman's response above. 1 slight change I had to make is volumeMounts: - name: a mountPath: /etc/configs - name: b mountPath: /etc/configs/b because in my case spring boot was looking for one confog map in /etc/config folder. – Sushant Bokde Jul 23 '21 at 08:59