Configmaps
Introduction
used to store key value pairs
allows portability of containerised applications
1. Configmaps can be created from
files
directories
literal values
2. General syntax
kubectl create configmap <map-name> <data-source>
map-name is name of configmap you want to assign
data-source is the file,directory or literal value
3.Creating configmaps from directory
- let's see an example
mkdir -p configure-pod-container/configmap/kubectl/
wget https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties -o configure-pod-container/configmap/kubectl/game.properties
wget https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties -o configure-pod-container/configmap/kubectl/ui.properties
kubectl create configmap game-config --from-file=configure-pod-container/configmap/kubectl/
hadoop@k8s-00:~$ kubectl create configmap game-config --from-file=configure-pod-container/configmap/kubectl/
configmap "game-config" created
where name of configmap is game-config
and data-source is the two files in the directory
hadoop@k8s-00:~/configure-pod-container/configmap/kubectl$ tree
.
├── game.properties
└── ui.properties
0 directories, 2 files
- let's describe the configmap
Data
====
game.properties:
----
--2018-06-18 13:56:30-- https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties
Resolving k8s.io (k8s.io)... 23.236.58.218
Connecting to k8s.io (k8s.io)|23.236.58.218|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://kubernetes.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties [following]
--2018-06-18 13:56:31-- https://kubernetes.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties
Resolving kubernetes.io (kubernetes.io)... 45.54.44.100
Connecting to kubernetes.io (kubernetes.io)|45.54.44.100|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 157 [application/octet-stream]
Saving to: ‘game.properties’
0K 100% 4.20M=0s
2018-06-18 13:56:32 (4.20 MB/s) - ‘game.properties’ saved [157/157]
ui.properties:
----
--2018-06-18 13:56:32-- https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties
Resolving k8s.io (k8s.io)... 23.236.58.218
Connecting to k8s.io (k8s.io)|23.236.58.218|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://kubernetes.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties [following]
--2018-06-18 13:56:33-- https://kubernetes.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties
Resolving kubernetes.io (kubernetes.io)... 45.54.44.100
Connecting to kubernetes.io (kubernetes.io)|45.54.44.100|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 83 [application/octet-stream]
Saving to: ‘ui.properties’
0K 100% 2.59M=0s
2018-06-18 13:56:34 (2.59 MB/s) - ‘ui.properties’ saved [83/83]
4. Creating configmaps from files
hadoop@k8s-00:~$ kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/kubectl/game.properties
configmap "game-config-2" created
- describe the configmap as follows, having created using a single file only
hadoop@k8s-00:~$ kubectl describe configmaps game-config-2
Name: game-config-2
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.properties:
- --from-file argument can be passed multiple times as follows
kubectl create configmap game-config-3 --from-file=configure-pod-container/configmap/kubectl/game.properties --from-file=configure-pod-container/configmap/kubectl/ui.properties
hadoop@k8s-00:~$ kubectl create configmap game-config-3 --from-file=configure-pod-container/configmap/kubectl/game.properties --from-file=configure-pod-container/configmap/kubectl/ui.properties
configmap "game-config-3" created
- describe the configmap
Data
====
game.properties:
----
--2018-06-18 13:56:30-- https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties
Resolving k8s.io (k8s.io)... 23.236.58.218
Connecting to k8s.io (k8s.io)|23.236.58.218|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://kubernetes.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties [following]
--2018-06-18 13:56:31-- https://kubernetes.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties
Resolving kubernetes.io (kubernetes.io)... 45.54.44.100
Connecting to kubernetes.io (kubernetes.io)|45.54.44.100|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 157 [application/octet-stream]
Saving to: ‘game.properties’
0K 100% 4.20M=0s
2018-06-18 13:56:32 (4.20 MB/s) - ‘game.properties’ saved [157/157]
ui.properties:
----
--2018-06-18 13:56:32-- https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties
Resolving k8s.io (k8s.io)... 23.236.58.218
Connecting to k8s.io (k8s.io)|23.236.58.218|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://kubernetes.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties [following]
--2018-06-18 13:56:33-- https://kubernetes.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties
Resolving kubernetes.io (kubernetes.io)... 45.54.44.100
Connecting to kubernetes.io (kubernetes.io)|45.54.44.100|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 83 [application/octet-stream]
Saving to: ‘ui.properties’
0K 100% 2.59M=0s
2018-06-18 13:56:34 (2.59 MB/s) - ‘ui.properties’ saved [83/83]
5. Creating a configmap using a env file
- get the env file as follows
wget https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game-env-file.properties -o configure-pod-container/configmap/kubectl/game-env-file.properties
- edit the properties file with the following content
cat configure-pod-container/configmap/kubectl/game-env-file.properties
enemies=aliens
lives=3
allowed="true"
- now create the configmap using the env file we just created
hadoop@k8s-00:~$ kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/kubectl/game-env-file.properties
configmap "game-config-env-file" created
- describe the configmap as follows
hadoop@k8s-00:~$ kubectl get configmap game-config-env-file -o yaml
apiVersion: v1
data:
allowed: '"true"'
enemies: aliens
lives: "3"
kind: ConfigMap
metadata:
creationTimestamp: 2018-06-18T14:10:50Z
name: game-config-env-file
namespace: default
resourceVersion: "47999"
selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file
uid: 6789c931-7301-11e8-ad8d-76fd51353592
- when env-files are passed multiple times, only the last env file is considered
6. Create configmaps from literal values
- in this we define key-value pair from terminal itself as follows
hadoop@k8s-00:~$ kubectl create configmap special-config --from-literal=company.what=admatic --from-literal=skills.how=impeccable
configmap "special-config" created
- let's describe the configmap as follows
hadoop@k8s-00:~$ kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
company.what: admatic
skills.how: impeccable
kind: ConfigMap
metadata:
creationTimestamp: 2018-06-18T14:16:48Z
name: special-config
namespace: default
resourceVersion: "48468"
selfLink: /api/v1/namespaces/default/configmaps/special-config
uid: 3cc94fe0-7302-11e8-ad8d-76fd51353592
7. Define Pod environment variables using ConfigMap data
- create a pod configmap-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: config
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: company.what
restartPolicy: Never
- run it as follows
hadoop@k8s-00:~$ kubectl create -f configmap-pod.yaml --validate=false
pod "podtestingconfig" created
hadoop@k8s-00:~$ kubectl describe configmaps special-config
Name: special-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
company.what:
----
admatic
skills.how:
----
impeccable
Events: <none>