Bootstrap FreeKB - OpenShift - Mount multiple Config Maps to the same directory using projected volume
OpenShift - Mount multiple Config Maps to the same directory using projected volume

Updated:   |  OpenShift articles

If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.

There are different ways to configure a container with environment variables.

Config Maps are used to:

Secrets are similar, used to create variables that contain encoded data (e.g. passwords). In this way, if a change is needed to a configuration file, variable, or command line option argument, you just need to update the config map or secret as opposed to having to make the change to your applications or deployments.

flowchart TB subgraph OpenShift["OpenShift Cluster"] CM[ConfigMap
Configuration Data] SEC[Secret
Sensitive Data] subgraph Pod["Pod"] subgraph Container["Container"] APP[Application] VOL1[/Volume Mount
Config Files/] VOL2[/Volume Mount
Secret Files/] ENV1[Environment Variables
from ConfigMap] ENV2[Environment Variables
from Secret] end end end CM -->|Mount as Volume| VOL1 SEC -->|Mount as Volume| VOL2 CM -->|Inject as EnvVar| ENV1 SEC -->|Inject as EnvVar| ENV2 VOL1 -.->|Read Config| APP VOL2 -.->|Read Secrets| APP ENV1 -.->|Use Config| APP ENV2 -.->|Use Secrets| APP style CM fill:#90CAF9 style SEC fill:#FFAB91 style APP fill:#A5D6A7 style Pod fill:#E1F5FE style Container fill:#F1F8E9

The oc get configmaps command can be used to list the config maps that have been created in the currently selected project.

~]$ oc get configmaps
NAME              DATA      AGE
foo-config-map    1         44d
bar-config-map    1         44d

 

The --output yaml or --output json options can be used to display the YAML or JSON details of the config map. Notice that the foo-config-map contains "hello" and bar-config-map contains "world".

~]$ oc get configmap foo-config-map --output yaml
apiVersion: v1
data:
  foo: hello
kind: ConfigMap

~]$ oc get configmap bar-config-map --output yaml
apiVersion: v1
data:
  bar: world
kind: ConfigMap

 

Let's say you have a deployment named "my-deployment".

~]# oc get deployments
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
my-deployment   1/1     1            1           8d

 

A config map can be made available to a container:

 

The oc edit deployment command can be used to update the deployment YAML.

AVOID TROUBLE

The name of the projected volume and the name of the volumeMounts, which is foo-and-bar in this example, must be an exact match.

The name of the config maps, which are foo-config-map and bar-config-map in this example, must be an exact match of the name of the config maps returned by the oc get configmaps command.

apiVersion: v1
kind: Deployment
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: my-app
        image: openshift/my-app:latest
        ports:
        - containerPort: 80
        volumeMounts:
          name: foo-and-bar
          mountPath: /var/configmaps
      volumes:
        - name: foo-and-bar
          projected:
            sources:  
              - configMap:
                  name: foo-config-map
                  items:
                    - key: foo
                      path: foo   
              - configMap:
                  name: bar-config-map
                  items:
                    - key: bar
                      path: bar

 

A new pod should immediately be created after the oc edit command has been completed.

The oc exec command can be used to now see that the config maps have been mounted in the container as symbolic links.

~]$ oc exec pod/my-app-8x2nz -- ls -l /var/configmaps
lrwxrwxrwx. 1 root root 10 Aug 28 10:20 bar -> ..data/bar
lrwxrwxrwx. 1 root root 10 Aug 28 10:20 foo -> ..data/foo

 

And the value of each config map can be viewed.

~]$ oc exec pod/my-app-8x2nz -- cat /var/configmaps/foo
Hello

~]$ oc exec pod/my-app-8x2nz -- cat /var/configmaps/bar
World

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter fc5cfc in the box below so that we can be sure you are a human.