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.
- In a deployment YAML file
- Using Config Map (this article)
- Using Secrets
Config Maps are used to:
- mount configuration files in a container
- create environment variables in a container
- create command line option arguments in a container
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 will display the config maps in the currently selected project / namespace.
TIP
The -A or --all-namespaces flag can be used to list the config maps in every project / namespace.
The -n or --namespace flag can be used to list the config maps in a certain project / namespace.
~]$ oc get configmaps
NAME DATA AGE
my-config-map 1 44d
The oc describe configmap command can be used to show the files, variables, and command line options that are defined in the config map.
~]$ oc describe configmap my-config-map
Name: my-config-map
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
foo:
----
hello
bar:
----
world
Events: <none>
Or, the oc get configmap command with the --output json or --output yaml option can be used.
~]$ oc get configmap my-config-map --output yaml
apiVersion: v1
data:
foo: hello
bar: world
kind: ConfigMap
metadata:
creationTimestamp: "2021-10-20T10:25:10Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {}
f:key1: {}
f:key2: {}
manager: kubectl-create
operation: Update
time: "2021-10-20T10:25:10Z"
name: my-config-map
namespace: default
resourceVersion: "104538499"
selfLink: /api/v1/namespaces/default/configmaps/my-config-map
uid: 75469a11-949a-4b0d-90f6-adda92cdad49
The --output jsonpath option can be used to print the value of a specific JSON key.
~]$ oc get configmap my-config-map --output jsonpath={.data.foo}
hello
Did you find this article helpful?
If so, consider buying me a coffee over at 