How to Create and Use ConfigMaps in Kubernetes

  • Config: keeps configuration information.
  • Maps: In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection. Learn more on this Wiki page.

Prerequisites

This tutorial is written using Windows 10, kubectl, and minikube. If you have a similar setup you are good to go.

Important Note for Windows Users

All files you create must have Unix (LF) line endings, not Windows ( CR LF ) endings. Read your editor documentation on how to change it to default to LF line endings. There should also be some EOL conversion menu option somewhere to convert to LF.

Basic Example: Create ConfigMaps from a file

You should speed-read rush through the first few basic examples. Its very easy to do and understand.

nano config-map-data.txtkey1=value1
key2=23
kubectl create configmap configmap-example-1 --from-file=./config-map-data.txtconfigmap/configmap-example-1 created
kubectl describe configmap/configmap-example-1Name:         configmap-example-1
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
config-map-data.txt:
----
key1=value1
key2=23
Events: <none>
kubectl delete configmap/configmap-example-1configmap "configmap-example-1" deleted
nano config-map-data.txtkey1=value1
key2=23
# comment 1## comment 2
kubectl create configmap configmap-example-1 --from-file=./config-map-data.txtconfigmap/configmap-example-1 created
kubectl describe configmap/configmap-example-1Name:         configmap-example-1
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
config-map-data.txt:
----
key1=value1
key2=23
# comment 1## comment 2Events: <none>
kubectl get configmap configmap-example-1 -o yamlapiVersion: v1
data:
config-map-data.txt: |+
key1=value1
key2=23
# comment 1 ## comment 2kind: ConfigMap
kubectl delete configmap/configmap-example-1configmap "configmap-example-1" deleted

Basic Example: Create ConfigMaps using — from-env-file

This section shows how to create a ConfigMap using the Docker environment file format.

kubectl create configmap configmap-example-1 --from-env-file=./config-map-data.txtconfigmap/configmap-example-1 created
kubectl describe configmap/configmap-example-1Name:         configmap-example-1
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
key1:
----
value1
key2:
----
23
Events: <none>
kubectl get configmap configmap-example-1 -o yamlapiVersion: v1
data:
key1: value1
key2: "23"
kind: ConfigMap

Basic Example: Create ConfigMaps from Literal Values

Simple example of how to create ConfigMaps from literal values.

kubectl create configmap literal-config-1 --from-literal=literal-key1=value1 --from-literal=literal-key2=22configmap/literal-config-1 created
kubectl describe configmap/literal-config-1Name:         literal-config-1
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
literal-key1:
----
value1
literal-key2:
----
22
Events: <none>
kubectl delete configmap/literal-config-1configmap "literal-config-1" deleted
kubectl create configmap literal-config-1 --from-literal=city=London --from-literal=population=millionsconfigmap/literal-config-1 created
kubectl describe configmap/literal-config-1Name:         literal-config-1
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
city:
----
London
population:
----
millions
Events: <none>

Basic Example: Create ConfigMaps from Directories

You will now see how to create a ConfigMap that combines the content of all files in a directory.

mkdir ConfigMap-Foldernano ./ConfigMap-Folder/file-1.txt
file-1-key-one:1
file-1-key-two:awesome
nano ./ConfigMap-Folder/file-2.txt
file-2-key-one:2
file-2-key-two:cool
kubectl create configmap from-folder-configmap --from-file=ConfigMap-Folderconfigmap/from-folder-configmap created
kubectl describe configmap/from-folder-configmapName:         from-folder-configmap
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
file-1.txt:
----
file-1-key-one:1
file-1-key-two:awesome
file-2.txt:
----
file-2-key-one:2
file-2-key-two:cool
Events: <none>
kubectl get configmaps from-folder-configmap -o yamlapiVersion: v1
data:
file-1.txt: "file-1-key-one:1\r\nfile-1-key-two:awesome\r\n"
file-2.txt: "file-2-key-one:2\r\nfile-2-key-two:cool\r\n"
kind: ConfigMap
kubectl delete configmaps/from-folder-configmapconfigmap "from-folder-configmap" deleted
kubectl create configmap from-folder-configmap --from-file=ConfigMap-Folderconfigmap/from-folder-configmap created
kubectl get configmaps from-folder-configmap -o yamlapiVersion: v1
data:
file-1.txt: |
file-1-key-one:1
file-1-key-two:awesome
file-2.txt: |
file-2-key-one:2
file-2-key-two:cool
kind: ConfigMap

Access ConfigMap data via container environment variables

We did not delete ConfigMap literal-config-1. We use it here, this is its content:

