How to use/pull environment variables into Helm Chart

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is rahul and in today's session we are gonna see how to pass environment variables into the helm chart so the question comes why do we need to pass the environment variables into the home chart as you know whenever you create a helm chart so you get values.yml along with it so that is your template where you specify the values so consider an example you have created a hello world helm chart where you have your values.yml and inside that values.yml you have a replica count equals one let's switch over to terminal and see what i mean by the values.yml and replica count one so i have created a hello world helm chart already so this is my hello world helm chart if i switch into that directory and here you will see this is my values.yml so whenever you create a very basic helm chart then by default values.ml comes with it and if you see the content of values.ml then you will see the replica account the replica account is always one by default so whenever you are going to install this helm chart so the there will be only one replica which is being created for your hello world let's do one thing let's install this hello world helen chart where we have by default replica account that is one so it is going to create only one replica inside our kubernetes cluster so to install the sound chart what we need to do is we need to get one directory up and you can see this is my hello world helm chart i'll clear the screen and this is our hello world directory for hello world helm chat actually and the command for installation is helm install followed by your release name release can name can be anything of your choice so i'm gonna put hello world it should be like hello world one and after that you need to put actual helm chart name so that is hello world okay and i'm gonna install it okay now the installation has been done let's go and verify the replica so for that you need to run the command cube ctl get deployment and here you can see our hello world helen chart has been installed and the replica count is one that's why we have only one replica running inside your kubernetes cluster okay coming back to our slide where we have seen the values.yml where we have defined the replica count one and we have installed the helm chart but the next question is can't we just overwrite this value using command line interface so what does it mean ah do we always need to go and update the values.yml to increase the replica count okay let's consider in a scenario you are in a production environment where you have already hello world health chart running hello world is just an example i am just keeping it for simplicity so you are running this hello world exam helm chart inside your production cluster where the replica count is one but somehow you need to increase the replica account to two or three okay so are you going to go into the values.yml are you gonna update the values.yml so there is an easy way where you can just use the command line interface and you can just increase the replica account by issuing the command and the command for that is and here you can see the command is helm install it's the same command which we have used but there are few parameter which we are going to add so the parameter are help install followed by the keyword set then you need to specify the replica account and the number so i'm going to put the replica count to 2 and after that you need to specify the release name okay so again you can keep any release name of your choice and followed by your actual hello world helm chart okay let's go back to terminal and see how we can do that but first of all we need to delete this release because we are going to again use the same release name so the command for deletion is helm delete followed by your release name so i'm just gonna copy and paste it over here so it's going to delete the release name okay and also one more thing this is the guide which i have prepared for you so you can come back and copy the commands from here also so you don't need to remember any of the command so i will put the link of this guide into the description section so you can follow this guide anytime whenever you feel comfortable okay so this is the command helm install set replica account 2 release name and the actual hello world name here the name is different but we can change it okay so go back going back to terminal i'm gonna paste the command over here okay sorry so the command is incorrect because the name is world over here so i need to correct the name here is hello world okay and just hit the enter so here you can see our command has been executed successfully and we can verify the deployment cube city you'll get deployment so here you can see the replica count is two out of which one is ready but second is about to get up so if we rerun the command get deployments hopefully it should be up and running okay it's taking time so it should be up in some time okay so here you can see uh now we can run the two replica set or to do replica count of hello world release so this is the way you can use the command line interface to increase the replica account using the cli which is provided by helm chart so what benefit you are getting over here is you don't need to go and update the values.yml manually you can just do it by using command line interface so that's the benefit you will get if you are running uh this kind of uh release on your production environment so you can just use the command line interface and increase the replica count inside your kubernetes cluster using the helm chart all right moving further is there a way we can overwrite the values inside our command so the answer is yes you can overwrite the values and here you can see in this example we are using the same command helm install we are using the same variable replica count initially we are setting the value to 2 and after that we are again setting it the value to the 3. so here we are trying to override the values which we have assigned previously from 2 okay so that is also possible but this feature is like very less often used because it will create lot of complexity if you keep on using quite a lot in your production or staging environment but just for example purpose we are going to run this command and see is it really going to override this value or not okay first of all let's delete the previous release so the command is helm delete followed by the release name so hello world one and i'm gonna delete the release all right now it has been deleted i'm just gonna copy the command from the guide and the command is this and i'm just going to head back to terminal and paste it over here so here you can see uh we are increasing the replica count from two okay let's run the coupe ctl get deployment command to verify the replica account so the command is cube studio get deployment and here you can see there are three replica set which is running inside our kubernetes cluster okay before we move ahead let's delete this release so the command is okay so now we have seen like how we can override the values and how we can increase the replica count of our hello world help chart okay moving further is there a way we can create my own values dot yml so this is my own values yml uh where i can define my own variable so i'm not gonna use the values.yml which is provided by helm chart but instead i'm gonna create this new file and i can keep any name of my choice so just for the example purpose i am keeping the myvalues.yml and i'm gonna define the replica account to two so in this way you can define your own values.yml and you can keep your own variable name with their values so this feature is also provided by our helm chart okay let's head back to our guide and here you can see this is the myvalues.yml which we need to create so i'm just gonna copy this command and it's basically doing just the vi actually so i'm just gonna clear it and i'm just gonna create the file vi values.yml and i'm gonna paste the count that is replica account two and i'm just gonna save and quit so here you can see i have created my values.ml along with my hello world help chat okay let's head back to our guide and this is the command which we are going to run so the command is helm install followed by the flag that is f and after that you need to specify your myvalues.yml followed by your release name that is hello world release and your actual health chart that is hello world so i'm just gonna copy this command and paste it into my terminal so here you can see the command so command is help install followed by the keyword f that is i think force and that is your values.yml that is myvalues.yml which you have created recently then again you are going to specify your release name and followed by your actual hello world helm chart and then just gonna hit enter and as it has installed successfully so again we are gonna verify it cube ctl get deployment so here you can see there are two replica set out of which none has started so it should start in a couple of seconds let's rerun the command again and here you can see the replica count is 2. so this is the way you can create your own myvalues.yml you can specify the variable or the replica count values inside it and then you can just apply this command using the f flag and the supplying the myvalues.yml file name and the file name can be different in your case so just supply that file name and your release name and then followed by your help chart okay so before moving further let's delete this release have delete so till now we have seen three ways to define your environment variables and pass into our helm chart but is there any other way by which you can pass key value pair so yes there is a one more way it is little bit tricky i would say because you will have a multiple values inside your key value pair and here you can see this is our example map where we are passing username as well as value so here in this example map i am passing username the value is test1 and the password the value is test2 so this is this kind of a key pair i am trying to pass it over here okay so let's head back to our guide and see how we can achieve that so okay so for that we are going to create one more file that is environment values uh this is a new file apart from the my values file which we have created in the previous step number third so first of all just copy this command and create this environment values where we are going to define this key value pair i'll clear the screen i'll paste the command so now we are inside the environment values.yml so the file is empty but i'm just gonna copy this content over here i'm just gonna save and quit so now we can see we have environment values.yml also okay now head back to our guide and how we can use that so to use those variable you need to iterate it and how we are going to iterate we are going to iterate using this configuration actually so you need to define environment variable after that after that you need to define the range and then in the range you need to define the values of your key value map okay so the key value map is example map so here you can see if you check the name the name is example map so we are just going to use the same name over here and after that we are just gonna iterate with this range okay so the range means uh how many variables inside that map so here we are having two variables so in that range we are going to iterate those values okay so for that what we need we need to edit our deployment.yml because we are going to use this script or not it's just not a script but it's just a vml configuration but we're just gonna use this yml configuration inside our deployment.yml okay okay so i'm just gonna copy this configuration from here and head back to my terminal and i'm gonna open the deployment.yml in a vi mode so that we can edit it so i will go to the directory template and then i'm gonna open deployment.yml okay so here we need to look for the line that is image pull policy over here and here i'm gonna insert that configuration so i'm just gonna go into the edit mode but i think this configuration you can paste it anywhere in the yml but you need to look for the syntax otherwise it might throw some error because you might have inserted at the wrong place so be careful about that and here i'm gonna just paste the content which i have copied so here we need to correct one thing this environment the spacing actually otherwise it might throw some error while we render this template ok that looks perfect i am just going to save and quit okay so now we have edited our deployment.yml and we have inserted this placeholder for the range and the key value map which we want to print over here okay so now how we can see the content so for that we have a command helm template f then the yml name that is environment values.yml where we have stored this values uh i have shown over here this example map so in that file we have stored those values and after that the hello world our actual help chart name one thing you might be wondering why i am using the helm template command over here instead of help install so the reason for that i am using helm template is we want to see the actual values once the template is generated if i directly run the helm install command then you won't be able to see the values which are like printed in this configuration okay that's why i am using the helm template command and if you do not know what is helm template command then i have a separate session on this helm series which i have posted earlier so you can go back and check the details about the helm template command okay so i'm just gonna copy this command and i'll clear the screen and i'll paste it over here so as soon as you paste this command then you can see the complete yml which is generated so here you will see service yml with the actual values which is gonna deployed inside your kubernetes cluster so here you can see all the details but we are interested in deployment.yml because we have edited deployment.yml and we have inserted that configuration this one on the right hand side if you can see this one so just go back uh below and here if you remember i have mentioned the image pull policy tag inside the yml so this is my image policy tag and after that we have inserted our scripts for environment and the example map so here is the values which is like iterated and printed in the deployment dot firmware so this is how you uh process the environment variable if you have a multiple key value pair so this is how you gonna store it inside your external file so that external file name is environment values.yml but you can keep anything of your choice and after that you should test it with the helm template command and after that it should be okay to just run the install command i hope you like the today's session on how to pass environment variables into the helm chart and if you have any question related to the today's session then please put down into the comment section i'll try to get back to you if you're new to the channel then you can find me on youtube with the name rahul walk or either j hook and on this channel you will find the lab session similar lab session on devops kubernetes helm chart and soon there will be terraform also coming so stay tuned and also if you are interested in the guides which i keep on preparing for the lab session then you can find me on the web with the name so here you will find all the guides which i have prepared for all the lab sessions which i kept which i usually take on the youtube so you can find all those guides over here so you don't need to remember any of the commands so you can just come back here and you can just feel free to use those commands so stay tuned and see you into the next session bye
Info
Channel: Rahul Wagh
Views: 2,269
Rating: 4.8518519 out of 5
Keywords: devops, helm chart, pull environment variables into helm chart, variables, environment variables, kubernetes
Id: dKKMJseMiGU
Channel Id: undefined
Length: 17min 53sec (1073 seconds)
Published: Wed Mar 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.