apiVersion: v1 data: key1: value1 key2: value2 kind: ConfigMap metadata: name: configmapexampleThe manifest file of the ConfigMap is pretty simple. It contains the fields apiVersion, kind, and metadata. It also contain the data field that contains the keys-values of the ConfigMap.
$ kubectl create configmap configmapfromliteral --from-literal="key1=value1" --from-literal="key2=value2"
configmap/configmapfromliteral createdCheck the ConfigMap:
$ kubectl get configmap configmapfromliteral
NAME DATA AGE configmapfromliteral 2 2m10sDescribe the ConfigMap:
$ kubectl describe configmap configmapfromliteral
Name: configmapfromliteral Namespace: default Labels: <none> Annotations: <none> Data ==== key1: ---- value1 key2: ---- value2 Events: <none>View the YAML file of the ConfigMap:
$ kubectl get configmap configmapfromliteral -o yaml
apiVersion: v1 data: key1: value1 key2: value2 kind: ConfigMap metadata: name: configmapfromliteral
$ mkdir ./configmaps/Create a sample file "file1.txt" (single line text):
$ echo -n 'value1' > "./configmaps/file1.txt"Create a sample file "file2.txt" (multiple lines text):
$ echo 'value2-1' > "./configmaps/file2.txt" $ echo 'value2-2' >> "./configmaps/file2.txt"
$ kubectl create configmap configmap1fromfile --from-file="./configmaps/file1.txt" --from-file="./configmaps/file2.txt"
configmap/configmap1fromfile createdView the ConfigMap (notice that the content of the file is indented properly):
$ kubectl get configmap configmap1fromfile -o yaml
apiVersion: v1 data: file1.txt: value1 # note that the name of the key is file1.txt and the value is single line file2.txt: | # note that the name of the key is file2.txt and the value is multiple lines value2-1 value2-2 kind: ConfigMap metadata: name: configmap1fromfile
$ kubectl create configmap configmap2fromfile --from-file="mykey1=./configmaps/file1.txt" --from-file="mykey2=./configmaps/file2.txt"
configmap/configmap2fromfile createdView the ConfigMap (notice that the content of the file is indented properly):
$ kubectl get configmap configmap2fromfile -o yaml
apiVersion: v1 data: mykey1: value1 # note that the name of the key is mykey1 and the value is single line mykey2: | # note that the name of the key is mykey2 and the value is multiple lines value2-1 value2-2 kind: ConfigMap metadata: name: configmap2fromfile
$ kubectl create configmap configmapfromdirectory --from-file="./configmaps/"
configmap/configmapfromdirectory createdView the ConfigMap (notice that the content of the file is indented properly):
$ kubectl get configmap configmapfromdirectory -o yaml
apiVersion: v1 data: file1.txt: value1 # note that the name of the key is file1.txt and the value is single line file2.txt: | # note that the name of the key is file2.txt and the value is multiple lines value2-1 value2-2 kind: ConfigMap metadata: name: configmapfromdirectory
$ vi "./configmaps/file1.properties"
key1=value1 key2=value2Create the ConfigMap:
$ kubectl create configmap configmapfromenvfile --from-env-file="./configmaps/file1.properties"
configmap/configmapfromenvfile createdView the ConfigMap (notice that each entry in the property file is represented by a key:value pairs in the ConfigMap):
$ kubectl get configmap configmapfromenvfile -o yaml
apiVersion: v1 data: key1: value1 key2: value2 kind: ConfigMap metadata: name: configmapfromenvfile
$ vi configmapkeyvalue.yaml
apiVersion: v1 kind: ConfigMap metadata: name: configmapkeyvalue data: key1: value1 key2: value2To apply the file:
$ kubectl apply -f configmapkeyvalue.yaml
configmap/configmapkeyvalue createdHere's a sample YAML with multi-line values entries:
$ vi configmapmultilinevalue.yaml
apiVersion: v1 kind: ConfigMap metadata: name: configmapmultilinevalue data: key3: | first line second line third line
$ kubectl edit configmap configmapkeyvalue
# Please edit the object below. # Lines beginning with a '#' will be ignored, and an empty file will abort the edit. # If an error occurs while saving this file will be reopened with the relevant failures. # apiVersion: v1 data: key1: value1 key2: value2 kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"key1":"value1","key2":"value2"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"configmapkeyvalue"}} name: configmapkeyvalue
configmap/configmapkeyvalue edited
$ vi hello-busybox-configmap-keyref.yaml
apiVersion: v1 kind: Pod metadata: name: hello-busybox-configmap-keyref spec: containers: - name: hello-busybox-configmap-keyref image: busybox:latest command: ['sh', '-c', 'sleep 300;'] env: - name: mykey1 valueFrom: configMapKeyRef: name: configmapkeyvalue key: key1 - name: mykey2 valueFrom: configMapKeyRef: name: configmapkeyvalue key: key2Note: You can give the environment variable a different name than the ConfigMap key name (i.e. name: mykey1).
$ kubectl apply -f hello-busybox-configmap-keyref.yaml
pod/hello-busybox-configmap-keyref createdCheck the environment variables:
$ kubectl exec hello-busybox-configmap-keyref -- env | grep key
mykey1=value1 mykey2=value2
$ vi hello-busybox-configmap-ref.yaml
apiVersion: v1 kind: Pod metadata: name: hello-busybox-configmap-ref spec: containers: - name: hello-busybox-configmap-ref image: busybox:latest command: ['sh', '-c', 'sleep 300;'] envFrom: - configMapRef: name: configmapkeyvalueApply the Pod:
$ kubectl apply -f hello-busybox-configmap-ref.yaml
pod/hello-busybox-configmap-ref createdCheck the environment variables:
$ kubectl exec hello-busybox-configmap-ref -- env | grep key
key1=value1 key2=value2
$ vi hello-busybox-configmap-volume.yaml
apiVersion: v1 kind: Pod metadata: name: hello-busybox-configmap-volume spec: containers: - name: hello-busybox-configmap-volume image: busybox:latest command: ['sh', '-c', 'sleep 300;'] volumeMounts: - name: configmapkeyvalue mountPath: /tmp/configmapkeyvalue volumes: - name: configmapkeyvalue configMap: name: configmapkeyvalueApply the Pod:
$ kubectl apply -f hello-busybox-configmap-volume.yaml
pod/hello-busybox-configmap-volume createdCheck the files in the volume (notice two files are created: key1, key2):
$ kubectl exec hello-busybox-configmap-volume -- ls -l /tmp/configmapkeyvalue
lrwxrwxrwx 1 root root key1 -> ..data/key1 lrwxrwxrwx 1 root root key2 -> ..data/key2Check the content of the files:
$ kubectl exec hello-busybox-configmap-volume -- cat /tmp/configmapkeyvalue/key1
value1
$ vi hello-busybox-configmap-volume-item.yaml
apiVersion: v1 kind: Pod metadata: name: hello-busybox-configmap-volume-item spec: containers: - name: hello-busybox-configmap-volume-item image: busybox:latest command: ['sh', '-c', 'sleep 300;'] volumeMounts: - name: configmapkeyvalue mountPath: /tmp/configmapkeyvalue volumes: - name: configmapkeyvalue configMap: name: configmapkeyvalue items: - key: key1 path: key1pathApply the Pod:
$ kubectl apply -f hello-busybox-configmap-volume-item.yaml
pod/hello-busybox-configmap-volume-item createdCheck the files in the volume (notice the "key1path" file was created):
$ kubectl exec hello-busybox-configmap-volume-item -- ls /tmp/configmapkeyvalue
key1pathCheck the content of the files:
$ kubectl exec hello-busybox-configmap-volume-item -- cat /tmp/configmapkeyvalue/key1path
value1
$ vi hello-busybox-configmap-keyref-arg.yaml
apiVersion: v1 kind: Pod metadata: name: hello-busybox-configmap-keyref-arg spec: containers: - name: hello-busybox-configmap-keyref-arg image: busybox:latest command: ['sh', '-c', 'echo "mykey1:$(mykey1) | mykey2:$(mykey2)"; sleep 300;'] env: - name: mykey1 valueFrom: configMapKeyRef: name: configmapkeyvalue key: key1 - name: mykey2 valueFrom: configMapKeyRef: name: configmapkeyvalue key: key2Apply the Pod:
$ kubectl apply -f hello-busybox-configmap-keyref-arg.yaml
pod/hello-busybox-configmap-keyref-arg createdCheck the logs of the Pod:
$ kubectl logs hello-busybox-configmap-keyref-arg
mykey1:value1 | mykey2:value2
$ kubectl delete -f configmapkeyvalue.yaml
configmap "configmapkeyvalue" deletedTo delete a ConfigMap using its name:
$ kubectl delete configmap configmapkeyvalue
configmap "configmapkeyvalue" deleted