Till this time we have been writing definition files for a number of objects and specially in case of pods we have put all configuration in a single file. This is OK if our environment is small but as we grow and we have number of applications working together with large number of pods, its not convenient to put configuration in pod definition file. We can put the configuration in a separate object and we call it as ConfigMap.
The easiest way to define a config map is via a YAML file and then mount it using a volume to use it. Below is an example to create a config map.
[root@node1 kubernetes]# cat configmap.yaml kind: ConfigMap apiVersion: v1 metadata: name: example-configmap data: database: mongodb database_uri: mongodb://localhost:27017 [root@node1 kubernetes]# kubectl create -f configmap.yaml configmap/example-configmap created [root@node1 kubernetes]# kubectl get cm NAME DATA AGE example-configmap 2 6s
In production environment we usually mount configmap on a volume but because our lab environment is on virtual machines we will see other way to mount it that is with environment variable.
[root@node1 kubernetes]# cat pod-with-config-map.yaml kind: Pod apiVersion: v1 metadata: name: pod-with-cm spec: containers: - name: env-var-configmap image: nginx:1.7.9 envFrom: - configMapRef: name: example-configmap [root@node1 kubernetes]# kubectl create -f pod-with-config-map.yaml pod/pod-with-cm created
This is how we will create a configmap and use it as per our convinience