Manage Configurable Data In Kubernetes
Join the DZone community and get the full member experience.Join For Free
When doing containerization, often there is a need to have some configurations manageable from outside the container. After a container has booted with certain pre-configured data, it is necessary to have a way by which this data can be modified at runtime, if needed. For instance, we may have different configurations for different deployment environments and we may want to use the correct set of configurations for a target environment without re-creating the container.
ConfigMap to help configure data externally. This follows the design paradigm - "separation of config from code." The ConfigMap API makes the application portable. The configuration can be changed without redeploying the application.
Kubernetes' ConfigMap resource holds data as a key-value pair. It allows us to specify configuration data outside of the container. This decouples the image and its configuration. Any changes to a configMap at runtime also reflects within the running container. Hence, configMap is a very useful tool in several use cases, such as:
- Setting environment variables
- Setting command-line arguments in a pod
- Reading config files from a Volume
Each of these use cases is explained further in this post.
First, let's take a look at a sample ConfigMap.
In the above example, the field data holds the configuration data. As we can see, it can hold individual properties as well as content from an external file.
Create a ConfigMap
Let us now start by creating a configMap. This can be done by using the command below:
where the name is the name of configMap and the source is a literal value or a file containing the data.
To populate the configMap from literal values, see the command:
If we have a directory containing multiple files then use the following command:
To see details of an already created configMap, use the following command:
Using ConfigMap in a Pod
There are several use cases where a configMap is used inside a Pod specification.
For Environment Variables
If we need to set the value of an environment variable declared in a Pod specification using configMap then we can follow below steps.
First, create a configMap:
Now create a pod specification (mypod.yaml):
Now create the pod from the above specification.
Now to verify if the environment variables have been created.
It is not necessary to set the config data from one configMap only. We can specify multiple configMaps and use them to set different environment variables.
Let’s say we have two configMaps, my-configmap1 and my-configmap2.
We can then use them as follows in a pod:
For Command-Line Arguments to A Pod
Pod specification has a section of command. We can set this command from a configMap thus making the command dynamic. See the below pod specification:
KEY holds the value of property.1 defined in a configMap my-configMap.
If we are using a Kubernetes volume for the pod and then we can populate it using files declared in a configMap. See the pod specification below:
In the above example, my-configmap data will be added to the volume mentioned in volumeMounts.mountPath.
We discussed in this post how we can use Kubernetes resource configMap to set the configuration data of the pods dynamically thus decoupling config from code.
We explained how to use configMap through actual code snippets which can be used readily in your project.
Opinions expressed by DZone contributors are their own.