hello everyone welcome back to my channel my name is push and this is video number 19 in the series CK 2024 and in this video we'll be looking into config map and secret so something that we would have covered in one of the previous videos but somehow I missed it so I thought of covering it now so we'll be looking into the concept and we'll be doing some demo and I will have a task for you in the GI up repository of day 19 folder uh you can complete that as well for the Hands-On practice and uh the comments like Target of this video is 150 comments 150 likes pretty small easily doable and I'm sure you can do that in the next 24 hours so please try to complete that and I will see you soon with the next video after that so yeah without wasting any time further let's start with this video okay so if you remember in the day 11 video where we have looked into multicontainer concept and we also looked into commands and arguments and environment variables right so this is how we used environment variable earlier so we actually pass the environment variable within key value pair in the Pod yaml itself it's okay if I mean if we do that like this but when these environment variable grows and let's say we have some repeatable task like this particular environment variable I'm using in this part and 10 other parts then it's not really a good practice to keep everything in 10 different yaml right and the values could differ from each other but we want to have a same value for The Identical environment value variables right so for that what we do as part of a best practice we keep everything aside of the container we take this outside and we create a config map object and we actually place this key value pair inside that config map and then we inject that value inside this board right so this is how we do so let me just copy it for now okay and I'm going to go to day9 folder I have a part. yaml paste it and and I'm just going to remove some other fields such as init containers I don't need init containers for this demo so I'm going to remove it okay and okay so now what it has it has a busy box image and I'm just uh doing an EnV I'm just using an environment variable with name and value and then there's there'll be a command running okay and the command will be printing the app is running and then it will sleep for 3600 seconds okay pretty simple pretty straightforward nothing fancy about it so if we do that if we apply this yl K apply okay first I have to go to day 19 folder okay and GTL hyph F apply P. L the Pod should be created okay I'm going to clean up the previous pods I don't need those anymore and then three okay been deleted now if we do K get pods uh the Pod is running that one is still terminating so that's okay it will take its own time not worried about that I'm going to exec into this pod exec and then hyphen hyphen sh and I'm going to print the environment variable or I can just do an echo on the environment variable that we had it was first name and it is printing the value of the environment variable now let's try to do that with a different way by creating the config map out of it okay so for that uh let's remove the environment variable from here okay and let's create a config map so creating the config map we have different ways like imperative and declarative ways so let's have a look at all those things so first we'll try to do with the imperative way so we'll be using the command K create CM or config map it's CMS short for config map and then give it a name so let's give it uh app Hy CM okay and use this syntax from literal equal to and then name and the value so first name equal to Value first name equal to P so this is the syntax if we have multiple environment variables or uh you know U multiple data in the config map you can separate it with this backs slash so that it will take the next line as well and use the same syntax again from literal equal to last name equal to Value okay so from literal equal to name equal to value key value pair okay and hit enter now our config map is created so if we do K get CM uh here is our config map you can do a describe on that describe cm/ app cm and it will show you four different fields uh name Nam space is part of it we have name labels annotations namespace and then data okay in data we have uh these values last name is sucha first name is BU okay so key colon value key colon value okay that's what we see when we describe the config map now we have created the config map now the next part is to inject that config map in the podl so how we can do that you can check the syntax as well okay let's go over here config map there are different ways by which we can do that so first is you create the config map and then you inject it like this environment name value from and this so if you're using this method you have to specify uh the environment variable and this block for every single variable right so that's one way of doing it the next way is using config map as the file from Port so you mount that as a volume okay so this is one of the best practice to mount config map or Secrets as the volume actually we don't use config map to Mount as a volume but we do that with the secrets we have not covered volumes yet so that's why I'm not covering this for now it will just confuse you I don't want to do that but uh what it will do basically it will create a volume okay volume is nothing but a storage attached to your container okay on a particular path and then you can consume the environment variable from that path right we'll cover that once we do the storage section okay and the next is um so this is one way like what I've told you like we can configure it with one single config map uh one single environment variable and you can do that for entire file but let's see uh what how we can do that with this method right so I'm just going to copy this field over here and let's go back and open over here it's supposed to be inside container let's make double show yeah inside the container uh at the same level of name image and other container properties so over here right so we have config map username so username is first name so remember when we created the data inside config map the variable name that we used was in small letters now we are using capital letters because those are different now we are just what we are doing is we are creating the name this is the actual name this is the name of actually the config map okay so config map name was app hyph cm and username was first name and over here is our first name reference so this reference is from uh is for the container and this reference is from the config map right so let's let's have a look at this again so we have EnV block inside that we have the variable for the container so inside container this will be the variable okay and then we are taking the value from config map key reference okay from where we are getting the value we are getting the value from config map key reference and the config map name is this and the key is this okay so there is a key inside the config map from where we are getting the value here is the name of config map here is the value of the config map okay so this is how we will inject the config map inside the container so I'm just going to save this file and let's apply this again K apply hyphen fb.
yl and let's do a force on this okay it's kind of getting stuck so let me just delete the Pod and recreate it again it will be much easier it was actually doing it thing but I deleted it before that uh Delete pod okay so now let's apply this file again created K get pods it's running so let me do exec into the Pod and now if we do an echo on first name now I'm getting the value because we have injected the config map inside the container so if we do an exit and let's do a describe on this pod okay go up uh in the environment variable section okay here environment first name okay and it says set the key to the first name of the config map app CM so it is giving you the reference set to the key first name of the config map app CM so inside app CM first name value will be substituted over here in this local environment variable right so that's uh what it says and um yeah that's that's about the config map so that's one way of creating the config map other way is so instead of like the way we did that over here the command that we used to create config map was from literal and then um key value pair and all those things but if it grows too large so let's say there are hundreds of key value pair it is not advisable to do that line by line so what we can do is we can give the reference of a file so we place all these key value pair inside a file okay and then we give the reference from here and we call it instead of from literal we call it from file and let's say app. config or whatever the name of the file it is right and it will create the config map for you and same way you can inject the config map inside the PML now there is one thing uh in the exam they might ask you to update the values of the config map or change it so you cannot really change the value unless you force it so you have to use the hyphen iph force or you have to delete the Pod and create it again like the way we did okay so that that was the imperative way let's see how we can create the config map from declarative way so first is what we can do we can uh use the same command word okay from literal this command then do a dry run client okay and hyphen o yaml let's keep it in a file the output of this in a file so we call it cm.