References:
ConfigMaps
Kubernetes API: ConfigMap
Agenda
- Overview.
- Create a ConfigMap and a Pod that uses this ConfigMap.
- Cleanup.
Start a Kubernetes cluster using
minikube start
.
Background
A ConfigMap is an API object used to store non-confidential data in key-value pairs. A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable.
Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume. This decision should be based on how your application reads its config values.
- ConfigMaps consumed as environment variables are not updated automatically and require a pod restart.
- ConfigMaps consumed as mounted volumes are updated automatically and don't require a pod restart.
Note: A container using a ConfigMap as a subPath volume mount will not receive ConfigMap updates.
Create ConfigMap and Pod using config file
Create the manifest. Save the following YAML file in your directory.
File: 10-k8s-configmap-env-vol.yaml
Apply the configuration using kubectl apply -f YAML_FILE
.
~/learnk8s> kubectl apply -f yaml/10-k8s-configmap-env-vol.yaml
configmap/mycfgmap created
pod/mypod created
Verification
Get a shell to the Pod.
~/learnk8s> kubectl exec -i -t mypod -- /bin/bash
Run the commands as shown below.
root@mypod:/# pwd
/
root@mypod:/# ls
bin boot config dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@mypod:/# echo $ENV
dev
root@mypod:/# cd config/
root@mypod:/config# ls
debug.properties dest.properties
root@mypod:/config# cat dest.properties
Q_INPUT=InputQ
Q_OUTPUT=OutputQ
root@mypod:/config# cat debug.properties
TracerMode=always
LogLevel=INFO
root@mypod:/config# exit
exit
~/learnk8s>
Cleanup
Delete the configuration using kubectl delete -f YAML_FILE
.
~/learnk8s> kubectl delete -f yaml/10-k8s-configmap-env-vol.yaml
configmap "mycfgmap" deleted
pod "mypod" deleted