Hi, Let us understand what is config map, how to create, and make use of it.
Whenever I am creating a pod there will be a requirement to pass some
configuration informations to the pod how it should function or for the
containers to give some of the environmental variables that needs
to be passed and sometimes it may be confidential as well as non-confidential
informations. For that we do have two type of configuration kinds like
config maps and secret. Config maps mostly it will be used to store and pass
the non-confidential informations in the format of key value pair. Whenever I say
key value pair we can very well relate to the yaml file where the key is going to be
a string and value could be another map or it could be a collection or even it
could be another string. Let's go ahead and create a config map and see how to
make use of it. Once the config map is created the pod can make use of it as
environmental variables or command line arguments or even a configuration file
within a volume. Now let us see different options available and this
configuration map will help to create environmental specific configuration so
that we can pass it to the container and make the container to have different
behavior in different environments, in terms of using different images or
application properties based on the environment. Let us go ahead and create a
config map I do have a yaml file where I have mentioned the kind as config map
and within data I do have key value pair for example player initial lives it is 3
and the value needs to be mentioned as double quotes and that needs to be
handled as the integer when it is getting read within the application. Within
config map it has to be of type string or another map. In this case I do have
game.properties within that I do have two key value pairs where types
as collection of values and the maximum lives as a string with value 5. So I'm going
to use this particular yaml file and create a config map, let me go ahead and
create a config map using the yaml file. I'll use kubectl apply and then the
yaml file name. So the config map got created let me go ahead and get the
config maps available, so I do have the config map my config. I'm going to get
the details about this config map kubectl describe config maps and then the
name of the config map. The name of the config map was available as a part of the
yaml file, here I do have the name my-config. So I'm going to give describe
and then the name of the config map. Here I do have information in terms of
what are all the keys and the values. Now I'm going to get the output in the
yaml format which would be much easier to understand kubectl get the
config maps and the name of the config map -o the yaml format. Here
the tree structure will be very clearly visible. Now I am going to have a
property file and use the property file and create the config map. As the
property files going to have the key value pair, I can use the property file as well so
I do have a property file with key value pairs. I can directly feed in this property
file and create a config map. I'll use kubectl create configmap and then name
of the config file and provide the property file. Now I can get the output
of the config map that got created as a yaml format. So this different ways of
creating the configmap will be useful whenever I am going to migrate the
legacy applications into the kubernetes where I can go ahead and directly use the
property files or the other yaml files and create a config map. Now let us see how
to use the config map as a part of the pod and get those values. For the demo
purpose I am going to create a configmap, let me get the details about the yaml file
here I do have the configmap where I do have the data that is this is the key and
this is the value. So I do have special_level as one key special_type as
another key. Let me go ahead and create a configmap using this specific yaml file
So I'll use kubectl create and then provide the yaml file the configmap got
created. Now I'm going to have the yaml file for the pod, let me get the details
about the pod yaml file. As I mentioned earlier all these yaml files will be added
as a part of the lecture, so that you can download and directly make use of it.
Here I'm going to create a pod with the name dapi-test-pod and here I am
going to pass the config map as the environmental variable. Here this is the
first time I'm introducing a container can get the environmental variables and
make use of it internally. So here I'm going to pass the environmental variable
with the name special_level_key, so what is going to get passed for this particular
environmental variable that's going to get from the config map and it's going to use
this specific key special_level and what config map to read from it's going to read
from special-config configuration map and the same way I'm going to have
another environmental variable as well that's going to have the name as
special_type_key within the container I am going to execute the bash shell and
it is going to echo these two environmental variables. So this pod
when it is getting created it's going to pass the environment variables by
reading the config maps and within the container it is going to print this
environmental variables as a part of the logs and there will not be any daemon it will be
running, so it's going to log and the life cycle of the pod will get over. I can check
the logs of the pod and see whether those values got printed or not. Now let
me go ahead and create the pod, I'm going to use the kubectl create and
provide the yaml file. Before getting into the pod let's go ahead and check the
details about the config maps that got created. So I'm going to get the details
about the special-config that got created using the yaml file and I do have these
data. That is level 4 and power user as a part of special_level and special_type
Now I am going to get the details about the pod that got created, let me get the
list of pods kubectl get pods, so here's where, I do have the pod, let me get the
description about the pod. The pod got completed these are all the
environmental variables that was set, now I can get into the pod and see the
logs. So I can use the command kubectl logs and the name of the pod kubectl
logs and then name of the pod. So here's where the value that got printed using
the echo statement. So in a quick summary we have seen what is config
maps and how to make use of it within the pods and their uses.