Kubernetes - ConfigMap
Kubernetes ConfigMap
Problem with setting the configuration in the deployment file is tight coupling between application and configuration. ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. ConfigMaps can be generated from Files, Literals or Environment Files
Create ConfigMap from Literal
1 | kubectl create configmap app-config --from-literal=FOO=bar --from-literal=BAZ=qux |
Use kubectl describe cm app-config
to view the config map. Use kubectl describe cm app-config
command to describe the config map.
1 | kubectl describe cm app-config |
Create ConfigMap from File
When you create a ConfigMap using –from-file, the filename becomes a key stored in the data section of the ConfigMap. The file contents become the key’s value.
1 | $ echo -n bar > ./FOO |
Create ConfigMap from Env File
1 | $ cat << EOF > env |
Create ConfigMap using YAML file
app-configmap.yaml
1 | apiVersion: v1 |
Using ConfigMap as Environment Variable
busybox-configmap.yaml - set environment variable for a pod using configmap
1 | apiVersion: v1 |
after the pod is created, use kubectl logs busybox
to check the pod’s log. FOO environment variable is set with value “bar”.
Add ConfigMap data to a Volume
You can add ConfigMap to a container as a volume
pod-configmap-volume.yml
1 | apiVersion: v1 |
When the pod runs, file /etc/config/Foo is displayed. it contains the value for key FOO. Value is ‘bar’