ConfigMaps
Module
ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable.
Overview
At the end of this module, you will :
Learn the format of a YAML ConfigMaps file
Learn how to manage a ConfigMaps
Learn the composition of a ConfigMaps
Prerequisites
Create the directory data/configmap
in your home folder to manage the YAML file needed in this module.
Create
ConfigMap can be created from directories, files, or literal values thanks to the Kubectl client.
Independently from the deployment method, the command line to create a ConfigMaps should look like this :
CONFIGMAP_NAME is the name to assign to the ConfigMap
DATA_SOURCE is the directory, file, or literal value to draw the data from
The data source corresponds to a key-value pair in the ConfigMap, where
key is the file name or the key provided on the command line
value is the file contents or the literal value provided on the command line
The create command can directly ask the API resource to create a Service in command line or create a ConfigMap object based on a yaml file(s) definition.
From literal values
Multiple key-value pairs can be passed to the Kubectl client to create a single ConfigMap. Each pair provided on the command line is represented as a separate entry in the data section of the ConfigMap.
In this particular method, the key / value are explicitly defined in the command line and each key / value will generate a new entry in the ConfigMap.
The command line for this kind of ConfigMaps creation should look like this :
Exercise n°1
Create a ConfigMap named mysimpleconfigmap with those key / value pairs :
course=kubernetes
subject=configmap
subject.title=mysimpleconfigmap
This method is not recommended in production. It is recommended to version the ConfigMaps files in a sources repository to manage it with a CI/CD tool.
From a directory
Like any other object in Kubernetes, multiple ConfigMap can be created based on files stocked in a directory. The Kubectl client take care to parse each file in the directory given to ensure that each content files are integrated as ConfigMaps.
In this particular method, the key of the ConfigMap will be the name of the file and the value will be the entire content of the file.
The command line for this kind of ConfigMaps creation should look like this :
Exercise n°1
Create this file :
~/data/configmaps/directory/configmap.properties
Copy / paste the content below in the new file
Create a ConfigMap based on the directory
From a file
Like any other object in Kubernetes, multiple ConfigMap can be created based on multiple files stocked in a different directories. The Kubectl client take care to parse each file given to the command line to ensure that each content files are integrated as ConfigMaps.
In this particular method, the key of the ConfigMap will be the name of the file and the value will be the entire content of the file. In this method, it is possible to specify a key at the ConfigMap creation to avoid using the file name.
The command line for this kind of ConfigMaps creation should look like this :
Exercise n°1
Create two properties files in this path :
~/data/configmaps/directory/
Put some properties in each one
Create a ConfigMap based on the two property files created
Get
The get command list the object asked. It could be a single object or a list of multiple objects comma separated. This command is useful to get the status of each object. The output can be formatted to only display some information based on some JSON search or external tools like tr
, sort
, uniq
.
The default output display some useful information about each ConfigMaps :
Name : the name of the newly created resource
Data : the count of keys in the ConfigMap
Age : the age since his creation
Exercise n°1
Get a list of ConfigMaps deployed in the default namespace.
Describe
Once an object is running, it is inevitably a need to debug problems or check the configuration deployed.
The describe command display a lot of configuration information about the ConfigMaps (labels, resource requirements, etc.) or any other Kubernetes objects, as well as status information about the ConfigMaps and Pod (state, readiness, restart count, events, etc.).
This command is really useful to introspect and debug an object deployed in a cluster.
Exercise n°1
Describe the ConfigMaps deployed in the default namespace with a file or a directory.
Exercise n°2
Describe the ConfigMaps deployed in the default namespace with litteral values.
Attach
Once a ConfigMap is created, it has to be attach to a pod to be able to read the data within it.
The data can be attach as :
Environment variable : The application in the container has to be able to read data from those environment variables.
File : The application in the container has to be able to read the content in the specific files and find the needed keys to get the right value.
Multiple ConfigMaps can be defined in a Pod, this is useful when the configuration has to be managed by different teams.
In environment variable
There is two method to define the content of a ConfigMap in a Pod :
Define each variable individually
Define each key / value pairs in a ConfigMap as an environment variable automatically
Depending of the ConfigMap content, defining each variable individually can be much complex and more secure than giving access to all the content of a ConfigMap.
Exercise n°1
Based on the previous ConfigMaps created, create a Pod using the busybox image to display some part of the ConfigMap in single environment variables.
Create the Pod to attach the ConfigMap.
Get the logs to ensure the Pods is running and configured.
Exercise n°2
Based on the previous ConfigMaps created, create a Pod using the busybox image to display the entire ConfigMap in environment variables automatically.
Create the Pod to attach the ConfigMap.
Get the logs to ensure the Pods is running and configured.
In file path
With this method, the data of a ConfigMap is directly connected as a Volume to a Pod in a specific path in the container to be readable by the application deployed.
As the previous method, the content of a ConfigMap can entirely be copied in the specific file or only some specific keys can be defined to be available in the specific path.
Be careful in the path used to mount the volumes, if a file already exist, it will be erased to mount the ConfigMap Volume.
Exercise n°1
Based on the previous ConfigMaps created, create a Pod using the busybox image to display some keys of the ConfigMap mounted as Volumes.
Create the Pod to attach the ConfigMap.
Get the logs to ensure the Pods is running and configured.
Exercise n°2
Based on the previous ConfigMaps created, create a Pod using the busybox image to display the entire content of a ConfigMap mounted as Volumes.
Create the Pod to attach the ConfigMap.
Get the logs to ensure the Pods is running and configured.
Explain
Kubernetes come with a lot of documentation about his objects and the available options in each one. Those information can be fin easily in command line or in the official Kubernetes documentation.
The explain command allows to directly ask the API resource via the command line tools to display information about each Kubernetes objects and their architecture.
Exercise n°1
Get the documentation of a specific field of a resource.
Add the --recursive flag to display all of the fields at once without descriptions.
Delete
The delete command delete resources by filenames, stdin, resources and names, or by resources and label selector.
A ConfigMaps can only be deleted when it is not attached to a Pod.
Note that the delete command does NOT do resource version checks, so if someone submits an update to a resource right when you submit a delete, their update will be lost along with the rest of the resource.
Exercise n°1
Delete the previous ConfigMaps created.
Module exercise
The purpose of this section is to manage each steps of the lifecycle of an application to better understand each concepts of the Kubernetes course.
The main objective in this module is to understand how to externalized some configuration part of an application to simplify the development and the lifecycle of both application and configuration.
For more information about the application used all along the course, please refer to the Exercise App > Voting App link in the left panel.
Based on the principles explain in this module, try by your own to handle this steps. The development of a yaml file is recommended.
The file developed has to be stored in this directory : ~/data/votingapp/05_configmaps
Delete the vote Deployment to be able to manage the environment variable with a ConfigMaps
Create a ConfigMaps resource to externalize some part of the vote Pods :
Name the ConfigMaps vote
The ConfigMaps must manage those data :
option_a: "CATS"
option_b: "DOGS"
Update the Deployment of the vote Pods to attach the ConfigMaps as environment variables :
The name of the
option_a
environment variable has to be OPTION_AThe name of the
option_b
environment variable has to be OPTION_B
External documentation
Those documentations can help you to go further in this topic :
Kubernetes official documentation on ConfigMaps
Last updated