Create Kubernetes Helm Chart for your application | Create Helm Chart from scratch for your apps

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my name is vivek i am a software engineer i make videos about kubernetes containers co-as a programming language and software engineering in general in this particular video we are going to look into how we can create a helm chart for your application that you have already developed so in some of the in some of the previous videos we just went ahead and created an application then we created manifests kubernetes resource manifests so that we can deploy that application on top of kubernetes in this particular video we are going to look into how we can actually help me fi or how we can create him chart for the application that we have already created so if you are not very familiar about these drums for example hem chart repo and release i have also made a video about uh basic understanding or basic building blocks of film so maybe you would love you would like to check that out so uh this is the project structure that we have as of now uh and this is the project that that we have created and here is this uh this is this is pushed in my github repo already if you want to uh check that out right so this is the project structure we have manifests and this application consists of two components uh one is rest api and this another one is the mysql database that we are going to run right so this is the application this is the project that we are going to uh create a helm chart out of right so as we discussed in in uh the last video in the helm chart we basically try to specify all the manifests that should be deployed as part of the application deployment right so now what we have to do is we have to somehow use all these manifests that we have here and package them into into a helm chart right but before that we should first uh let's just go ahead and try to create a dummy helm chart right and then in that hem chart we are actually going to copy all these manifest files uh again in in one of the previous videos i actually created a dummy helm chart repo and help chart application and then i i actually explained everything or every directory in that in that hem chat application so you should check that out let me just quickly open this on on visual studio code and now as you can see we have our docker file here we have code and we have all the manifest in in the manifest directory and this is the application that we are going to convert into uh uh help help chart right so let me just go ahead and try to create a what should i say helm chart very basic help chart that is going to have some some some test applications some test deployments inside it and i'm going to name it help because this is what people usually do for example let's say your application is library and you want to create helm chart for your application in the same repo you are going to have in the same repo you are going to have a directory named help and everything related to help is going to get inside that particular directory so i'm just going to go ahead and create a branch right and check out to that branch right and once we are inside once we are inside this particular branch let's just run him create and this is going to create a dummy helm application or him chart and we are going to name it hell so this is the name of the uh directory would we can say right so if i just go ahead and try to hit enter we can see that a directory has been a directory has been created named hill and this has some dummy details for example uh deployment deployment files in this files and and some other things right so let's just go ahead and first of all try to get rid of the the yaml files that we don't want right deployment.yaml service.ml and service account.yaml so now we are just left with very skeleton code i would say so we have already discussed about these files that we see here so now what we are what we are going to do is we are going to copy uh every manifest that we have in the manifests right inside the templates directory because because uh if you are creating helm application for for your application if you are creating a help chart for your application templates is a directory uh where everything is going to going to be right so every manifest should be copied inside templates directory like we saw we we had some files for example uh service account service and increases that we just have deleted right so yeah these are all the files these are all the files that that should be there right uh in inside this templates directory okay so let's just go ahead and try to try to do that so copy from uh manifests and but before that if we have a look into the the manifest files that we have we can see that we have some some duplicate files right for example secret.yaml and service.aml ns.aml and things like that so let's just go ahead and try to rename that so this is going to be nsiphonemysql.aml right and then this is going to be secret hyphen mysql.yaml and this is going to be service hyphen mysql.yaml right similarly we are going to change it to ns hyphen rest api.yaml and this secret is going to be secret hyphen rest api similarly this service is going to be renamed to to rest api dot ammo right so now that we have all the yaml files all the resource manifests let's just go ahead and try to copy copy them inside the templates directory that we just have created so cp manifests mysql uh to help and templates right and we should be able to see things uh inside templates directory so we are able to say deployment ns mysql pvc secret mysql so all the mysql files has been have been transferred inside templates directory similarly we are going to copy over the things inside rest api templates right and and here we go so we have a very simple help help chart we can say right and this help chart had some manifests inside this template directory right and this is how or this is the structure uh off of a helm chart of a helm application now if i just go ahead and try to try to install this so this is the directory where our helm application is present locally right so what we are going to do it do is help install another thing that i would like to emphasize is i'm using helm version 3 as of now so yeah i mean there are chances if you are using help too there are chances that the command formats are going to be slightly uh different right so he'll install and then name of the name of the application or we also call it release name right so release name name is going to be let's say lib and we are going to install it in the namespace library let's say and now we are going to specify the help chart right and health chart is inside help directory and that this is this is almost it right uh that we want to do to install a help chart that is present locally right and if i if i press enter right now uh as as we have discussed in in previous video as well all the manifests that we have inside templates all these manifests are going to be uh going to be what should i say they are going to going to go through a rendering engine we can say right and then they would actually be applied into into your target kubernetes cluster right so every every resource manifest that is there inside inside uh templates directory but before uh doing that what i want to show you is if i just uh press enter if i just type enter here right we can actually see the rendered version of the manifests right uh and this would make a much more sense in in a bit but there are chances that for example for this pvc we are using something from values.yaml right and after rendering how this pvc is going to look like can be figured out by by applying this trident flag while installing uh the chart right so let's just go ahead and try to hit enter and i should be able to see every resource manifest that would be applied uh in in the kubernetes cluster right and this is this is how these are all the manifest that are going to be that are going to be applied right so for example pod namespace and every other thing right now now that we have a very simple not very ideal health chart let's just try to go ahead and and let's just uh try to install it and then we will actually look into the problem that we are going to face then we are going to resolve it right so what i'm going to do is i'm going to remove the dry run and then i'm going to to hit enter right another thing that i would like to specify is we are actually trying to install two applications right we have two applications one is my sequel and another one is rest api right people don't usually do this they try to create two separate help charts for two applications right but just to just to give you an understanding of the things i'm doing things this way but yeah people usually create two charts or or n number of charts for an and different applications and it says namespaces library not found so library right and if i just go ahead and try to install it once again we should be able to see the output that is there in the node start nodes dot text and again we have discussed it about about it in in the previous videos as well so nodes.txt is the file uh that is going to be shown once the chart is successfully installed right and once the chart is installed to to see all the charts that are installed we can see hill helm list and if you know the name space you can specify the name space using hyphen n right or you can just specify hyphen hyphen all name spaces okay and we can see that lib is a release name that is installed in in library namespace right now to look into uh the resources that that that this particular chart has been created since in this particular case we know that uh it is going to create two namespaces database and and rest api and we can see that these two namespaces has been have been created successfully and we can also go ahead and try to check the the namespaces here so this is for a rest api and there should be another there should be another namespace file for database namespace right so we can see that these two namespaces have been created or in other words we can also say that all the all the resource manifests that were there in the manifest directory have been applied obviously after going through after going through the rendering engine let's say they have been applied to your kubernetes cluster now let's just go ahead and try to look into uh everything from database we because first of all let's just make sure that database is running up and kind and we can see that this particular part is in pending state and i know the reason uh maybe i can i'll make another video about how to debug these things in your cluster but if i just go ahead and try to look into the pvc for this particular cluster we can see that this particular pvc is in pending state right and and the reason is standard storage class is not found so if we just go ahead and try to look into this pvc we can see that we have hard coded the storage class right so again this is a very simple example or or one example of how helm can help us to to to enable the users provide the values in the run runtime or while installing the application so what i'm going to do is i'm going to get this value from values.yaml so that if someone is trying to install my application they can provide or they can specify the storage class name while installing the applications so let me just go ahead and try to open the values.yaml file right let's not worry about the other things that are that are mentioned here so i'm what what i'm going to do is i'm going to specify again this is uh this is a yaml formatted file so what i'm going to do is database dot and then for database volume we want to specify storage class name right and by default let's make it standard right and now if i read this stories class value from values.yaml user would be able to specify a dynamic value for this particular field while installing this this chart right so let me just go ahead and try to read this value and again uh to read these values helm provides some predefined objects for example dot values dot chart dot release and obviously some other named templates that we are going to look into uh so what we can do is we can just go ahead and try to read that value so dot values and then database right and volume dot storage class here we go storage class name yeah so now if i if i try to install the application once again right and we specify the value for this particular field database dot volume dot storage class name uh to let's say something else that particular storage class would be applied uh to to this particular field in this particular pvc right so let me just go ahead and try to try to delete the application that we have just installed right while it's getting deleted what i'm what i will be uh doing is we can also before actually installing the application we are actually going to look into the manifest rendered manifests using hyphen iphone driver and driven flag right just to make sure that storage class has been replaced with the storage plastic class that we are that we are providing so okay delete ns library and if i just go ahead and try to run the same command helm install right and now i'm going to specify hyphen iphone set and using hyphen iphone set what i'm going to say is database dot volume dot storage class name and send the storage class name to the storage class name that is present in the cluster right so we have this particular storage class right and we actually want to look into look into the the manifests rendered manifests right so and let's redirect this uh output into a file let's say temp dot yammer so that navigating through that manifest file would be easier right and if i open tim dot yaml now and if we search for this particular pvc right so mysql pv claim and here we go we can see that rendered manifest now has storage class name set to do blog stories right so this is a very simple example i would say of specifying the things but but uh this is how help helps helps us right to achieve the things to specify the things at the run time right now if i just go ahead and try to install the thing so help install and this time i actually want to install things if i press enter now we should be able to see again let me discuss about this in a bit we should not actually create this particular namespace manually usually what people do is again because one particular application is packaged in one chart that application is usually deployed on the name space where the chart is being deployed right so for example since we are deploying it in the library namespace the application that we are deploying through this particular help chart should be deployed in library namespace but but since we have two different applications we are explicitly creating those two namespaces separately and then deploying those applications into respective names namespaces so now if i just go ahead and try to install this we should be able to see uh after this everything running fine in in both the namespaces so we can see that things have been deployed don't worry about this as of now because we have not changed the nodes.txt file if i get everything from let's say database namespace we can see that now at least my sql port is is being created right so and if we look into the pvc form database namespace this is this is now bound right now let me explain how how this actually helps us so when we when we created manifests for our application we hard coded the pvc to standard right and even if we let's say we are trying to install it on digitalocean cluster we know that the storage class that is available here is do block storage if we hard code that particular value at this particular line what if someone tries to install it on a cluster where we don't actually have do block storage storage class available right so for example let's say they are trying to install it on either eks or ada or gke or or any other cluster self-managed cluster right so using values.yaml helped us in in a way that they can specify the storage class dynamically right while installing this application so for example if i am if i am on a kubernetes cluster that has a storage class named let's say gp2 right so what i would say is helm install this particular application hyphen iphone said storage class equals to gp2 right and in that case value of this particular field is going to be changed to gp2 right so this is how helm helps us to to distribute our application right so again this is very very simple example uh like we read values here we can and here we go the part is up and running successfully so the other thing that i wanted to discuss about is like we read values here we can read other things as well for example dot chart dot excuse me dot release [Music] and and dot dot values as well right uh so the other thing that i would like to show is we can actually there are a lot of other things that helm helps us with for example let's say uh if you have to change this particular value right to upper case there are let's say uh this value of this particular field should be in upper cases right so what you can do is uh you can use some predefined uh template functions that help provides us with right and then you can use those functions to actually apply into another functions output so for example we we call this as a pipeline so for example let's say if we have to uh quote it right quote the value of storage class name what we can do is we can just specify a code here right and this is uh this is pipe operator i would say that is going to apply this particular operation in this in this particular output right so these are some other things uh very very what should i say helpful and important thing that that help provides us with now let's take another example right so we also uh create secrets right so the mysql instance that is installed here so this particular this mysql instance right now has the password that we have specified over here right and this password is is a base64 encoded so if we try to decode that this is the password right and using this password we can actually log into the mysql instance that we are running so mysql and the password is going to be this yeah and and you can see that we are able to login to log into the mysql instance now the problem is what if the user wants to specify their own password right because in this particular approach we are we are almost saying that you cannot change the password you will have to use this this particular password right so there is another thing that we can change about this particular chart so let's just go ahead and try to take the value for database root password right so we are going to say database dot root password and let's say default value is the wake right and let's read that value over here and one thing that i would i would like to highlight is you can see that this particular value is base64 encoded of your password right so if i mention dot values dot database dot root password this is going to fail right and then we are going to look into this uh why so let's just go ahead and try to apply this once again so first delete the helm release that you have installed and then we are going to reinstall it so yeah we are going to specify stories class do you blog stories and hyphen hyphen said database dot root password equals to let's say something else right and i run so that we can actually see what is being rendered if i hit enter here this shouldn't work so database dot root password okay so let's just go ahead and try to look into the secret that has that has been generated and i'm also going to explain why i was saying this should not work so name of this secret is my sequel password okay so value has been set to something else right now if i just go ahead and try to install it this is not going to work uh because let's just look into the error and here we go right it is just saying that illegal base64 data right for this particular secret right and we we we know the root cause right because this is expected as as base64 b64 stream right so now what we are going to do is since getting base64 password from user is no it not going to be a good idea we are going to convert that password provided password into base64 using help functions right so if we look into the helm template functions and if we look into encoding functions i am going to specify or or provide this link in the description if you want to so base 64 encoding and base 64 decoding or the function are the functions so if i just go ahead and specify base64 encode right and then we also want to and and this is pretty much pretty much it i think so we use pipeline here and and we used base64 encoding function to actually encode the value that has been provided by the user now if i just go ahead and try to install the things now i think things should work as expected right so let's just let's just delete these two databases that our hem chart creates and then we are going to install uh install our helm chart again okay i think we installed that in default namespace so all right now if we try to install this this should be installed in default namespace right and the mysql instance that is going to be to be get deployed should have this particular root password right so a get part from database namespace right and let's just wait for this particular pod to be up and running meanwhile what i would like to discuss is uh all the values that you that we are specifying here right using hyphen iphone set flag these values can also be specified by creating a values.yaml file or by creating a file locally so for example let's say you you want to specify some custom values for these particular fields you can create a file locally right and then you can specify these fields in the similar format right and then you can you can specify that file using hyphen f flag to hyphen to help install command right and here we go it is running if i exit into this particular pod and we should be able to login to mysql by providing the password that we have here right and then here here we go right so we are we are into my sql i'm exiting it and we are so this is this is what uh i i wanted to discuss about right so now we have since we have two secrets being created we can read uh the same thing into into another secret as well so for example this should go into this particular file as well okay and yeah so this is how you can use values.yaml to read the values and all those values can be specified while installing your application right and if i want to show you how you can specify custom values right uh using using file locally not and not using iphone f1 set plug you can pretty easily do that so let me first delete this from default namespace and we are going to delete the name the the okay it has been installed now let's just create another another file so let's say customvalues.yaml right and in custom values.yaml we are going to specify just the things that we want to change and this is going to be do hyphen block hyphen storage and root password is going to be secret password and to install what we can save help install and we don't have to specify hyphen iphone set we can we can we can give the the what should i say custom values file that we have right and this should install the applications with the values that we have provided locally in in the custom values dot java so i think we should have custom values.yaml somewhere here right [Music] and and here we go right so now the mysql instance that has been deployed has has secret password right so this is another way of specifying the values while installing your helm application so another thing that i would like to discuss about is how you can read the values from using dot chart or our dot release right so what people usually do is uh the the objects that are being created they depend on the release name that user is creating right so for example let's say we are installing two applications mysql address api and for rest api the deployment name should be uh equal to the release that release name that that user has provided so if we take a look into the deployment name as of now the deployment name is rest api right but we want this deployment name to be what what the user has provided uh the the release name to be right so for example if we installed using helm install lib1 right the deployment name should be should be lib1 instead of rest api so let's just go ahead and try to look into how we can how we can achieve that so templates and this is the file that is responsible of creating creating that deployment right so again there are a lot of things that we can we can uh make dynamic here for example images and every every environment variable for for example we can read it from from values dot ammo and every other thing right but what i'm going to do is i'm just going to change the name here right and we can we can obviously read the name from values.yaml here but but i'm going to i'm going to explain another thing and the aspect that is uh that is named templates right in case of helps right so if we have a look into helpers.tpl right so we can we have discussed that every manifest file right inside templates is treated as a kubernetes resource manifest right instead of some files for example nodes dot txt and if and other files that are starting from underscore or that are starting with underscore so these are some specific files uh that are not treated as kubernetes resource manifests but every other file inside templates is treated as kubernetes resource manifest right so if we look into this particular this particular file again let's not look into the other thing that i already mentioned here so let's say we want to we want to get a namespace from from the the release name right so we can create a named template or very simply very naturally we can say that a function right template function and this is how we create a template function so using define right and then let's name it to be let's say deployment name right and then we are going to end this right so hyphen end here we go so now we can say that this is a function and this function can be used from any of the any of the manifests that we have that we have here right so from rest api dot com well what we can do is we can just call that function using include right and we are going to say dev dot name right and we are going to also pass it uh the scope right we are going to discuss about the scope uh let's right now let's say right now so some things uh about this particular function that we have so these names are global right so there are chances that you have sub chart and in your sub chart there are chances that you have you have function names or are named templates with the same name right so there might be there might be conflict to resolve that what people do is we they name their they name their name templates by this uh format so chart and then actual name whatever they want to so since our chart name is help this should be actually library not helm so lib.name right and then in rest api we can call that clip dot name right and if i just go ahead and if i want to just return a string from here we can just written let's say changed name right and if i go ahead and try to see the rendered manifests we should be able to see that the name for this particular deployment is changed name instead of instead of whatever we had earlier right so if i try to install the things once again but dry run [Music] and in custom values okay we don't want to specify anything in custom values and redirect it into temp start yeah well so now we should be able to see that the name for this particular deployment is going to be changed name and we are going to modify it a bit again this print is a a helm template function predefined function like base64 encoding that we have uh seen earlier so if we search for print here we should be able to see all these these are all the string functions that that help provides us with this is pretty useful documentation if you ask me okay so list and let's delete the chart first so what we are going to eventually do is uh we want to have a logic here if values if a deployment name is not specified from values right the deployment name should be the release name right and if a value has been provided from from values.yaml or while installing while installing the application that should be the deployment deployment name otherwise the name of the release so that is the logic that we are going to implement here yeah and temps.yaml if we look for this particular deployment right name of this deployment should be changed name and you can you can see that the name of this this deployment is changed name here right now let's say we want this to be more dynamic and we want to specify or we want to make sure that if a name is specified or if a name is provided for let's say application dot name and by default this is rest api right and if application dot name is provided then deployment name should be changed to that otherwise deployment name should be should be release name right and release name again uh is this name that we are specifying here right so uh to implement that we can have control flows as well so for example we discussed about functions right similarly we can have control loops control flow as well so if statements and we can range over the things as well so if we can take an example here so if values start full override name so if values dot application dot name right if it is provided in that case print this right is else the name should be release name right so release dot name is this correct let me yeah so release dot name should be used okay so does this does this make sense so what we are saying is in rest api dot yaml we are seeing that this template function is going to be used to specify or to provide name for this particular deployment and this rendering happens uh when we try to install it and this all these manifest go through rendering engine or templating engine that we talked about so here if application dot name is not provided right if application.name is not provided in that case the name of the deployment should be release name right otherwise the name of the deployment should be whatever the value has been provided right let's just go ahead and try to try to apply this and see the result and we are not specifying we are not specifying the values dot application name so name of the deployment should be release name right and lib hyphen release iphone name and the temps.yaml that would be generated okay something seems to be wrong okay so the problem here is uh for if else we have end so this end is for this particular if statement but we forgot to add and for the defined rate so for example for every function in programming languages we do have an ending curly brace let's say right so this is let's say ending curly brace for this particular function okay and if we look into temps.yaml and if we look into the name here we go the name is liberally's name because we have not specified name in the values.yaml right so that's why the name is being taken from from release name right similarly if we specify the name from the name from values.aml right so name from values right and if we try to do the exact same thing now we should be able to see the name being generated as from values okay application dot name let's see okay so resolving this might take some time let's refer to some examples here if it's not going to work it's okay i think you you get the idea and here we go name from name from values right so now the name of the deployment is going to be named from values if we don't drive in that if we just go ahead and try to install that and we look into the deployment in the rest api namespace the name of that deployment should be should be named from values right so yeah this is this is what i wanted to discuss about and you can you can use named templates to make your make your hem chart very dynamic and very powerful i would say right another thing that i would like to discuss is uh this is this is the scope that is that is passed right so for this particular template uh the scope that this particular template has uh is what has been passed when this particular template was called right so for example since we passed and and from this particular while passing the scope we can pass any any any scope that we want to for example we can also pass let's say dot values or or dot chart uh from here right or dot release as well but in that case we would be able to just access the things inside dot chart right so we would not be able to access dot values right so that is about scoping off of these uh functions right now what i'm going to do is i'm going to push everything again into my github repo for this particular uh for this part in this particular branch that is help chart if you want to modify some things you you can do that eventually i would merge it in into the master right so yeah this was uh this was pretty much it that i that i wanted to discuss about again i'm looking into i'm looking into the the notes that i have if there is something else that i wanted to discuss about yeah so either we can you uh to call the template functions we so we have a named template here right lib.name and to actually use it or call it we used include here but there is another another what should i say another directive that we can use that is template to actually call that so instead of include we can use template as well but there is difference between include and template so include is a function but template is not so if you use include you can further use another functions right after pipeline so after using pipe you can use another another help help predefined functions but in in the case of template you you won't be able to so yeah this was this was pretty much it that i wanted to cover cover again this is uh there are a lot of other things that help provides us with but since this video would be much longer if i try to cover everything this is a starting block i would say once you have understood these things maybe you can you can try to understand understand things on your own and this is very helpful uh file i would say so you can see that you can you can actually refer this particular file to see how the the how these functions are being used in actual manifests uh in by generating a simple simple help chart right so yeah this was pretty much it if you think this is this is helpful for you uh maybe you can consider liking and subscribing uh share it with your friends and yeah thank you so much you
Info
Channel: Vivek Singh
Views: 7,338
Rating: undefined out of 5
Keywords:
Id: YDZ_i6OhjhQ
Channel Id: undefined
Length: 52min 4sec (3124 seconds)
Published: Sun Dec 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.