.NET 7 💥 - Deploy .NET Apps to Kubernetes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign thank you for watching this video I am Muhammad and today we're gonna be discussing how we can actually deploy our dotnet web API into kubernetes we're going to be utilizing a local kubernetes cluster on our own machine and we're going to be seeing step by step how we can actually create our application create a Docker image and then deploy to our kubernetes cluster if you like this video please like share and subscribe it really helped the channel as well if you'd like to support me please consider supporting me on patreon or buying me a coffee now grab a cup of coffee and let's get started so the first thing that we need to do is we need to make sure that we have the basic requirements available on our machine we're going to be needing not SDK and we're going to be needing Docker installed on our machine and once we have those installed and make sure that they are running as they should be we can actually start coding our application so let's do these checks right now so inside our terminal what we're going to be doing is we're going to be checking our note version.net version and as you can see I'm currently running on version 7.0.03 which is going to be the latest version by the time of this recording and as for docker I'm just gonna run it right now but if we don't have it you can go directly to zoom in go to Google type download docker and it will take you to this link inside here just make sure you download Based on your own machine so currently I'm on a Mac so you download if you're on a Mac on a Windows or Linux make sure you download it and install it and once it's downloaded and installed successfully all you need to do is once you're on it you're going to be seeing on the top right hand side you're going to see like a whale icon and once you open it up you're going to be seeing something like this and basically it's going to show you everything which is available on a machine for you it might be empty for me because I have some already some containers available it's already been populated as well the second thing that you need to do once you have downloaded and installed you need to go to settings and you need to go all the way down to kubernetes and make sure you enable both of them and click on apply and distort so once you have all of this ready we made sure that our.net SDK is available we have make sure that Docker is installed on our machine and running as well as kubernetes instances are also available locally on our machine once you have done all of that the next step is you need to go to hub.docker.com and I highly suggest that you create a an account here because we're going to be needing it to push our images it's a free service you can have one private directory a one-profit repository and as many as you want public ones so please create also an account on Docker hub so now that we have these three information available or basically the SDK the docker instance installed on our machine with kubernetes enables and we have a Docker Hub account now we are actually able to start our application and basic start coding so what I'm going to be doing is I'm just going to go back to my terminal I'm gonna go to my desktop work learning.net kubernetes and inside this directly I'm gonna create a new application and so you're going to be a very simple application so it's going to put the nut new web API and I'm just going to call this let's say formula one kubernetes pretty straightforward and just so we make sure that this application is unique so now that we have created it it's going to take a few seconds now let's navigate to it and now all we're going to be doing is just going to open it in Visual Studio code perfect so once we have done that the first thing that we're going to be doing is we're just going to add some binder modification for this to make sure that this is ordering as it should be and it's running basically over on instance so inside my controllers here we have a for weather forecast which is uh by default put strap when we create our application I'm just going to change the summary here to hello from my local uh something like that just to make sure that's different now let's open our terminal and I'm just going to do that not run be perfect just copy this and let's go back to our web browser let's open this here for the slash Swagger forward slash inductor HTML and now we can see here I have my water forecast controller and if I open on this I click on try it out then execute we'll be able to see that I have Hadoop from my local app and then I have the actual weather forecast okay great so now that my application is running locally what I want to do as well is I want to go back to my program.cs and I want to make sure Swagger is available for me anywhere I don't want it just in development mode because basically right now what we're going to be doing is we're going to be publishing our application I just want it to be available and I don't want this let's delete this and I want to remove the https redirection for now because I don't really need it that's going to be a completely different topic so for simulated simplicity's sake I just made I made some changes to my API so make sure that I customize it a bit and then what I did from there is I just enabled Swagger even in production mode it's just again for testing and of course in the real life scenario you should not do that and lastly I just remove the https redirection because right now we don't really need it so now once we have done that I'm gonna stop my application and what I want to do here is ADD our root director I'm going to create a new file and this file I'm just going to call it docker file pretty straightforward and what I'm going to be doing here right now I'm not going to be delving into uh how Docker works in the background I have a different video which basically explain how everything works within Docker but just a quick overview Docker is like a virtualization platform where basically we are running our application and a containerized way where basically it has all of the available sdks libraries and independencies that it needs is all available within the same Docker instance and Docker container and that way we're basically making sure that our applications have all of everything that needs to run we're not relying on any third-party dependency which is not available currently inside the docker container and what we're going to be doing right now is we're going to be containerizing our.net application and basically once we containerize it we're going to be seeing how we can actually deploy to our kubernetes instance so right now let us start firstly by cutting a base image and we're going to be utilizing MCR Dot microsoft.com forward slash.net forward slash SDK 7.0 and then we're gonna just put as this is going to be our build environment and this basically the base image we're going to be utilizing in order for us to basically create our application and deploy it the second thing is we're going to specify our work directory now it's going to be a simply forward slash up which is going to be a folded inside that instance that we need and then I'm just going to expose ports I'm going to expose Port 80. I'm just going to expose also Port 443 although we have disabled it but it's always good to expose it perfect now the next step is I want to copy my CS approach to the directory and then I'm just gonna run a command which is going to be.net store and basically here I'm gonna install all of the dependency for my application then what I'm gonna do I'm gonna copy everything else and once I do that I'm just gonna run .net publish Dash C release and I'm gonna put it in an output file perfect once that is done I'm going to create another get another base image and here all I'm going to be doing is specifying a work directory inside this work image and then I'm gonna copy whatever I publish from the first image to the find an image and then I need to specify that I want to copy everything and lastly I want to specify my entry point and in this case it's going to be dot not and we'll call that Formula One formula one to dll Let's double check this in the bin folder Formula One Ka ads okay perfect so now let's do a review of what we have done here so far so basically what we have done here is first of all we have basically get a base image so let's add a comment here we're just going to say this is the base image where everything is going to be building we created a folder we exported the right port and then we have copied the cs plus file into the store the reason that we have done this in case there's any problem we are basically stopping the processing of our Docker file directly here prior to continuing everything else and we can actually hear what we're doing is we're doing separation of concerns so basically we're dividing the steps into small steps instead of having one single big stat copy and restore and that way we are actually able to see where is exactly it's failing in case there's any problems and to allow us to have a bit much more control about debugging any issue that we might have faced so what I'm doing here is I'm just copying the Cs approach from my local directory so from my here to the docker container and basically I'm restoring it I'm just applying the not restore to get it and once I done that I'm copying everything else and then I'm publishing it inside a folder inside my uh local container inside the folder called out and once I have done that I just specify the final image it's here and basically inside of this I'm copying the output of my publish from the first image to this one and just specifying an entry point and you might think to yourself why are we copying it why don't we just use this because basically if you take a look here what we did is we have copied all of the source code inside our Docker image and then we have published it and you don't really want all of the source code all we want to do is to have is these types of dll which exists here rather than have the source code of all application and by publishing it and then copying the publish all we're doing is we're only cutting these information that we currently see here which is going to be our our Json file so on so for our configuration we don't really need the actual source code so that's why we're doing it so once we have done that let us check if our Docker image is actually building successfully so we're gonna go to the terminal let's clear it up let's make it a bit tired let me see how it looks okay great I'm just gonna put Docker build and then I'm gonna specify everything I'm gonna tag it and basically let's do it without attack and then we'll do it with a tag so now we can see that I will instance is running and we have a problem here publish okay I have a problem uh I will probably strong let's run this again okay perfect now my build has been completed and this is the ID that I got as you can see this ID is not really user friendly so what I want to do is I want to give my image that I just created a name where I can actually refer to it easily so let me clear this again and uh for me to do that all I need to do is put Dash D and then I give it the name that I want I'm just gonna call it formula uh key KAS swimming formula kubernetes now if I run this again everything runs and now we can see here that this is how I'm referring to my application so once I have done this now let us run our application in a Docker container running directly from Docker container to see if everything is working as it should be and for me to do that I'm just going to put docker run and here I need to specify the ports that I want to connect to so basically I'm just going to tell it that I'm gonna connect to it from Port 8083 and I'm gonna map it to the internal Port which is going to be 80 once my application is running and then I want to specify here my environment variable and here I'm basically telling asp.net that it needs to listen to Port 80 for my application because if you remember whenever we're trying to run our application let's do it here in a new tab if I do not run right now as you can see here it generates a random port and I don't want that I want to specify the port to be Port 80 inside this container rather than having this random port so that's why I need to specify here a random uh sorry a port that it needs to listen to and for me to do that all I need to do is just put asp.net core underscore urls equal HTTP is it looking good let's clear it up and then we can continue so clear okay let's do it again here so it's gonna be Docker run specify the port I'm gonna utilize Port 80 83 to connect to Port 80 and then I need to specify my environment variable which is going to be the ASP not the core underscore URLs it's going to be equal to http and then we'll put here the plus sign so it doesn't really matter what it is and I'm just going to put this hasters into Port 80 and then I'm going to specify my image name and I think I called it we can go up and see what did I call it formula okay let's complete this okay and now if I run this we can see here that my application is currently running inside my content of point 80 but for me to connect with I need to utilize Port 8082 so let's go back to my web browser and I'm just going to open it here I'm gonna put localhost and I'm gonna put here worth 80 82 for the slash Swagger forward slash index.html and now you can see here that this is my application running inside my Docker container if I try it out execute we can see I'm getting the information that I need here similar to what we have done download let's make sure that this instance that you are currently running is different from my local environment so what I'm going to do I'm gonna update my local application change the text in my gut response and basically see it that both of them are completely separate so I'm just going to go back here and I'm gonna come back to my controller here and I'm just gonna say this is from my local instead of Local app local computer not docker and now in my other terminal I'm just gonna run this again and now we can see it utilized the port 5153 so let's go back to my web browser and I think this tab refers to it now if I refresh now we can see that if I want to execute this we can see here that hello from the local computer not Docker and the other one which is also still running if I refresh click to write out execute we can see this is running from Hello up from the local app so we can see here now I have two versions of my applications running one running locally on my own machine without Docker and one is running inside my Dockers container and you thought here as you saw here the changes that I did did not automatically reflect inside my Docker image because basically in order for me to update a Docker image I need to basically do another build again and once I do another build it will basically update the current version of the image and will basically once I stock my container around it again it will be able to pick it up so right now if I just stop this for now and in order for me to update my Docker image to have the latest version I just need to run let's clear it up I just need to run my Docker build again so I'm just going to put docker build dot T and we called it formula 8 formula kubernetes and now we can see it's doing all of the building process again let's clear it again and now if I just run it directly from my docker on Port 8082 and I come back to my web browser so this was before the update now if I refresh and I come here and click on try it out execute we can see here that I got the latest version inside my Docker instance which is basically this is how we update it so now that we are we made sure that my Docker image is running as it should be I'm basically I'm able to uh deploy it basically run my Docker image directly inside Docker the next step is we're just gonna go a quick overview about kubernetes and we're gonna understand how that will fit uh with what with the docker that we have created how it will fit within a kubernetes so basically kubernetes if you don't know it's a container orchestration system it's basically allows us to orchestrate containers so in this in this example we only have one container to orchestrate which is going to be our application that we have created but mostly kubernetes is being utilized if you have a lot of micro services and you want to make sure that they are all running in the right way in case a container Fades is automatically being another one will be able to pop up there is auto healing so basically in case there's something went wrong the number of cluster the number of instances of all my application is still spread out in case there is a high load it will be able to scale Up and Down based on the load that we currently have so kubernetes is an orchestration which is available from Google I think in 2015 it went into the cloud foundation and basically it is a way where we can actually deploy our containers to and basically we can manage them on a large scale from there for this video to make it simple we're just going to utilizing a local version of kubernetes on our machine which comes when we install Docker desktop but basically you can utilize this if you're utilizing AWS fertilizing Azure Google Cloud so on so forth and what we're going to be doing here is we're just going to be creating a single container rather than trying to manage multiple containers so kubernetes is a domain by itself and there's a lot of information there about it so if you want me to jump and give an example about how kubernetes works and the structure behind it please have been on documents down below and then make sure I'll create a video about that but for now we're just going to be skipping all of those introductions we're just going to do a very quick one to understand the different components of a kubernetes cluster and then we're just going to directly stop start by actually implementing it but if you want me to have a much more deeper dive into kubernetes please let me know so basically our kubernetes clusters we can think about it as our big server in a certain way So This Server is going to be composed of multiple items and the main thing is there's going to be three components that we need to take a look at we're going to have the apis we're going to have the control plane and we're going to be half our nodes so basically our API here is whatever how we are actually able to interact with kubernetes so basically because of kubernetes is a managed service and basically anyway if you want to communicate with it there is an API set of apis available for us that we can actually utilize and these apis are directly available from the command line there's guis that we can utilize there's also different items that we can use in order for us to communicate with this API but in essence the main way that we communicate with our kubernetes clusters through those apis and we're going to be seeing how we can actually execute some basic commands against these apis so the second thing is we need to take a look at is the control plane under control plane we can think about it as Dr Brain of our cluster where it's actually manage everything that we need within our uh cluster so we can see here scheduler is going to be responsible for making sure that all of our instances is running and it's a certain extent make sure that in case there's something went wrong it will be able to publish new ones the edcd is like a key value pair store like a database where they store all of the different configuration that we need inside our cluster our control manager is basically something to keep the control player up and running we're not going to be really diving out of deep on how this works but you just if you see this you will just want to understand what they do the main thing that we're going to be focusing here is the nodes and the nodes is basically composed of cubelets I am inside the cubelets we're gonna have basically have our containers running so what we're going to be doing is we're going to be creating the actual deployment code for anode inside the deployment and we're going to be able to see how we can put our Docker image inside this deployment and once we have done that we're going to be able to see how we can deploy to a single node because it's at the end of the day it's on our local machine we don't really want to propagate it but we're going to be able to see how we can create different replica sets so we can gonna have different instances of our application also running simultaneously so that way we are basically able to see that we can scale it up scale it down we can make sure that we are covered on every single scenario that we might need but this is a very quick introduction about kubernetes and basically kubernetes worked in conjunction with Docker so it's not a replacement and Docker is not only the container only container service that kubernetes kubernetes can be adjusted to take different container systems but just make this video simple we're just going to be utilizing for this reason Docker so let's go let's go back to or before we do any of that make sure that you have an account open on Docker Hub because we're going to be hitting this account here to basically make sure that we have uploaded our image to it so please make sure you have a Docker Hub account available because that's what we're going to be it's a free you don't have to pay anything for it uh this is what we're going to be using to host our images so now that I have mentioned that let's go back to uh my application and what I want to do right now I want to push this version of my application Formula One k8s to my Docker Hub here so we're able to see it here so we can utilize it inside my kubernetes cluster and you might think to yourself okay why don't we just if it's available inside our own machine why don't we just utilize it from within the local kubernetes cluster this is a more simpler upload because we don't really want to configure kubernetes to speak to our local directory but because we uploaded on Docker Hub Docker Hub it's a free open Repository we don't really need to do any configuration kubernetes will be able to form the directly inside the docker Hub but if you want to utilize our local registry we need to do some configuration so kubernetes is a way is aware of our local instance we don't really want to do this right now so and the way we're gonna be able to push this it's going to be quite simple so the way let's go back here I'm just gonna clear this up and basically the way you push it is just for Docker push and then if you take a look first of all before we do a Docker push we need to Target when a search in a certain way so we need to put docker build dot dash D and the way you want to tag it is first of all it's going to take our username from Docker which is going to be Muhammad in my instance and because you can see here this is my username Muhammad lawand and if I go to my profile and you can see this is my username and once I do that I just want to specify a forward slash and once I have done that I'm going to specify my image name and this is going to be a formula k8s and I'm just going to give it a version one now if I do that we can see here at the end that my image has been tagged with this so now if I clear it and I do docker push and then I specify the name which is going to be Muhammad rawand forward slash formula k8s V1 and now you can see here that it's starting to push perfect now if I go back to my Docker repository and the file refresh now you can see here that a few seconds ago I was able to actually push my formula uh get it as to my Docker now it's available online on Docker Hub so for me to utilize so make sure once you push it you are able to see your Docker image here so once we have done that the next step is inside my root directory I'm going to be creating a new file and this one is going to be called deployment and yes kubernetes work with yaml so before we do anything just to make sure that our kubernetes is running I'm just going to clear this and we're just going to put Cube CTL and Cube CTL here allows us to communicate directly with kubernetes API that are available so if we go back here so basically once we to our Cube cctl it will allow us to communicate from our terminal with our kubernetes cluster which is available here and if you want to make sure that our kubernetes cluster is running either but when you click on the whale icon you can see kubernetes running or if we click on the dashboard you're going to be able to see it here kubernet is running just to make sure that uh if you want to double check that your kubernetes is running so now that we have Cube CTL I just want to put get all and basically here we're going to be seeing that I don't have anything uh running inside my service rather than the only kubernetes service which is exactly what we want at this moment so now what I want to do is I want to start creating my deployment and then we're going to actually see how we can deploy this deployment so first of all I want to specify my API version and in this case it's going to be Ops forward slash V1 and then I need to specify what kind of service I want to create and this is going to be a deployment perfect so now that I have specified my API version and what kind of deployment do I need then I need to specify some metadata and this metadata is really important because basically the way uh kubernetes is able to filter out through different deployments different pods different systems is through metadata and searching through metadata so it's really important for us to make sure that the metadata which we input are valid and actually represent of the changes that we currently have inside our kubernetes cluster so metadata and then I'm going to specify in my name I'm just gonna call it formula uh deployment this is what we're going to be doing deployment and then I'm going to specify The annotation and here because it's going to be a deployment I'm just going to say that this is going to be an initial deployment once we want to actually upgrade it so let's say we did some changes on my Docker image and I want to update it from version 1 to version two inside my annotation in I specify that this is upgrading the version of the container from version 1 to version two so that's where I can actually see that my different deployment stack and if I want to revert back to a previous deployment I can do that if I wanna view all of my deployment has still here I will be able to see that as well so here inside my annotation I'm just going to specify here my kubernetes dot IO forward slash change course and then you specify the message I'm just say initial deployment with the app I want to have specified my annotation the next thing that I need to do I need to specify my spec and the specs here is going to be allowing me to configure this deployment and giving it all of the specs that I need and here I'm gonna specify how many replicas or how many version of my application I want to run simultaneously and for simplistic I'm just gonna say five we can make it one we can make it 10 100 as many as I want just for Simplicity I just want to make it five and then once I specify my replicas I want to make sure that I specified my selector and the selector is going to work here on the labels March labels and I'm just gonna call my app right now formula Dash up and basically the reason I have the match label available here let me close this so you have so we can see it on a much more all of it on one page because we're going to be referring this when we're actually defining our container so once we have done that the next step is now we need to specify the template so if we go back make sure this is yaml so make sure spacing is correct so right now as you can see I want two back two Backspaces under selectors so now we can see that I'm directly under selector I want to specify here template and inside my template I want to specify my metadata and in this case I'm going to specify the name of my container that I want to run and in this case it's going to be my formula I can call it whatever I want let's just make sure my formula up all my formula okay it does anything that I want let's keep it formula up just for simplicity's sake actually let's keep it as SKS and then what I want to do here I want to specify my labels again because this is labels I'm going to utilize for too much so here we're gonna see first of all it's going to be the environment that I want to make sure this is going to be a production environment then I'm going to specify the app and this is going to be called formula up and then here I want to specify the way I'm going to be referring to this so app Dot kubernetes dot IO forward slash name and here I'm just going to specify this formula up and this one here the app dot kubernetes.io forward slash name is like a Reserve name or reserved key for then my labels so if I'm trying to refer to this instance from anywhere else or let's say from another service that kubernetes is using I can use this directly from here so once I have done this I need to specify now the spec for this template for this kubernetes for this container service that I'm trying to create and we need to make sure that I'm under metadata so again spacing is really really important in yaml specifically when we're running with kubernetes and here I need to specify my spec and as you can see here directly I'm I need to specify my containers and in this containers I'm going to specify the name for it and we're just going to call it formula up and then I need to specify my image and this is why we deployed it into Docker Hub so right now if I go to Docker Hub this is going to be my image name so I can just copy this if I click here on tags we can see my different tags and we can see that I'm a version one so I'm just going to take this and I'm just going to put that this is going to be version one and once I specify my image now I need to specify the ports so this one is going to be if you remember the command that we did to run Docker Hub to run Docker Locker or machine it specified the ports for my application and then here what I'm going to be doing I'm gonna refer to the sport as a name so basically in order for me to load traffic so kubernetes by itself is going to be closed and we're not going to be able to communicate with it directly so what we need to do is we need to create a service within kubernetes that can allow the flow of traffic from the outside Network to its inside Network and in order for us to do this we can we can either utilize directly the port 80 or we can give it a name if we give it a name we can utilize Port 80 is fine but the reason I'm matching here the name just so you can see if you have multiple apps I do not really want to memorize all of these sports that you are giving to your apps you can just refer to a name so that's why we're just giving the all different options that you might need to utilize so here if under containers again spacing is really important um and I'm just going to call this HTTP swab Dash API now once we have done that make sure that you go back under ports and we need to specify the resources we want to tell it how much resource it needs to allocate to this container to run and I'm just going to say it's going to requests CPU I'm gonna make it very small which is going to be 100 mm and then we're going to make it the memory so here it's going to be like 0.1 version of our CPU and we're going to make it with like 100 megabytes of memory and then other than that I'm going to specify the limits so it will not be able to get more than that so the CPU is gonna be maximum 200 0.2 and my memory is going to maximum of 200 megabytes we don't really need more than that for a simple empty API okay once we have done that now I need to specify my environment variable if I remember inside my terminal I had to specify my ASP not core URLs so I will tell yes the container that it needs to run on Port 80. so now this is what I want to do here so again make sure that you are under resources and then here we need to specify our environment variable and I'm just going to give it a name I'm gonna put the ASB nut core underscore urls and I'll specify the value in this case it's going to be HTTP colon forward slash forward slash plus column 80. and here basically I'm just specifying the same environment variable that I specified for my container so all of that here that you're seeing all the way from containers all the way down is just giving the configuring our simple Docker image and everything you see on top is basically what the giving kubernetes some information for it to actually be able to build this image for us let me see how this is looking okay I think it looks good so now once we have done that the next step is as we said I want to be able to communicate with my actually before we do that let's open our terminal and inside my terminal here I just wanna first of all apply this so Cube CTL again this is the CLI command that allows me to communicate with kubernetes I'm gonna do apply then specify the file which is going to be dash off and I'm going to put deployments.tamil and if I run this right now we can see that it has been created and if I want to check this out I can put Cube CTL got all and now as you can see here let me Zoom this a bit make this a bit bigger as you can see here I have five version of my application running as you see version one two three four five we can see that my deployment is here we have five out of five because we specifies five five replica sets and we can see here that everything is running as it should be so if I got this again we can see that the edges just keep getting bigger and bigger but something here to keep in mind is that all of this is managed by here so for example if I want to change my replica set from 5 to 10 if I save this I do another Cube CTL apply Dash F and specify deployment.yaml we can see it's set configured now and if I put get Cube CTL got all now we can see that I have more instance running we can see some of them are older and some of them are new which has just been created and this this is basically allows me to view all of my uh containers and basically all of the pods so if you take a look here we can see that you have pod and you have the container inside of it pod container inside of it so this refers back to this inside anode we have those different cubelets and you can see here that basically inside my single node I have different cubelets and every single one of them have a different node available I have a different pod available so that's just something to keep in mind so now I'm just gonna revert it back to five and see how that will work so now let's make it four even and now if I just do a cube City AL apply dash off and put deployment.tml and that I put get all now you can see here that some of the answers are being terminated terminated terminated I'm only gonna able to see have four so now if I go back get all we only have four running and everything has been updated perfect so now once I have done that now I know that my application is running is running inside kubernetes they are healthy I have four out of four based on my requirement but how can I connect to it because right now everything is running inside the world corner of kubernetes but I want the way where I can actually propagate the traffic to it so let's see how we can actually implement this so after I specify my deployment let's close this I'm just gonna do these three lines here so this is really important so these three lines will allow me to specify different commands for kubernetes it will basically like creating a new file and uh creating a new file that I'm able to actually give more commands so what I want to do here is I want to create a new service I'm going to put API version and it's going to be version one and here I'm gonna specify the kind of this version a service I'm gonna say it's gonna be a service and from here I want to specify the metadata it's going to give it a name and I'm gonna give this name my load balancer service actually my LV server should be enough because basically let's go back here what I want to do is I want to specify some kind of a load balancer which available here and this load balancer is going to be responsible for receiving traffic from the outside and routing it to the different nodes that exist at different pods because right now I have four I cannot really decide on which one of these one of these pods that my request gonna go to and for this I need some kind of a load balancer which is going to be responsible for managing that traffic it's going to see which spot is available for me and based on that it's gonna be forwarding the request to it because I don't really want to do all of the management of this by myself I can just utilize a load balancer for me to manage this for me and now if I go here once I've done this I need to specify the spec for this load balancer and I'm gonna give it a selector and basically the select it here I need to make sure uh that it's gonna be matching whatever I specified here and in this case it's gonna be app.kubernetes copy just this because this is where we're gonna we're referring this load balancer to this deployment we are conducting them with each other so it's really important for us to make sure that we got this right the same selector so make sure that app that kubernetes name this needs to exactly match this one if we don't it's not gonna work so once we have specified the selector the next step for us is we specify the ports how are we going to be able to communicate with this load balancer and here I'm just going to say give it a name I'm just going to say formula Dash up the sport and then here I'm just gonna provide the protocol and it's going to be over TCP and then specify the port that I want to utilize to communicate with it I'm just going to transport 8080. and I want to forward this port to Target port it's going to be Port 80 because this is what the port that we have specialized here as you can see this is the port that we are targeting and this is the container Port which is available and the last thing that I want to specify here and it's going to be the type of the service I'm just going to make sure that this type is a load balancer as simple as that so now what I did here I just created a load balancer and this load balancer I'm just making sure that it matched the deployment that I had before and specifying the ports for this load balancer where it needs to take the request and forward it to as simple as that now if I go back to my terminal and I do the same command again so first of all I'm gonna put the cube CTL got all we can see everything is running now if I wanna delete it I'm gonna put Cube CTL we don't really need to delete it but I just want to show you the command to delete dash off I've just specify the deployment it's going to tell me you cannot delete a service because it does not exist but the first one it doesn't exist which is perfectly fine now I'm just going to add on it again Cube let's clear this up and then I put the cube CTL apply dash of deployment and now we can see here a service has been created and a uh my deployment has been created so if I put Cube CTL got all here we can see actually that my four containers is running as it should be but something really important you can see now I have a different service which is my load balancer service and now as you can see it has an external IP that I can utilize to connect to and this external IP is going to be on port 8080 so let's see how this is gonna work right now so if I go back to my web browser and this should be stopped right now because it will stop the container this should be stopped because we also stopped the application from running locally or we didn't really stop this still running let's stop this so now if I refresh it's stopped perfect let's copy this URL I just want to update the ports to Port 8080. and now if everything goes to plan I should be able to see that my instance is running and now you can see that my application is running from within kubernetes on port 8080 which I have specified here and if I run this right out execute now you can see hello from my local computer now let's see how we can actually update this so right now what we have done is we have created our deployment creating our load balancer we are able to communicate with my application from within my local browser admin conduction is coming through to my pods where my container is running now I want to do I want to show you the the process of what we can do in order for us to actually update those containers from home version to another so I'm just gonna go back to my application I'm gonna go back to my controller and I'm just gonna say hello from kubernetes that's it and that the steps is going to be as follow so first of all what I want to do is I wanna create a new Docker version basic a Docker image so docker thus sorry Docker build Dot Dusty and then here I'm going to specify the name of it Muhammad Lawanda forward slash formula k8s and we're gonna give it version two so now this building perfect now the next step is I want to build it so sorry push it so I'm going to put docker push Muhammad rawand Ford slash formula foreign version 2. oh okay now it's pushing okay great now if I go back to my web browser and I go to Docker hub if I refresh this one we can see that we're gonna have version one and version two available for us now so we can see here we have version one and we have version two so what I want right now on utilize version two inside my application so if I go back to my deployment and what I want to do here is if I want to just change it up so first of all I want to change the course so I'm just going to say updating my Docker image from V1 to V2 and then here what I need to do is just specify a new version of my Docker image so the V1 put V2 save this and now again Cube CTL got all make sure everything is still running as it should be Cube or spelled Cube CTL now we can see everything is still running I'm just gonna put Cube CTL apply dash of deployment and now we can see here that the configuration the new deployment has been configured and now if I go back to my web browser and I refresh this first of all let us just make sure that everything has been updated so Cube CTL got all we can see here that everything is running because now that we can see the edge is 16 seconds because everything has a new version now and if I click on this and let's do a refresh select this try it out execute now you can see here that I'm running on the latest version which is going to be hello from kubernetes and basically once we have done this we are we basically got the latest version of our kubernetes instance available directly inside so the latest version of our Docker image inside our kubernetes instance so with that we were able to see how we can actually utilize kubernetes in order for us to host our application and let's do a quick summary of what we have done so far so basically what we have did is because our API we did some minor changes to it then we created our Docker file for it where we are actually able to build it and build an image then we have deployed this to our Docker Hub where it's available for us once we have deployed it to the docker Hub we created a deployment.tml file where we specified all of the deployment code that we needed for kubernetes once we have done all of that we created another load balancer service and this load balancer surface is going to be responsible for propagating the request from my local machine to my kubernetes cluster and once we have done this we are able to actually communicate with it then we have seen how we can actually update the container Itself by first of all creating a new version of our Docker container Docker image pushing it again to Docker Hub once we push the docker Hub we just updated our deployment file and then once we have done this and apply that we are able to directly get the response that we need and we are able directly have the latest version of our kubernetes instance uh sorry our application instance inside our kubernetes service so I hope this video was helpful please like share and subscribe if you like this video it will really help the channel as well if you'd like to support me please consider supporting me on patreon or buying me a coffee thank you very much and have a great day
Info
Channel: Mohamad Lawand
Views: 18,680
Rating: undefined out of 5
Keywords: .net 7, .net, api, beginner guide, step by step, csharp, c#, dotnet, code with me, coding, asp.net core tutorial, rest api, .net core, asp.net core, docker compose, docker explained, dotnet docker, dotnet dockerfile, dotnet docker tutorial, postgresql tutorial, postgresql, postgresql docker, kubernetes tutorial, kubernetes networking, deploy kubernetes, deploy kubernetes cluster, dotnet kubernetes, dotnet kubernetes client, c# docker kubernetes, dotnet core kubernetes
Id: x9NptrXA1dM
Channel Id: undefined
Length: 51min 37sec (3097 seconds)
Published: Thu Apr 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.