How to setup local development environment with Kubernetes and Skaffold

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there welcome to this video today in this video i'm going to show you how to set up your local development environment with kubernetes if you are using kubernetes in production for managing and deploying your micro services applications then you probably want to replicate that same environment on your local machine that way you avoid any last-minute surprises when you're trying to take your application to production [Music] very quickly i'm going to show you a quick demo of the kind of setup that we are aiming for i'll first show you really quick our code structure so in this this is our root folder okay it's setup say you're creating an e-commerce application and you're going to follow microservices architecture so you you're creating a separate service that is solely responsible for your products and the catalog and all of that kind of thing and then you have a separate service for cart which is responsible for let's say keeping track of users card and communicating with the database that is appropriate for the card and similarly you can have many more services but for the purpose of this demo i'm creating two very basic express applications and if i open the products file in the src open index.js you can see that i'm creating an express application and it is going to respond to this endpoint slash api products with the message hello world from product service and this application is listening on the port 3000. similarly if you go to the cart index.js you will see that this is a separate express application listening on port 3001 and it will respond when it when it sees this and point slash api slash cart with the response hello world from card service let's see this in action so so now as you can see i'm on this path shop.com api slash cart and when i hit that i'm getting this json response and if i go to slash products i'm getting this now what i'll do is i'll add some change here save the file and if i hit the refresh immediately you will see the file changes there and this is amazing when you consider the kind of complexity we have added to our setup with the whole containers and the kubernetes deployments and things like that and for this purpose we are going to use a tool called scaffold let me show you scaffold okay so scaffold is responsible for watching for your file changes syncing your code with the containers and all of that so in this video i'm going to take you step by step all the way from creating the application from scratch to configuring the right routing rules and setting up kubernetes clusters and and then finally setting up scaffold to watch for the file changes so i really hope that you find this video useful and if you do please do not forget to subscribe to my channel thank you okay so i just realized my resolution was not adjusted for the screen so i have just adjusted that and moving forward you should see my screen more clearly anyways so moving on before i get started i just want to make sure you have the right environment on your machine to follow along so the first thing i want to check is that you have docker desktop installed on your machine i'm personally going to use docker desktop so i'll be posting the link in the description if you don't already have it installed just visit that link and you will find that you have installations for all the different operating systems just find yours and follow the installation instructions now i already have it installed on my machine so on the top right corner as you can see there's a docker icon click on that and you'll see docker desktop is running kubernetes by default is not turned on so you'll have to go to the preferences on the left hand side you'll see kubernetes click on that and enable kubernetes once you apply and restart you will see that kubernetes running symbol there close that and then just to confirm you can go back to the terminal and just type docker once you type docker you should see all of these commands and that means your docker is installed correctly and to check if kubernetes engine is up and running you can just type in cube ctl get nodes and you'll see at least one node here docker desktop and it's in the status ready now the second thing i want you to check is if you don't already have an account with docker hub just visit this link hub.docker.com once again i'll post this in the description so visit this link and sign up for an account once you have an account on docker hub just go to your terminal and type docker login and if this is the first time that you are trying to log in you will be asked for your username and password just go ahead and provide those details and then you'll see login succeeded at this point your environment is all set up and ready to go next i'm going to walk you through all of the steps that we are going to take starting with creating two different applications products and cart we are going to write out docker image files for both of them and will create containers from these images so at this point we will test our application with the containers and if everything is successful we are going to push our image to docker hub once the images are pushed to docker hub we are ready to start writing out our kubernetes configuration files so we are going to write out configuration for deployments and these deployments are responsible for generating the number of pods that we specify so for the development purposes uh we can just create one part for each of our application and once these pods are created i mean they use the image from docker hub they pull the image and spin up the containers once these pods are running successfully we need to expose them to enable communication with our application so for that purpose we are going to configure something called as cluster ip service and they are responsible to enable the communication within the cluster so at this point if you were to spin up another container or another pod inside the cluster it will be able to make a request to our application and receive that json response but to make it available to the outside world we are going to create something called as ingress ingress unlike other objects is a controller which is not built in with kubernetes and we have to actually this is an open source project we have to install ingress in the cluster once we install it what we have installed is ingress controller similar to the deployment controllers and replication controllers they have some intelligence built into them but to add some rules and specification we still need to create ingress object so we are going to write out ingress file and that will and in that file we are going to specify all the path names and how the request is going to be redirected to those services so let's say if we have slash api slash products we want it to be redirected to our products application and if it is slash api slash cart we want it to be redirected to the cart application so this is where we are going to add all of the configuration when we hit that shop.com domain which by the way it doesn't belong to me i just add it in our host file and and this domain is actually mapped to localhost 127.0 points uh that's the ip address of localhost so it will reach out to that and that will get redirected to ingress controller ingress controller will then look for the path name and then it will redirect appropriately so this is the overview of the setup that we are going to do and outside of all this we are going to also set up scaffold and which will watch for all of these changes and accordingly take the decision to sync the code or rebuild the images all right so now that we have clarity on how we are going to approach the setup let's start diving into the code and look at all the exciting stuff so i switched back to the terminal and as you can see i just initiated a fresh directory and there's no code right now so we are going to start from complete scratch let's create a couple of directories products cart and then we are going to enter the products directory let's start with the products first i am going to initialize the project with npm init dash y and i'm going to add couple of dependencies like nodemon and express basic ones all right once these are installed i'm going to create another directory called src and this is where we are going to add our index.js file and i'm just going to paste the express application here as you can see it is targeting api slash products and it will listen on port 3000 all right very quickly i'm going to modify the package.json file to add another script start and when we hit npm start we want nodemon to start watching for this file okay so now if i do npm start as you can see we are on listening on port 3000 let's go back to the browser and api slash products so we are successfully getting this message from the service let's stop that and this is the same way we are going to set up cart so what i'm going to do is clear the screen and i'm going to copy everything from products to the cart folder and now if i see it into the cart folder as you can see everything got copied over we just have to modify a couple of things here so v i src index.js and here we'll be listening for the url api card and in response we'll send it back card service and will be listening on port 3001 okay perfect let's save that and now if i run npm start it is listening on port 3001 and let's just confirm it cart and yeah we are getting response successfully okay so at this point we have just completed step number one where we created both of these applications so let's go back to the terminal and close the connection and let's quickly take a look at the steps so next we are going to write out docker file let's do that real quickly clear the screen so i'm currently inside the cart folder and this is where i'm going to add docker file make sure it starts with capital d and there's no file extension so i'll paste all the code here and then we'll go through it step by step so what helps in imagining and visualizing these steps are think of when you have like a fresh linux machine okay to create any kind of applications you need to install certain environment and if you're creating let's say node application you would make sure that you have node installed on your machine and that is exactly what we are doing here so the first step is from this image node version 10 alpine so we are first making sure that our container has node installed on it then just the way we do on our laptops we create a fresh folder for any kind of project that's exactly what we are doing here we'll create another directory called app and we'll first copy our package.json and package log.json inside the app folder once we have that we can now install all of the dependencies so we just run npm install so in the next step we copy over all all of the other files and folders into this folder slash app the way we break down is like we could have run copy dot dot at the top but why we don't do that is because it takes a long amount of time for node modules to be copied over in inside the container and not just that whenever we are trying to run a node application or this docker container we always have to make sure that these dependencies are first installed on the host machine and only then we can create these containers so we we don't want that kind of dependency on the host machine so in this case what we do is we only copy these files and let it install from the web and then we copy over all the other files once we do that we just start our application with the same command that we have been using npm start okay so i'm going to save this and essentially this is the same file same exact file that we have to create in the products directory as well so let me go back to the products and save it and now we have in both of these applications we are going to quickly build images for both of these docker files that we just created so the command for that is docker build and dot dot is basically the path where the docker file is located so with dot we are just telling that docker file is in the current folder and if we hit enter you will see that the docker image is being built and once the image is built uh the name of the image is this kind of randomly generated unique id but since this is difficult for us to remember what we can do is we can tag these images so what we will do is dash t and the way we tag the convention is let's say if you're going to push this image to docker hub we we have to first use our docker hub user name as mine is amore codes you can replace that with yours and then the app name and don't forget the dot towards then that's the path to docker file and as you can see the this image is now also tagged with the name that i provided so oh actually i provided the wrong one i'll have to okay so as you can see now the tag and now this image is stacked with ammo codes slash product and uh to run this image what we are going to do is docker run amore codes slash products and as you can see the application is running but if you try to access the application from the web let me go to the products you will not be able to reach it because it is completely isolated once again so we need to map the port on my machine to the port on the container let me show you docker run dash p dash p is the flag that you provide for mapping the ports the port on my machine is 3000 and on the container it is listening on 3 000 so we do that and the name of the image press enter and now if you go to this url you will be able to get the response from the application now similarly if we go back to the cart cd card and if i give it this tag card dot it has successfully created this image and now if i run docker run dash p but if you remember for the cart it is listening on port 3001 so we are going to do that amore codes slash cart and now if we go to 3001 slash cart we get the successful response so at this point both of our docker images are perfectly fine and we can push them to docker hub so we are going to do docker push amore codes cart this is the first one i'm going to push and once this is pushed successfully then i'm going to push the next one that is amore code slash product i'm going to pause the video real quick here and i'll be back once this is done so a cart image is pushed to docker hub successfully let's do the same for our products so slash products all right so both of our images are pushed successfully and to confirm that we can go back to docker hub and just refresh and as you can see i have both of these images products and cart so so far we have been successful and let's quickly look at our steps so we have just finished pushing our image to docker hub at this point we can start writing out deployment files so let's get back to the terminal and what i'll do is for this particular app i'll go to the root of our application clear the screen as you see here we have these two micro services here so i'm going to add another folder here called k8s and cd into that so this is the folder where i'm going to add all of my configuration files so that it's all going to be in the same place so firstly we are going to write out deployment file for the products then we'll write out the deployment file for the cart and i'm going to name it as products deployment.yaml now there are two ways to generate a deployment file is one is you write out all of the specification in the file itself and the second one is using imperative method where you give a command and that will automatically generate a file for you so let me show you the other approach also so cubes cube cdl create deployment deploy is the shorthand for deployment and the name of this will be products and the image that we are going to use is the one that we just pushed amore code slash products and what we want is we don't want to right away create this deployment we want to first generate the file so we are going to dry run it and output the specification in yaml format and then copy that into products definition dot gml now if i open the products deployment products definition you can see all of this is generated for us so firstly i'm going to just clean up everything that we don't need all right so everything here should make sense now the kind is deployment metadata name products we can give it any name and when we actually inspect the environment which we will be able to then uh based on the name identify the kind of object that it is so name appropriately and the number of pods that we want to spin up for this purpose we just want one we'll give it one and the label that it will look for is the app products and this is the same one that we match on line 13 so line 13 is where we are defining the specification for the pod itself so from line number 10 all the way to the end is the specification for the pod and previously everything from nine and before is the specification for deployment okay so this deployment will look for the pods with the label app products okay and this is what we are going to give the same name to our pod as well and in the same file what i'm going to do is add the service as well if you remember we have to configure cluster ip service so i'm going to copy that here paste here and as you can see this is a product service and the selector is the main thing make sure this selector matches the selector that we have given to the pod so app products app products and if you remember the target port is 3000 so we just assign the port 3000 and the target port inside the container is 3000 let's save that and now if we create products definition.yaml you'll see that both service and deployment is created what i'm going to do quickly is just to show you that our app is currently accessible from within the cluster i'm going to create a very temporary pod and from that part we are going to try and make a request to our products service so the command for that is k run nginx we're going to create a pod with an image nginx and this rm flag is basically for telling that we just want this part to be temporary once we exit it needs to be deleted and then we are going to enter into the shell once the container is created and now you see that we are inside the container clear it and we are going to make a request to our products service on port 3000 slash api slash products oh sorry curl products service on port 3000 slash api products and as you can see we received the message successfully so at this point our application is accessible from within the cluster let's exit it as you can see this part is deleted similarly we have to write out the definition file for the cart as well it's going to be exactly the same as the products definition file okay so let's go through each line quickly uh all we are doing here is changing the name to cart replicas remain the same and match labels again we give all the pods related to this as cart label and the image we are going to use is amore codes slash cart then we also define the service for it and this service is meant to listen on port 3001 perfect now let's save this and once again we create from this definition file now if you inspect the environment okay get all you will see that we have both the services card service and product service each of them is of type cluster ip and these are the ip addresses one is listening on port 8000 and one is listening on 3001. we also have both the deployments card and products what deployment does is it spins up replica set and replica set is the one that is responsible for spinning out pods and the core function of replica set is to always make sure that whatever the number of replicas that we have defined in the specification is always maintained so if i were to delete this part it will detect that the desired number is one but the current is zero so it will immediately try to spin up another part to replace this one and we have the pods in the status running so at this point our application is running and it is accessible from within the cluster now let us take a quick look at all of the steps that we have done so far so we created our application we created docker file for it and pushed our image to docker hub then we also created the pods and the cluster ip to expose these pods the next step we need to configure ingress controller so as i told you before ingress is not something that's built into the kubernetes and it's an it's a third party project so we need to add it to a cluster i will be posting the link to this documentation for ingress nginx as you can see what we are going to install is nginx ingress controller now ingress controllers come in different flavors but the one that we are going to use is nginx one and under the installation guide you will see that we have docker for mac so just copy the whole command here and go back to the terminal and paste it just if you see the command cube ctl apply dash f and the path to the file so it's like just like one of the object files like definition files that we have been using so far so we can either use like cube ctl create dash f or cube ctl apply dash f and as you can see it has installed all of these different services some clusters some service account name space and all of that for ingress let's clear the screen and if i do this k get services across all namespaces you'll see that ingress has created its own namespace ingress nginx and this is where it has its own controller and service installed cluster ip load balancer so what it does it configures two types of services for us one is load balancer and one is cluster ib as i told you load balancer is something that is external to cluster and it remains outside of the cluster so as you can see here it is mapped to localhost and our cluster ip resides within the cluster okay so just clear this and now we are going to write out definition for ingress so in the same folder as our other k8 file we are going to create ingress.yaml so let us go back to our documentation and i'll post the link for this as well so you'll see the basic usage okay so just from here we're going to copy until this separator here because we are only going to configure one ingress service okay and just paste it here and there are some things that we need to change obviously so the name first we're going to change it from ingress my service to ingress shop and the host we're going to give it shop.com this is the domain and i'm going to add this domain uh to our etc host file okay and the backend so for the path we're going to give it like api products and this should be mapped to the service name products dash service as you can remember the service port for this one was 3000 okay and as you can see paths is basically an array so we can add multiple paths to it and so we are going to copy this paste it here and i'm going to change this one to cart so whenever we go to slash api slash cart we want it to be redirected to cart service and on port 3001 that is all we are giving here and save the file and then if we do k create ingress.yaml it is right now created so we still need to add that one thing like shop.com to our etc host file so in my case i have to do sudo because i need to i can only change this file as a super user sudo bi etc host this is the path provide the password and as you can see here 127.0.0.1 i've already added this domain shop.com now what happens is like you don't really have to own this domain i can even change it to something like google.com and it will work so the way it works is basically when you type shop.com it first checks in your etc host file if it doesn't find the domain year then it will reach out to the external dns server which is like the global dns server which will then resolve it to the correct ip address similarly if i were to add google.com it wouldn't go to the actual google.com it will check for the ipa of google.com in this file if the entry is there it will just look for the server on this ip address all right so now just save the file and now if we go back to our browser and type in shop.com api slash products as you can see we are getting successful response and if we do the same thing for the cart hello world from cart service as you can imagine this already makes our life so much easier because our front end doesn't need to know what port it is on whether we are calling two different back-end services or what not all of that is abstracted away from your front end all it knows is it has to um reach out to this domain shop.com api and slash whatever it wants products card whatever perfect so we have reached this step where we have configured our ingress successfully now the only thing we are left to add is the scaffold tooling which will watch for the file changes because currently if you make any changes they will not be reflected in the browser so very quickly what i'm going to do is i'm going to go outside of this folder into the root of my application so i'm at this room and i'm going to first look at installation of scaffold okay so once again this link i will be providing in the description and under the docs install you can see that you have installation instructions for all the different operating systems so for me it's mac os and i have already installed using brew install scaffold please go ahead and install this and once you have it installed i can just confirm it by typing scaffold in the terminal and if you see all of these commands that means it's installed successfully and once you have that let's clear the screen in the root of our application just type in scaffold init and then you will be asked certain questions so choose the builder to build image so amore cards codes slash cart so of course we are going to use docker file which is located inside the cart press enter now it will ask to choose the builder for this image amore code slash products once again we have products docker file click enter and it has generated a file for us once we give it yes enter if you see now we have scaffold.yaml okay so clear the screen open this file and you can refer to all of the steps that are mentioned over here in this api references okay so as like with anything in kubernetes we the first line is always api version so make sure it is copied from here the kind we are going to use is config metadata name and identifier for the project so i'm going to change the name so just do like shop scaffold all right now if we go to the build artifacts we can see that image is correctly mentioned here because it asked us for that and we can give it the context so context in this case is going to be cart so it will look for the docker file inside the cart folder okay and if you see here we have this option of sync so local files sync to the pods instead of triggering an image built when modified if no files are listed sync all the files and infer the destination so this looks like an option that we want to use for watching and keeping our code changes in sync so i'm going to go under context and i'm going to add sync let's see what's next manual manual sync rules indicating the source and destination perfect i want that and you have to be very careful when it comes to yaml the indentations are very important if so far you have had any errors please make sure that your yaml files are done correctly and i'm going to push all of this to github so if you face any issues you can just clone from there and use that okay so under manual we have to define src and destination so once again it's an array so you do that src and we are going to go into src star star star dot js so it will watch for all of the javascript files under the source folder inside cart directory okay and the destination we're going to give it dot what that means is in the inside the container if you remember we created slash app folder right so it's going to just take these updated files from here and copy it in that directory all right so we're going to do the same thing for the products as well sync manual src src star star star dot js and destination will be current folder okay save that and now if you run scaffold dev you will see that it will first kill all of the pods that we had already running in the background it will kill that and it will try to restart all of them so it will try to restart your service the deployment and everything so that it can watch for any changes across the cluster this will take a while so let's take a break and i will come back once this is done so welcome back if you guys are seeing this error like i am senior don't worry about it it happens the first time so once again just try it scaffold dev and this time around i'm sure that will work out as you can see this time around it also um built faster and then you'll see like products and cart both of these pods are being watched and the logs are being locked over here so for testing this now i'll go back to let's let me open another tab here um and just to confirm that everything is running so far uh so i'm on the card service let me change something there so cd cart visrc index.js and i'm going to modify the message so chain nothing to see here save that save the file and if you refresh the browser now nothing to see here so sometimes it does take time for it to build because it does go through some changes and then copies it inside the container but let me show you again how quickly it goes through once again let's open this file and this time around once again hello from cart okay let's save this and quickly we'll change the tab and see what happens in the background as you can see it's rebuilding the image but it just builds it locally so it doesn't actually push it to docker hub and it stabilized in two seconds and now if you refresh this you'll see that the the changes are pretty fast so that's all we have the setup here and i hope this video helped you set up your environment locally if you have any questions if you face any troubles any issues please feel free to drop in the comments and what i'm also going to do is i'll push all of this code to github and add the link in the description so you can always go to github and clone all of these files stay safe and keep learning bye
Info
Channel: The FrontOps Guy
Views: 3,811
Rating: 4.9058824 out of 5
Keywords: kubernetes, skaffold, docker, local development environment, nodejs, cloud native
Id: 9A3paYRHvDQ
Channel Id: undefined
Length: 41min 44sec (2504 seconds)
Published: Sun Aug 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.