kubectl get configmaps literal-config-1 -o yamlapiVersion: v1
data:
city: London
population: millions
kind: ConfigMap
nano ConfigMap-demo-1.yamlapiVersion: v1
kind: Pod
metadata:
name: configmap-pod-1
spec:
containers:
- name: cm-container-1
image: alpine
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "env" ]
env:
- name: ENV_CITY
valueFrom:
configMapKeyRef:
name: literal-config-1
key: city
restartPolicy: Never
kubectl create -f ConfigMap-demo-1.yamlpod/configmap-pod-1 created
kubectl logs configmap-pod-1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
HOSTNAME=configmap-pod-1
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
ENV_CITY=London
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
kubectl get poNAME                  READY   STATUS      RESTARTS   AGE
pod/configmap-pod-1 0/1 Completed 0 2m46s
kubectl describe pod/configmap-pod-1Name:               configmap-pod-1
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: minikube/10.0.2.15
Start Time: Wed, 02 Jan 2019 11:24:24 +0200
Labels: <none>
Annotations: <none>
Status: Succeeded
IP: 172.17.0.5
Containers:
cm-container-1:
Container ID: docker://791e4ae1f8aad006ee77d81c401cf19221764805952cc9190e16a722d4bb9929
Image: alpine
Image ID: docker-pullable://alpine@sha256:46e71df1e5191ab8b8034c5189e325258ec44ea739bba1e5645cff83c9048ff1
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
env
State: Terminated
Reason: Completed
Exit Code: 0
Started: Wed, 02 Jan 2019 11:24:25 +0200
Finished: Wed, 02 Jan 2019 11:24:25 +0200
Ready: False
Restart Count: 0
Environment:
ENV_CITY: <set to the key 'city' of config map 'literal-config-1'> Optional: false
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gs2wt (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-gs2wt:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-gs2wt
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m18s default-scheduler Successfully assigned default/configmap-pod-1 to minikube
Normal Pulled 3m17s kubelet, minikube Container image "alpine" already present on machine
Normal Created 3m17s kubelet, minikube Created container
Normal Started 3m17s kubelet, minikube Started container
Normal SandboxChanged 3m15s kubelet, minikube Pod sandbox changed, it will be killed and re-created.
kubectl describe pod/configmap-pod-1Name:               configmap-pod-1
Start Time: Wed, 02 Jan 2019 11:24:24 +0200
Status: Succeeded
Containers:
cm-container-1:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Wed, 02 Jan 2019 11:24:25 +0200
Finished: Wed, 02 Jan 2019 11:24:25 +0200
Ready: False
Environment:
ENV_CITY: <set to the key 'city' of config map 'literal-config-1'> Optional: false
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Status:             SucceededState:          Terminated
Reason: Completed
Exit Code: 0
Started: Wed, 02 Jan 2019 11:24:25 +0200
Finished: Wed, 02 Jan 2019 11:24:25 +0200
Type              Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f ConfigMap-demo-1.yaml --force --grace-period=0warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "configmap-pod-1" force deleted

Access 2 ConfigMap’s Data via Container Environment Variables

Let’s create another literal ConfigMap so we have 2 ConfigMaps to access in our Pod.

kubectl create configmap literal-config-2 --from-literal=shipping=free --from-literal=region=allconfigmap/literal-config-2 created
kubectl get configmaps literal-config-2 -o yamlapiVersion: v1
data:
region: all
shipping: free
kind: ConfigMap
nano ConfigMap-demo-2.yamlapiVersion: v1
kind: Pod
metadata:
name: configmap-pod-2
spec:
containers:
- name: cm-container-2
image: alpine
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "env" ]
env:
- name: ENV_CITY
valueFrom:
configMapKeyRef:
name: literal-config-1
key: city
- name: ENV_SHIPPING
valueFrom:
configMapKeyRef:
name: literal-config-2
key: shipping
restartPolicy: Never
kubectl create -f ConfigMap-demo-2.yamlpod/configmap-pod-2 created
kubectl logs configmap-pod-2KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
HOSTNAME=configmap-pod-2
SHLVL=1
HOME=/root
ENV_SHIPPING=free
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
ENV_CITY=London
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.96.0.1

Access All Key-Value Pairs in a ConfigMap as Environment Variables

Show the content of both keys in literal-config-1

kubectl get configmaps literal-config-1 -o yamlapiVersion: v1
data:
city: London
population: millions
nano ConfigMap-demo-3.yamlapiVersion: v1
kind: Pod
metadata:
name: configmap-pod-3
spec:
containers:
- name: cm-container-3
image: alpine
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: literal-config-1
restartPolicy: Never
envFrom:
- configMapRef:
name: literal-config-1
kubectl create -f ConfigMap-demo-3.yamlpod/configmap-pod-3 created
kubectl logs configmap-pod-3KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=configmap-pod-3
population=millions
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
city=London

Access All Key-Value Pairs in a 2 ConfigMaps as Environment Variables

To access all the keys in SEVERAL ConfigMaps we just do not refer to any specific key in our spec.

nano ConfigMap-demo-4.yamlapiVersion: v1
kind: Pod
metadata:
name: configmap-pod-4
spec:
containers:
- name: cm-container-4
image: alpine
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: literal-config-1
- configMapRef:
name: literal-config-2
restartPolicy: Never
kubectl create -f ConfigMap-demo-4.yamlpod/configmap-pod-4 created
kubectl get configmaps literal-config-1 -o yaml | head -4apiVersion: v1
data:
city: London
population: millions
kubectl get configmaps literal-config-2 -o yaml | head -4apiVersion: v1
data:
region: all
shipping: free
kubectl logs configmap-pod-4KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
HOSTNAME=configmap-pod-4
population=millions
SHLVL=1
HOME=/root
region=all
shipping=free
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
city=London

Clean Up

Get a list of your ConfigMaps :

kubectl get cmNAME                    DATA   AGE
configmap-example-1 1 25h
from-folder-configmap 2 24h
literal-config-1 2 25h
literal-config-2 2 23h
kubectl delete cm/configmap-example-1
kubectl delete cm/from-folder-configmap
kubectl delete cm/literal-config-1
kubectl delete cm/literal-config-2
kubectl delete -f ConfigMap-demo-1.yaml --force --grace-period=0kubectl delete -f ConfigMap-demo-2.yaml --force --grace-period=0kubectl delete -f ConfigMap-demo-3.yaml --force --grace-period=0kubectl delete -f ConfigMap-demo-4.yaml --force --grace-period=0

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com