Complete Application Deployment using Kubernetes Components | Kubernetes Tutorial 20

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're gonna deploy two applications MongoDB and Express and I chose this too because it demonstrates really well a typical simple setup of a web application and its database so you can apply this to any similar setup you have so let's see how we're gonna do this so first we will create a MongoDB pod and in order to talk to that pot we are gonna need a service and we're gonna create an internal service which basically means that no external requests are allowed to the pod only components inside the same cluster can talk to it and that's what we want then we're going to create a Express deployment one we're going to need a database URL of MongoDB so that Express can connect to it and the second one is credentials so username and password of the database so that it can authenticate so the way we can pass this information to Express deployment is through its deployment configuration file through environmental variables because that's how the application is configured so we're gonna create a config map that contains database URL and we're gonna create a secret that contains the credentials and we're gonna reference both inside of that deployment file so once we have that setup we are gonna need Express to be accessible through a browser in order to do that we're gonna create an external service that will allow external requests to talk to the pod so the URL will be HTTP IP address of the node and the service port so with this set up the request flow will now look like this so the request comes from the browser and it goes to the external service of the Express which will then forward it to the Express pod the pod will then connect to internal service of MongoDB that's basically the database URL here and it will fall ready then to I'm going to be pod where it will authenticate the request using the credentials so now let's go and create this whole setup using kubernetes configuration files let's dive right into it and create the whole setup so first of all I have a mini cube class to run if I do cube city I'll get all which basically gets me all the components that are inside the cluster I only have a default currently service so my cluster is empty and I'm sorry from scratch so the first thing that I said we're gonna do is create a MongoDB deployment I usually created in an editor so I'm gonna go to visual studio code and paste a prepared deployment file there for MongoDB and this is how it's gonna look like so I have deployment kind and I have some metadata I'm just gonna call it MongoDB deployment labels and selectors in the previous video I already explained the syntax of kubernetes llamó configuration file so if you want to know what all these attributes mean then you can check out that video and here in the template I have a definition or blueprint for pots that this deployment gonna create and I'm just gonna go with one replica so the container is gonna be called MongoDB and this is the image that I'm gonna take so let's actually go and check out the image configuration for I'm going to be so montgo and I see this image here that's open this and basically what I'm looking for is how to use that container meaning what ports it's gonna open and what's external configuration it's gonna take so a default port of MongoDB container is twenty seven thousand seventeen so I'm gonna use that and we are gonna use variables environmental variables the root username and root password so basically I can on the container startup define the admin username and password so let's go ahead and configure all the inside the configuration file so here below the image MongoDB so we're just gonna leave the name of the image and is gonna pull the latest one and that's what we want so here I'm gonna specify what port I want to expose so ports attribute name and container port and that's the standard port so I'm gonna leave it and below that I'm gonna specify those two environments of variables so one is called let's see what it's called it's DB username and here was gonna be values so we're gonna actually leave it blank for now and the other one is called init root password and we're gonna leave that blank as well just view and once we have the values here we'll gonna have a complete deployment for MongoDB this is basically all we need now note that this is a configuration file that it's going to be checked into a repository so usually you wouldn't write admin username and password inside the configuration file so what we're gonna do now is we're gonna create a secret from where we will reference the values so meaning that the secret is gonna be live in kubernetes and nobody will have access to it in a git repository so we're gonna save this in complete deployment file first of all so let's call it deployment or let's just call it yamo and save it here so that we get the syntax highlight and now before we apply this configuration we're going to create the secret where the root username and password we'll leave so let's create a new file and I'm gonna paste in the configuration of a secret which is actually pretty simple so we have a kind secret then we have a metadata which again is just simply the name we're gonna call it MongoDB secret the type OPEC is actually a default type which is the most basic key value secret type other types for example include TLS certificates so you can create a secret specifically with the TLS certificate type and a couple of more types but mostly you're gonna use the default one and these are the actual contents so you have the data and here you have key value pairs which of course are the names you come up with so we're gonna specify user name or we can actually call it root username and we're gonna call it root password and here's the thing the values in in this key value pairs are not plaintext so when we are creating a secret the value must be base64 encoded so the way you can do that the simplest way is go to your terminal so here I'm gonna say echo - n very important option don't leave it out otherwise it's not gonna work and here I'm gonna put a plaintext value and I want so I'm just gonna go with just user name whatever of course you can have something more secretive here and I'm gonna base64 encoding and the value that I get here I'm gonna copy it into the secret configuration as a value and I'm gonna do the same with password so again I'm just gonna go with simple password obviously you want to have something more secure and I'm gonna copy that it's a value here and save it is secret thoughts mo okay now we have only written configuration files we haven't created anything yet in the cluster so this is just preparation work and we have to create secret before the deployment if we're gonna reference the secret inside of this so the order of creation matters because if I'm creating a deployment that reference is a secret that doesn't exist yet I'm gonna get an error so it's not gonna start since we have our first component let's actually go ahead and create our secret from a configuration file so again I'm gonna go to my console it's actually clear all this and I'm gonna go into the folder where I'm creating all this configuration files I called it kubernetes configuration and here I have both on my first so I'm doing I'm gonna do cube city I'll apply secret and secret create it so I'm gonna do cube city I'll get secret and I should see my secret has been created this is something created by default with a different type and this is our secret here so now that we have our secrets we can reference it inside over deployment configuration file so let's go back and this is how you reference contents specific key value data of secret so is there a value we're gonna say value from and then I'm gonna do secret key ref secret key reference and name is gonna be the secret name so this one here and key is gonna be the key in the data I want the value of this key value pair so I want this part of the data so I'm gonna reference it by key so you don't have to learn it by heart obviously all the syntax and attribute names important thing here is that you know approximately how to reference it the actual syntax you can always look up in Google or maybe from previous configuration files but yeah this is how you reference it and we're gonna do the same with password so I'm gonna do from and I'm just gonna copy the rest here remember llamo is very strict with the indentation here is the same secret but a different key so I'm gonna use password key here and that will be it so now we have the root username and password referenced from the secret and no actual values inside the configuration file which is good for security because you don't want your credentials in your code repository okay so our deployment file is actually ready so let's apply that apply and the deployment created meaning if I do get all I should see the pod starting up the deployment and the replica set so let's actually check how pod is doing container creating so let's actually watch it might take some time to create it if it takes long and if you want to see whether there's a problem there you can also do cube CTL described pod and the pod name so at least we know nothing's wrong there so we see that it's just pulling the image so that's what it takes so long so let's see again cube City I'll get pot and as you see it's running so we have more going to be deployment and the pod one replica of its pod running now the second step is we're gonna create an internal service so that other components or other parts can talk to this MongoDB so let's go ahead and create service configuration so go back to Yemen and here we can either create a separate mo configuration file for secret or we can also include it in the same way so in yellow you can actually put multiple documents in one file so if I put three dashes that's basically a syntax for document separation in the mo so I need new document is starting so actually I'm going to put both deployment and service in one configuration file because they usually belong together so here I'm gonna paste the service configuration and by the way I'm gonna put all these configuration files in git repository and link the repository in the description of this video so this is a service for MongoDB let's go through some of the attributes here so it's the service kind just the name we're gonna call it MongoDB service selector this is an important one because we want this service to connect to the pod right and the way to do that is using selector and label so using this here the labels that deployment pod have service can find the pots that it's going to attach to alright so we have the selector here and this is an important part where we expose service port so this is gonna be the service port and this is gonna be the container and since we expose container port it this address right here these two have to match so target port is container or pot port and this is the service port and obviously these two here can be different but I'm gonna go with the same port and that's basically it that's our service so I'm gonna create the service now so let's save this file and go back to my console and I'm gonna apply the same file that I applied before to create deployment so let's see what happens see both deployment and service configuration but it's gonna know that I haven't changed the deployment that's what it means here and service is created so if I were to edit both for example I can reapply the file and deployment and service can be changed so I think using local configuration files is a handy way to edit your components so now let's actually check that our service was created get service and this is our service and it's listening at port twenty-seven thousand seventeen and I showed it in one of the previous videos but we can actually also validate that the service is attached to the correct pod and to do that I'm gonna do describe service you know need the service name for this so here I have the endpoint which is an IP address of a pod and the port where the application inside the pod is listening it so let's actually check that this is the right pot I mean we just have one but still so if I do get pot and I want additional output to what I get by default one of the columns includes the IP address which is this one right here so 172 point 17:06 that's the pod IP address and this is the port where the application inside the pod is listening at so everything is set up perfectly MongoDB deployment and service has been created and by the way if you want to see all the components for one application you can also display them using Kip's ETL get all that will show all the components and you can filter them by name so DB and here you see the service deployment replicas said and the pod so when you do all that component type will be the first here okay that's just a side info so now the next step we're gonna create Express deployment service and also an external configuration where we're gonna put the database URL form I'm going to be so let's go ahead and do it so I'm gonna clear that up go and create a new file for Express deployment service so this is the deployment draft of Express same things here Express that's the name and here we have the pod definition where the image name is Express let's actually go ahead and check that image as well we don't need this express and it's the name of the image more Express and let's see the same data here let's see the port the Express application inside the container starts at is 80 81 and these are some of the environmental variables so obviously we need three things for my workspace we need to tell it which database application it should connect to so obviously we need to tell it the MongoDB address database address it should connect to the internal service and we we're gonna need credentials so that MongoDB can authenticate that connection and the environments of variables to do that is going to be admin username admin password and the MongoDB endpoint will be this year so these three environments of variables we need so let's go ahead and use that so first we're gonna open the port again container ports and the reason why you have multiple ports is that inside of the pot you can actually open multiple ports so that's gonna be a t1 and now we're gonna add the environment variables for the connectivity so the first one is the username and this is gonna be obviously the same username and password that we defined right here so what I'm gonna do is I'm just gonna copy them because it's really the same so the value from we're gonna read it from the secret that's already there so I'm gonna paste it here second environmental variable is called an password and I'm also going to copy that from here and the third one is gonna be the database server and since this is also an external configuration we could either do value here and we could write the MongoDB server address directly here or as I showed you in the diagram at the beginning we can put it in a config map which is an external configuration so that it's centralized so it's stored in one place and also other components can also use it so for example if I have two applications that are using MongoDB database then I can just reference that external configuration here and if I have to change it at some point I just change it in one place and nothing else gets updated so because of that we're gonna keep this incomplete deployment configuration and we're going to create the config map which will contain the MongoDB server address so I'm gonna create a new file let's actually save this incomplete deployment let's call it Express demo and we're gonna come back to it later so save that now we need a config map here so copy the configuration and this is also pretty simple just like secret you have the kind which is config map the name and the same construct see just like you saw here data which is key value pair it doesn't have a type because there's just one config map type and that's it and here you'll again have key value pairs so database URL and server name is actually the name of the service it's as simple as that so what do we call our service we called it MongoDB service so I'm gonna copy the service name and that's gonna be the database server URL so I'm gonna copy that and let's actually call it config map for consistency and save it and just like with secret the order of execution or creation matters so I have to have a config map already in the cluster so that I can reference it so when we're done I have to create the config map first and then the deployment so the way that I can reference the config map inside the deployment is very similar to secret so I'm actually gonna copy the whole thing from secret here the only thing different here is that instead of secret I'm gonna say config map it's all camelcase and obviously the name is gonna be conflict map that's what we called it I think yes that's the name that's actually copied and again key is the key in the key value pair here so let's copy that as well so now I have our manga request deployment these are just standard stuff and this is where the pod blueprint or container configuration exists we have exposed port 8081 this is the image with latest tech and these are the three environmental variables that manga express needs to connect and authenticate with MongoDB so deployment is done and let's go ahead and create config map first and then MA Express deployment keep CTL applying config map and I'm gonna do it oopsie they'll apply always Express and let's see the pot so container creating looks good so let's see the pod and it's running and I actually want to see the locks so I'm gonna lock the Express and here you see that Express service started and database connected so now the final step is to exes manga Express from a browser and in order to do that we are gonna need an external service for Momo Express so let's go ahead and create that one as well so it's clear this output go back to visual code and as we need last time in the same file as a deployment I'm gonna create more Express service because actually in practice you never have deployment without the service so it makes sense to keep them together and this is Express external service and this configuration right now looks exactly the same as the DB service configuration and even ports are the same like here I have exposed service port at 80 81 and target port is where the container port is listening so how do I make this external service is by doing two things so in the specification section so I'm gonna do it below the selector I'm gonna put a type and a type of this external service is load balancer which I think is a bad name for external service because internal service also acts as a load balancer so if I had two MongoDB pots the internal service would also load balance the requests coming to these pots so I think the load balancer type name was chosen not very well because it can be confusing but what this type load balancer does basically is it accepts external requests by assigning the service and external IP address so another thing that we're gonna do here to make this service external is right here we're gonna provide third port and this is gonna be called node port and what this is basically is the port where this external IP address will be open so this will be the port that I'll have to put in the browser to access this service and this note port actually has a range and that range is between 30,000 and 32,000 something so I can not give it the same port here as I said it has to be between that range so I'm just gonna go with the 30,000 that's the minimum in that range and that would be it so this configuration here will create an external service let's go ahead and do it and I will show you exactly how these ports differ from each other so I'm gonna apply Express so service created and if I do get service I see that MongoDB service that we created previously has a type of cluster IP and the mono Express service that we just created is load balancer which is the type that we specifically defined an internal service we didn't specify any type because cluster IP which is the same as an internal service type is default so you don't have to define it when you're creating internal service and the difference here is that cluster IP will give the service an internal IP address which is this one right here so this is an internal IP address of the service and load balancer will also give service an internal IP address but in addition to that it will also give the service an external IP address where the external requests will be coming from and here it says pending because we're in mini cube and it works a little bit differently in a regular kubernetes setup here you would also see an actual IP address a public one and this is another difference because with internal IP address you just have port for that IP address with both internal and external IP addresses you have ports for both of them and that's why we had to define third port which was for the external IP address this is that pending means that it doesn't have that external IP address yet so in mini the way to do that is using the command mini cube service and I'm gonna need the name of the service so this command will basically assign external service a public IP address so I'm going to execute this and the browser window will open and I will see my Express page so if I go back to the command line you see that this command here assigned Express service a URL with the public IP address or with an external IP address and the port which is what we defined in the node port so I can basically copy that command which is the same as this one here and I get the page form Express so now with this setup the way it's gonna work is that when I make changes here for example I'm gonna create a new database let's call it test DB whatever and I'm gonna create a request what just happened in background is that this requests lended with the external service of Express which then forwarded it to the Express pot and the Express pod connected to the MongoDB service and internal service and MongoDB service then forwarded that request finally to the MongoDB pod and then all the way back and we have the changes here so that's how you deploy a simple application set up in a kubernetes cluster thanks for watching the video I hope it was helpful and if it was don't forget to like it this is a video series so I will create a new one every week so if you want to be notified whenever a new video comes out then subscribe to my channel if you have any questions if something wasn't clear in the video please post them in a comment section below and I will try to answer them so thank you and see you in the next video
Info
Channel: TechWorld with Nana
Views: 169,186
Rating: undefined out of 5
Keywords: kubernetes deployment, kubernetes example deployment, getting started with kubernetes, kubernetes in practice, kubernetes create deployment and service, kubernetes components, kubernetes cluster setup tutorial, kubernetes application setup, kubernetes setup from scratch, kubernetes setup, kubernetes complete application setup, kubernetes practical example, kubernetes services, kubernetes secrets, kubernetes configmap, kubernetes, kubernetes demo, kubernetes demo deployment, k8s
Id: EQNO_kM96Mo
Channel Id: undefined
Length: 30min 27sec (1827 seconds)
Published: Fri Feb 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.