ConfigMaps Part 2
ConfigMaps are used to store non-sensitive application configuration parameters. Think of config files managed by Kubernetes and you are already close.
Creating a ConfigMap
There are multiple ways of creating a ConfigMap. They can be created from files, directories or by providing values directly using the command line .
Although ConfigMaps are key value pairs, this does not mean that values have to be very short. In fact, a value can contain content up to 1 megabyte of non-binary UTF-8 text.
Creating a ConfigMap from a Configuration File
20-config-file.conf contains an imaginary configuration file:
# This is an exemplary config file
number-of-requests = 20
very-import-switch = true
To import the config file into a ConfigMap execute the following command:
kubectl create configmap config-example-1 --from-file=20-config-file.conf
To verify that the ConfigMap has been created successfully:
kubectl get configmaps config-example-1 -o yaml
-o yaml switch has the effect that not only the ConfigMap is listed but also its content shown. You should see a section
data contain the content of the imported configuration file.
Creating a ConfigMap from Command Line Literals
It is also possible to specify ConfigMaps using command line literals:
kubectl create configmap config-example-2 --from-literal=number-of-requests=20 --from-literal=very-important-switch=true
See yourself how the key values pairs have been joined into a ConfigMap:
kubectl get configmaps config-example-2 -o yaml
Deleting a ConfigMap
Deleting a ConfigMap is simple:
kubectl delete configmap config-example-1
kubectl delete configmap config-example-2
Once a ConfigMap is created Kubernetes offers several access mechanisms to applications.
- Env Variable
In order to grant an application access to a ConfigMap it must be told which ConfigMap to access and which mechanism is to be used. This is done in the Pod spec:
See YAML file:
- image: busybox
- name: NUMBER_OF_REQUESTS
- name: VERY_IMPORTANT_SWITCH
Apply the YAML file:
kubectl apply -f 40-pod-with-config-map-env.yml
Verify whether the Pod has been created successfully:
kubectl get pod busybox-config
You should see an error with the
STATUS field indicating
CreateContainerConfigError. This gives you the opportunity to think about how a failing Pod can be investigated. Maybe it's worth having a look at the Pod's log output:
kubectl logs busybox-config
But the output is disappointing:
error: the server doesn't have a resource type "logs"
Why are there no logs?
The answer is because there is no Pod that could produce logs as the Pod creation failed. At this stage the
kubectl log command is not helpful.
As a general utility to investigate Kubernetes objects - not only Pod objects - the
kubectl describe command is very handy:
kubectl describe Pod busybox-config
At the end of the output a tabular paragraph
Events tells us about the lifecycle events of our
Error: configmap "config-example-2" not found
Try to fix the above ConfigMap not found issue.
If you have fixed the issue, the container named
busybox-config should have been started successfully.
kubectl logs busybox-config
Should return the environment variables seen by the container inside the
busybox-config Pod. The listing should include:
Also have a look at all the other environment variables added by Kubernetes:
It's worth remembering that Kubernetes provides you with context information of the execution environment.
- The Twelve Factor App, https://12factor.net/
- Environment Variables, Wikipedia, https://en.wikipedia.org/wiki/Environment_variable
- Kubernetes Documentation, Tasks, Configure a Pod to Use a ConfigMap, https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/