In this lecture we discuss how to work with configuration data in Kubernetes.
In the previous lecture we saw how to define environment variables in it pod definition file.
When you have a lot of pod definition files it will become difficult to manage the environment data
stored within the query files.
We can take this information out of the pod definition file and manage it centrally using Configuration
Maps. ConfigMaps are used to pass configuration data in the form of key value pairs in Kubernetes.
When it pod is created inject the config map into the pod.
So the key value pairs that are available as environment variables for the application hosted inside
the container in the pod.
There are two phases involved in configuring ConfigMaps.
First create the ConfigMaps and second Inject them into the POD. Just like any other Kubernetes object.
There are two ways of creating a configmap.
The imperative way - without using a ConfigMap definition file and the Declarative way by using a Config
map definition file.
If you do not wish to create a configmap definition, you could simply use the kubectl create
configmap command and specify the required arguments.
Let's take a look at that first with this method.
you can directly specify the key value pairs in the command line. To create a configMap of the given
values, run the kubectl create configmap command.
The command is followed by the config name and the option –from-literal. The from literal option is
used to specify the key value pairs in the command itself.
In this example, we are creating a configmap by the name app-config, with a key value pair APP_COLOR
equals blue.
If you wish to add additional key value pairs simply specify the from literal options multiple times.
However this will get complicated when you have too many configuration items.
Another way to input configuration data is through a file.
Use the from file option to specify a path to the file that contains the required data.
The data from this file is read and stored under the name of the file let us now look at the declarative
approach.
For this we create a definition file just like how we did for the pod.
The file has apiVersion, kind, metadata and instead of spec, here we have “data”.
The apiVersion is v1, kind is ConfigMap. Under metadata specify the name of the config
map.
We will call it app-config. Under data add the configuration data in a key-value format. Run the kubectl
create command and specify the configuration file name. So that creates the app-config config
map with the values we specified.
You can create as many configmaps as you need in the same way for various different purposes.
Here I have one for my application, other for mysql and another one for redis.
So it is important to name the config maps appropriately as you will be using these names later while
associating it with PODs. To view the configmaps, run the kubectl get configmaps command. This lists
the newly created configmap named app-config. The describe configmaps command
List the configuration data as well under the data section now that we have the config map created let
us proceed with step 2 configuring it with a pod here I have a simple pod definition file that runs
my simple web application to inject an environment variable add a new property to the container called
envFrom. The envFrom property is a list, so we can pass as many environment variables as required. Each
item in the list corresponds to a configMap item. Specify the name of the configmap we created earlier.
This is how we inject a specific configmap from the ones we created before. Creating the pod definition
file now creates a web application with a blue background. What we just saw was using configMaps to inject
environment variables.
There are other ways to inject configuration data into pods you can inject it as a single environment
variable or you can inject the whole the data as files in a volume.
We will look at some of these options in the coding exercises that accompany this lecture.
So head over to the coding exercises section and practice viewing configuring and troubleshooting environment
variables on a live Kubernetes environment.