Microservices in Python using Flask Framework | Dockerize and Deploy to Kubernetes with Helm

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] [Music] [Applause] hello guys this is vikram and welcome to my channel in this video we will see how to write a micro service in python and that too using flask as the web development framework so we are going to start from very scratch right from installing python in our windows system and writing the python script and then we will move on to writing the docker file for this python based application we'll also see how to build this docker image and then run it using docker compose and then we'll create various kubernetes manifest files like deployment and service object for this application followed by creating the helm chart so we have already seen this kind of application in our kubernetes and docker demos but this time we'll be doing everything from scratch right from installing python in our windows system so these are the various parts of the complete demo we'll start off with installing python in our pc and then we'll see what are python virtual environments and why these are needed and then once these are installed we'll be installing various extensions for python in vs code and then we'll start writing the python based application or python web application using flask we are going to follow the official documentation by taking the few examples we are not going to make a huge web application so we will just create few endpoints and see how to dockerize and also we will see how to run them locally and then we will see how to write dynamic web pages using zinja templating so we'll be creating index.html files and see how to give dynamic information to those files and we'll also see how to freeze your python dependencies using pip and then once everything is running properly in our local we are going to write the docker file for this application and then build it we are also going to see how to write the docker compass file from scratch and then deploy it into the docker and finally we will move on to writing the kubernetes manifest files like the deployment and service object and then we'll see how to create helm chart out of this particular manifest files so the prerequisites are we need python 3.10 along with pip install we would also need docker and docker compose installed because we would be building the images and running them as a docker container first before moving into kubernetes at the same time we also need kubernetes clusters like mini cube cluster and utilities like cubectl and helmcli and for writing the source code uh i prefer vs code but you can also use notepad or notepad plus plus so what are web frameworks a web framework is a code library so that makes it easier for us to develop web applications by providing common patterns for building reliable scalable and maintainable web applications so these are nothing but a code libraries that makes or that provides various patterns which we can reuse to build highly scalable web applications in python some of the popular frameworks include zango flask and fast api so for this demo we will be using flask but in the coming videos i am planning to use fast api as well a framework a framework will provide various functionalities in the source code or through extensions that it makes it easy to perform various common operations required to run web applications the common operations like the url routing depending upon various url parameters and then input form validation and handling and xml html and json output format with the templating engine we can also have database connections with object relational mapper which we call it as orms we can also have web security and session controls everything baked into web frameworks so we will be seeing uh quite a few of these operations in flask based framework so now let us start with installing various tools that are required to build the web application so first i'm going to open my terminal and then see if python is installed or not by running python hyphen iphone iphone version so in my case python is not installed so what i will be doing is the first step is before installing python i would be creating a github repository and then will clone it and then start from there so i'm in my github page i'm going to create a new repository called microservices in python so i am going to initialize it with a readme file and then create a repository so first i will be cloning this repository so that we'll take it from here so i'm going to go to the downloads directory and then use git clone to clone this particular repository and then i'll cd into this directory and then use this particular folder and i'll open this folder in the vs code so the prerequisite is you need to have the vs code installed so doing code space dot will open my particular application inside the vs code so first thing which i'll do is first i'll create a file known as okay so we already have this readme.md file so we'll start modifying this so first i am going to write each and every step that i am going to create as a part of this example so first thing is we will be installing python so already we have mentioned or listed out every step here so i'm going to copy everything from this presentation and i'll copy it here so the first thing is which we are going to see is installing python 3.10 which is the latest version and then we'll start to understand what are python virtual environments and why these are needed and then we'll create or we'll install various python plugins in the vs code followed by creating a sample flask based application and then once this is done and running inside our local machine we are going to see how to generate dynamic web pages using zinza templating engine and then once everything is running we are going to use pip to freeze the python dependencies so we will see what all python dependencies are needed for this web application and then once everything is available we are going to start the docker file for this complete application followed by writing the docker compose file so in every step we are going to test it locally by running the docker container either through docker run command or through the docker compose command and at the end we are going to see how to write the manifest files for the deployment object in the service object followed by creating the complete health chart itself so let us start with the first step which is downloading and installing python so i'll head over to official documentation of python so it is python download if you search it so the first link will be the official link so all you have to do is to click on this button to download python so in my case i've already downloaded it so it is in my downloads folder so what i'll do is i'll double click on the exe file and then i'll also check this add python 3.10 to path and click on customize installation so i'll select all of these click on next and i'll also check this install for all users and then click on install so this is going to take some time so python installation is successful i'm going to close this dialog box so i'll open a terminal so python won't be detected in the current terminal so i'm going to close this and i'll reopen the new terminal and then you can type python hyphen iphone version to see the latest version of python installed so it is python 3.10 you can also see what is the pip version so pp is a package manager for all the python based modules so p version is 21.2.3 so this these are the different versions of python np so i'm going to close this dialog box and now the first so now the second step is to understand what is python virtual environment so with the python so when you install python for the first time python is going to install in the c drive programs files under python 310 folder which is python 3.10 folder so this is where python is going to store all the libraries that we are going to download through pip but imagine we have two different applications that require two different sort of uh dependencies let's say one application needs yaml 1.0 as the dependency another application needs yaml 1.1 as the dependency but the problem is we cannot have two different versions of a library in in the same system so if you are working on two different projects so you should make sure that both the projects either satisfy common library or you have to go back and forth by uninstalling a particular library and then installing a new version so depending upon the requirement so in order to avoid this so what we are going to do is we are going to create virtual environments for each and every application so these virtual environments like are our containers so as containers are isolated from each other these virtual environments are also isolated from each other so in order to learn more about the python virtual environments and how to create one so you can type python virtual environment so this is going to take us to the official documentation of the virtual environments so the in in the introduction part they've explained it very clearly python applications will often use packages and modules that don't come as a part of standard library so with your python installation you will get only a few packages that are coming as a standard one but some application needs specific version of a library because the application may require that a particular bug has been fixed or the application may be returned using an obsolete version so this is some sort of introduction so in the next paragraph they have explained it that if a particular application a needs version 1.0 and if there is another application b that needs 2.2 then the requirements are in conflict that means that either you install 1.0 and work with application a or you install 2.0 and work with application b but at the same time you cannot have 1.0 and 2.0 as the library dependencies so this is where our virtual environments comes into the picture so virtual environment is nothing but a self-contained directory so it will create a directory and inside that you can put your own dependencies so we can create an n number of virtual environments and these environments are completely isolated from each other so for every application we create a virtual environment and we put all the dependencies in that virtual environment since no two virtual environments will collide with each other so we can have different versions of applications and their dependencies running inside the same machine so in order to create a virtual environment and which i highly suggest that we should do so we should use this python hyphen name module hyphenum stands for the module over here and the model name is venv and this is the folder that is going to be created that is the virtual environment folder so i'll take this command over here and then i'll go to the vs code the first step that i'm going to do is so creating this virtual environment i'm going to open the terminal so so here we are inside the micro services in python folder i'm just going to paste this but the problem is uh we don't have python 3 binary it is only python so i can just use python hyphen m virtually and with tutorial env so this is going to create a particular directory so if you try to open this directory you can see that we have include library scripts so this is exactly like your python installation that means for every virtual environment that you create you're automatically creating a separate python installation instance for this particular application that means you can have n number of these virtual environments and these isolate these environments are completely isolated from each other so now i have the virtual environment so i need to activate it so for activating we have the activate dot b80 and deactivate.bat inside the scripts folder so i'll go to the official documentation and i'll see how to activate this so all you have to do is inside this for folder that we have just created so we have the scripts directory and inside that we have activate dot bat file so i'll just copy this command over from there and i'll paste it here so this is going to activate my virtual environment so how to identify whether virtual environment is activated or not so it is through this prompt over here so earlier we didn't have this prompt and now we have this prompt that means that we are inside the virtual environment so inside this virtual environment also we have python so if you just type python hyphen iphone version so you can also see that it is basically the same version so that we have installed in the machine so basically it is just like cloning the existing installation and putting it in an isolated environment so this is the advantage now i can work with this particular application inside this virtual environment by creating or you know installing any sort of dependencies and these dependencies are completely isolated from your main python installation so for now we have just activated the python virtual environment and this is for the windows for unique or mac os so you can use this source command so that's it so we have just activated the uh virtual environment and then you can also install any sort of dependency using pip install and the dependency name along with the dependency version so we will see about all these installations in the coming video so in this video so we are going to see all these in this video so i can also do pip list so to see all the dependencies inside this virtual environment so if you see we only get the default ones so it is taking some time so we only have these default ones which are pip pen setup tools so now let us go to the next step uh which is installing python vs code extension so so vs code will also have various python uh plugins so we if we activate them so they will give a lot of other features like autocompletion etc so i'll go to this extension tab over here and then i'll select python so i'll search for python and then click on this first which is from microsoft itself and then click on install so python extension is enabled i'm going to close that window so the next thing is we need to set up the python interpreter which we will see in the in some time now what we'll do is we'll go to the next step which is creating the sample flask application so we are going to see it from the official documentation itself so i'm just going to type python flask python so this will take me to the official flask documentation so this is the official flash documentation so you can click on this user guide directly so this will take you to the user guide if you want to learn more about the flask so which is nothing but a framework for developing the web applications and you can also click on the python version that is needed so basically flash supports anything more than python 3.6 and also we need flask as the dependency to work with python flask and we also need virtual environments which we have just created so it is not needed if you are working on only single project but if you have multiple project that requires multiple dependencies so it is always suggested to go for the virtual environments and then how to install flask so it is through pip install flask so i'm going to take this command and then going to run it inside the vs code if you see here we already activated the virtual environment so i'm just going to paste this command which is pip install flask with fs the capital letter so this is going to install flask and along with the other dependencies like markups uh safe zinja 2 etc so now if you want to see what all dependencies installed you can just do pip list so this is going to show me everything so as you see along with the flask we also have other dependencies automatically downloaded which are ginza to markup safe so rest of the other things so if you are not sure what is the use of this virtual environment now so what i'll do is i'll open the terminal and do the same command which is pip list so now we are using the systems python interpreter or the installation so if you see so we don't have flask here the reason is we have created a virtual environment and inside that virtual environment we are using pip to install the flask dependency so for now we are using only uh we have installed these dependencies only in the virtual environment so similarly i can create a lot of virtual environments and i can you know install various dependencies inside that virtual environment so now virtual environment creation is also done and installing flask is also done so now let us first write a small python application and see whether python is working or not so i am going to create a new folder over here and one more thing is before um creating this new folder so i wanted to create dot git ignore because i don't want to push any of these tutorial env folder which is nothing but the virtual environment to github so i'm going to just write the folder name tutorial env so this will be excluded when i'm trying to push my changes into github so this is the first um you know entry in the kit ignore and now what i'll do is i'll create a new folder called src and inside this src i'm going to create a file known as app.py so app.py so now i'm going to test the python installation by just typing a simple print statement which is hello python and i'm going to save this and now in order to run it you can all you can click on this button over here and click on run python file so if you see here so your python is being run inside the virtual environment if you see the interpreter that is being invoked is not your system interpreter which is inside your c drive program files the interpreter is from the virtual environment that we have just created so that means that this python.exe file is from the script directory of the tutorial env so this is where your uh this is the python.exe that your system is currently using so now we are executing uh we have executed the first python program so now we will go to the official documentation of flask and see how to create a simple web application or a restful api so we have installed now we are going to [Music] go to the the main page and then we will see a minimal application so this is a minimal application of last and you can see we are first importing the class flask class using from flask import flask and then we are instantiating an object and then we are creating various routes so what i'll do is i'm going to just copy this complete source code and then paste it in the i'll replace the print statement and i'll play paste it here so from flask we are importing the flask class and then we are instantiating an object known as app and then this is our first route so the routes are it is these are the http routes so whenever you access flash based application on slash so it is going to invoke this so it is going to invoke this hello world as the function and then it is going to return this html object so which which is nothing but hello world so it is also mentioned clearly in the documentation also so first we import the flash class and then instantiate this class with an object and then we create a route so the route is nothing but a particular decorator so that will dynamically change the function over here so we'll then use route decorated to tell flask what url should trigger this function so in my case so the url slash will trigger this particular hello world function so in order to run this so there are multiple ways so first thing is uh you know if you are working on windows you should select the cmd and what we are doing is we are selecting the flask app as hello because here uh the file name that they have used is uh hello dot py so that's the reason why they are using hello so they have saved it as hello dot py so that's why they are setting this flash cap environment variable as hello and then simply doing flash current flask will automatically take this environment variable and run this python file so this is not needed uh for every use case so what we can do is we can simply run it using python so i'm i should go into the src directory so cd into the src directory so here we have sorry ls won't work so here we have app.py so i'm going to run it using python app dot py so it is going to give me hello world sorry i have not saved this file so that's why it was giving me earlier print statement now we can run using python app.py but before doing that so let us see where on which port the python flask generally runs so by default python flask runs on 5000 as the port so you can also change it to some other port so if you want to know how to do it so we can just search for flask running on different port so you can take any link so this is how you to run the flask on a different port so i'm going to take this command and then going to paste it here so this is in if condition so that sees whether you're running this file as it is or you're importing it so if name is equal to main that means that if you are running this python file directly then only app run will work so this app run it is now running on the same local host and it is using 80 as the port so default it is 5000 so you can also change it to some other port so right now i'm using it as 5000 itself so now in order to run this python file all you have to do is python app.py so this is going to start my server and this is the url so the url is so the ipaddress of my machine colon 5000 it is the port on which python flask application is listening on so we have the first endpoint so that is slash so which is just displaying hello world so if you see the type of the response so all you have to do is just right click on this uh page and then select the network and you have to refresh this page so this is the response so if you click on this response or the headers information you can see that the response is of type text html but generally all the web applications um you know give json as the output so that it can uh so that other uh microservices can read that json output so it is difficult to parse this html output so what we'll do in the coming video so we are going to change this response type to the json because the response type is now text slash html so i'm going to go back to the obvious code now let us try to create one more endpoint called slash health so this slash health is pretty much needed for all the micro services because so using slash health we can only detect or you know using slash slash health we can detect whether the application is up and running successfully so in the readiness and liveness probes of the kubernetes so we can use this end point to see whether your particular container is alive or dead so what i'm going to do is i'm going to create one more route here so the route is for the slash health so for slash health also i'm going to create one more a function i'm going to create a function known as health and then here what i'm going to do is i'm going to return a json response so there are many ways um you know you can convert a string into a json but uh in case of python we have some class known as jsonify so using jsonify we can convert any any object into a json object so all i have to do is i have to call jsonify and then uh for its function i can pass a key value pair in the form of key equal to value so in this case what i'm doing i'm going to use status as the key and then value in terms of string it is up so now this particular health endpoint if someone reaches this particular application on slash health it is going to response with this status is equal to up and that too as a json object so since i made the changes so i'm going to save this file and i'll reload the server by clicking on control c to stop the server and again i'll do python app.py it is going to restart my server so it is the same uh ip address so if you do refresh since we are hitting the slash as the endpoint we are going to get the hello world but if you do slash health as the end point so it is going to send me a json response see if you see from the source code it is status is equal to up but in the browser it is status column up so this is a json value so if you are not sure just right click and select inspect go to the network tab and then refresh this particular page now click on this health endpoint over here now if you see the response address the content is application slash json you can also see the response which is this same status is equal to up so this is how you return json responses using the jsonify so which is a particular um you know we are importing this uh function from the flask so now health endpoint is created so let us now try to create static web pages so what i'll do is i'll go to the official documentation since we are done with running the basic web server or basic um you know we have created the basic api endpoints so by default it uh the flask application runs on 5000 you can also change it to 80 over here so the web server listens on port 80 so now let us go to the rest of the documentation to see if we can do some more things so this is how we do the routing so here we have the routing for slash hello and here they are just returning a string as the response so in the in the next demo in the coming demo we will see how to serve the static file and uh the flask will support various http methods so by default if you are not mentioning any methods it is going to be get but if you have any methods like post port delete you can also use them by uh mentioning the method in the array like this so here this particular route will trigger for both get and post api calls but in order to do the differentiation between get and post so here we have created an if else statement if the request dot method is equal to post then it is going to return this particular information but if it is a get it is going to return this particular information so here this is a function call and here this is one more function call so let us see now how to see uh serve the static files or the uh html files basically flask uses jinja 2 as the templating engine so we can have dynamic web pages created using ginza 2 templating so all you have to do is you have to create a folder structure like inside the source directory we have to create one more folder known as templates and inside this template you can put your html file so what we will see now is we are going to create a static index dot html page so inside this templates folder and then we will see how to modify it or templatize with ginza to templating so i'll go to the vs code and then um inside the src folder i'm going to create one more folder i'm going to name this folder as templates so inside these templates i'm going to create one more file known as index.html so i'm going to write some html code so basically i'll start from html tag and then we will close it so and inside the head section so i'm going to give the title so the title is um i'm just writing dockerizing a python web application so i'm going to close this title tag and also i'm going to close this head tag now inside the body i'm going to write some static code so there is static code is i'm going to use the h1 as the heading tank and then um i'll just write simple web application using python flask framework so this is the first error value so let's see how to dynamically change this since we are we now have the html static file so how to render this so there there are various ways we can render this so in the documentation it is said that so we have to import this particular function called render template so we have to include this as well render template and then for rendering what i'll do is um i'm going to create one more endpoint known as amp dot route so the route i'm just going to say it as details so slash details and then i'm going to define a function so called details function and here i have to exactly copy this written render template statement so i'm going to copy this go to the vs code and then paste it so here i am rendering the particular template so it will automatically detects uh the file from the templates directory but so in my case the inside the template directory the index file is the html file is index.html i'm going to change its name and remove the rest of the statements from inside so i'm just saving this file and then i'll stop this server and then restart it so this time what i'll do is i'll go to an end point known as details so this is going to serve me a static html page which is simple web application using python flask framework but so far we have been serving the uh static page so what if i wanted to solve the dynamic content so i can also modify this index.html with certain placeholders so these placeholders will get the values from the main app.py file at the rendering state so what i'll do is for rendering so i just wanted to show the host name and ip address of the particular uh the machine which is running this python application so i have to write a logic uh to fetch all the details of the host name and ip of this particular machine where my application will be running so inside the docker or kubernetes it is going to be the container hostname and the container ip address so what i'm going to do is i'm going to create a one more function called the function call called the fetch details so naming is up to you and then i i'm going to return two values from this particular function call so which is nothing but your uh host name and then i'm going to return the ip address of the particular um you know instance but here i don't have them so what we we have to do is we have to have a logic for fetching the hostname and ip address of this particular container so what i will do is so i am going to go to web and i'll search for it so hostname and ip using flask so there will be plenty of you know website that are going to give you that information so we are going to select uh one such website so we can use stack overflow or any other website so this is the ipads of visitors so it is not this one which hostname and ip using it's not flask basically it is in python because so there is nothing like uh it is different from yeah so we are going to take this logic hostname and before that we need to import this particular library called socket so i am going to import this using import keyword import socket and then i'm going to fetch the hostname and ip by using these two commands i'm going to paste them so i'll explain in a while so here how we are doing is we are fetching the host name by calling the socket uh and then we are calling one more function inside it called get hostname so this is going to give me the hostname and the host ip it is through the same command but instead of get host name it is get hostnet get host by name so that means it is going to get the ip address so as an input i have to pass the host name so here host name and ip address are available now so it is up to us to print them in the console or not so i'm going to remove both these print statements so now i'm going to return both these hostname and host ip back to the caller who is calling this particular function so if you want to add some comments you can add them like this so hash is for the commenting function to fetch hostname and ip of the house name and ip uh so wherever this program runs it is going to fetch those details so i am going to return hostname and host ip so i wanted to return them as a string so i'm going to use this data conversion so i'm going to convert them into different data type called string so now i have this host name and host ip so i can dynamically show them whenever a user hits this class details so what i'll do is so what i'll do is i'll go to the index.html file and i'll going to create one more tag history tag here i'll create one more a particular tag so this time what i'll write is response from so uh we have to create placeholders so i'm going to give it a name hostname so whenever i have this variable hostname passed to this index.html so this particular variable is going to get replaced so response from host with ip address and then i'm just typing it as ip so it should be double braces and spaces should be there so so this is about writing the templating in ginza so the dynamic response will be response from host this particular host name with ip address this particular ip so i have to pass the values to this host name and ip so how will i pass is so i'm going to pass it in the render template so i have to pass the host name is equal to something which is question mark and iep is equal to something which is a question mark so those ip and uh the host uh hostname and ib information are available here so i am going to call this fetch details function just before the rendering fetch details since it is going to return me two values hostname and ip so i am going to cache them here so this is called tuple unpacking so host a name comma ip is equal to fetch details so this host name has to be sent to the index.html so this is the actual value so hostname is equal to hostname so don't get confused over here this host name is the this host name or else what i'll do is i'll write a hostname in caps over here okay and i pin caps over here and then so this becomes your host name which is capital and i'm assigning a value which is this and also for ip i'm assigning a value small ip so this is how you render the dynamic uh template so i'm going to start this error once again so now i'll go to the slash details and i'm going to refresh it so if you see here it is saying response from host so this is the host name with the ip address of this since i am running only one instance so you are going to get this the same instance suppose in case of kubernetes you are running two different replicas using the deployment object so you are going to get various uh you know dynamic responses from this api endpoint so now we are done almost done with creating the endpoint so we have the route which is a hello world and then we have the health route and we have the details route so now our application is also running properly so now the first thing uh uh the next thing that we are going to do is so we are going to use pip to freeze the dependencies because we would like to know what all the dependencies we have used so far inside this virtual environment to create this application so using pip freeze i can freeze the dependencies or get all the dependencies from this particular virtual environment and i am going to redirect it to requirements.txt file so this is the file actually uh pip needs to install all the dependencies from a file so if i just redirect these dependencies to a different file and if you open it so basically these are all the dependencies so that we have used for this application development so uh currently it is under the src so what i'll do is i'll bring it outside the main folder so this is the requirement.txt let's say if someone wants to run your application what they have to do is they have to use pip install hyphen r and then if you give the file name which is requirements.txt file so it is going to download all the dependencies which are mentioned in that particular file so if you are not sure you can always go to the virtual environment uh official documentation and see how they are freezing the requirements and then you can use ppinstall hyphen requirements.txt to install all the dependencies so so far we have seen how to run or how to create a simple web application and run it locally so now what we'll do is we'll try to create a docker file and see how to dockerize it so for this i need to have the havoc cluster which which should be your mini cube cluster so i'm going to go one step back to the main directory and now uh since mini cube is already installed and i have cube ctel and helm install so i'm going to use mini cube start to start the mini cube cluster so my mini cube cluster is up and running and my cube ctel is also configured to use the minicube cluster so now if i just uh do docker so i'm going to get some output okay so now what i'll do is i'll just see what are all the running containers so by running the docker ps command so if you see uh i'm not able to connect to the docker daemon which is basically running inside the mini cube so the way i can do is i can just get all the environment variables of docker inside mini cube by running mini cube docker env so i can uh set all these environment variables i i'll copy all the first four and then i will use them i will set all the environment variables so now if i do docker ps i am going to get the various containers which are currently running inside the mini cube cluster so now what we'll do is we'll start to write a simple docker file we are going to close everything so inside the readme so we are at this step writing a simple docker file so on the main directory i am going to create a new file called docker file and also when i uh when you're trying to build it i don't want this tutorial env to go to the daemon during the docker build stage so i want to exclude it um from going um uh from the docker context so what i'll do is uh i'll create a new file known as talker ignore so this will ignore any files that i input in this file during the docker build stage so i'm going to input the same tutorial hyphen envy so so this won't be sent to ascend as the docker build context so tutorial env so now let us go to the docker file so first and foremost thing that we will do is uh selecting the base image so i'll go to the docker hub and see if base image python is available so it is there but i wanted to select a proper tag so i'm going to search for python click on this and see various tags so i can use the latest one this is 338 mb but this is not needed for me so what i'll do is i'll select a smaller one called the alpine version and alpine uh i'll take something with 3.10 as the tag and so you can take 3.10 hyphen alpine so it is just 16 mb so i'm going to take this as the base image because i don't need everything that comes in the uh the latest version of python so i'm going to take this as the base image of the docker so base image for this application and now what i have to do is i have to build this image so first thing that i have to do is i have to copy this requirements.txt so that people install all the dependencies so i can use copy statement copy requirements.txt into the directory so inside the image it is not but before that so i wanted to set some a working directory so i use work dir as slash app so it is going to copy this requirements.txt into this particular app directory so now what i have to do i have to use run pip install uh ppinstall hyphen r and then requirements.txt so that will read all the dependencies so if you open this requirements.txt these are all the dependencies that we are needed so i'm going to install all the dependencies and then i'm going to copy the source code which is src in our case and then i'm going to copy into the src directory so it will be copying into slash app slash src because we have set the working day directory as slash app so once this is done so i have to have the entry point created so as an entry point what i have to do i have to run my python application so i'm going to use the executive executable form so python followed by the name of the file which i wanted to run so i wanted to run um inside the src that is dot uh slash src slash so we have app dot a py file so this is the file that i'm going to run and i'm also going to use expose just for documentation so my particular container is running on port 5000 and i will also want to include the health checks because if you see the original source code i have created a health endpoint called slash health so i'm going to add this in the health check so if you just type health check it is going to give you all the uh various flags like uh the interval 30 seconds time or 30 seconds start period 5 seconds retrace 3 so interval 30 seconds means uh every 30 seconds this health check command is going to run and timeout is it is going to wait for 30 seconds so within 30 seconds if it is not going to get a response from the application so it is going to mark it as failed start probe is 5 seconds so this is the initial delay that we want to wait so before help check commands are run so i am changing it to 30 seconds and retries has 3 i'm going to increase it as 5 so after 5 failed request this health check gets fail and the container is marked as unhealthy so from for the command what i'll do is i'll simply use curl and then hyphen f which is a fail flag and then on the local host okay i'm going to break it into two lines by giving slash at the end and then command curl hyphen f http localhost and on the the container is running on port 5000 and the health endpoint is slash health and or exit one so this is going to give me the exit one as the response for the health check if the curl to this particular endpoint fails so this is uh uh this is how you write the docker file so now what i'm going to do is i'm going to build this docker file so i'll use docker build hyphen t so i'm going to just name it as uh web app colon 1.0 and dot to indicate the location of docker file so this is going to download the base image and then it is going to install all the dependencies and then build the final image so now everything is done so image is successfully tied so in order to run it what we'll do is we'll use uh docker run command so it is docker run hyphen d support mapping i'm going to map 80 on the host machine and the container board has 5000 and the container i wanted to name it as a web and the image name is web app colon 1.0 tag so this is started so if you just do docker ps so you are going to see this os container sorry web container so now i'll go to the um you know a browser and see the output so but before that i need to get the ip address of mini cube by running mini cube ip but if you are running the cluster you should you can also take the masters ip address in my case masters ipad sees this so i'm going to take this ip address and since my application is already running on 80 because i used port mapping so i can simply run this ip address so this is the slash output slash endpoint output if you see the slash health this is going to respond with the health endpoint so application is up and running and i can also do the details so this time it is going to give me a different value so this is the host name or the container id and this is the internal ip address of the container so if you keep refreshing you are going to get the same value because there is only one container for your application and also if you do docker ps and the web okay sorry docker ps so you can see that the container healthy state uh starting so that means health has not updated yet so if you check after one or two minutes so this container is going to be marked as healthy so this is it so i'm going to remove this running container using docker rm minus f and the name of the container is web so now what we we are going to do is we are going to see how to write a docker compass file for deploying this application using docker compose so i am going to create one more file so name it as docker hyphen compose dot yml so this is the standard name so what i'll do is i'll start uh with the basic template so you can go to the web and then search for docker compose so click on the first link so this is docker compose i'm going to take this particular template and i am going to copy the entire template over here so the version of the docker compass is currently 3.9 and this is the service so we are making one service so i am going to delete this volumes and links because there are no volumes and links the other image and the volume so i'm just starting with the bare minimum one so this is the web which is the container the service name so what we'll do is so we are going to take this and extend this so it is up to you to build the image inside the docker compose as well or directly refer the image so let's see how to build it so inside the build you are just giving dot that means finding the docker file in the current working directory at the same time you can also use context so this is also the same thing context column um the dot that means finding the uh docker file in the current directory so if the docker file is named properly with the same name called docker file with capital d we don't have to give the name so if it is the docker file is named with a different file like the my docker file or docker file iphone dev so in that case you have to mention the docker file name as well or else even if the name is docker file you can also use this particular statement and then i wanted to change the image name so once this is built i wanted to name it as web app so earlier it was 1.0 now i'm going to use web 2.0 and coming to the port information the container port is 5000 but on the host mission i wanted to use it as 80 and i'll also put the restart statement so the restart i always wanted to use always so even if the container of fails it will automatically restart so it is up to you to use networks that means that you know putting this particular container in a particular network like webnet that means that if you wanted to put it in a custom network so you have to create it first so i'll use a network section over here and and then use webnet so do sure you indent it properly so you created the network called webnet and you are referring it here so this is the complete uh docker compass file so we have the images port restart networks yeah so now first thing what we do is we try to build this particular image using this build instructions inside the docker compass so what you can do is you can just use stalker compose and an option known as build so this is going to build your docker image if you have the docker file in the current working directory so this is just building it and it is going to tag it with web app 2.0 see it is tagged now we can also use docker compass up minus t so this is going to bring up your complete stack so the network is created and then the container is started so if you just do docker ps so you should be seeing that particular container up and running so it is the same process again so we will go to the we'll take the ip address of the mini cube so i'm going to take it one more time mini cube ip so take this ip enter and slash so this is the slash endpoint so we have the slash health which is the health information and we have the details so this is also same thing so this is how you deploy a web application using docker compose so now i'm going to bring down the complete stack by running docker compose down so this will bring the complete docker compost by removing the container first so it is currently stopping the container and then network is also removed so this is about docker compose so now docker composition so let us now move to writing the kubernetes deployment and the service object files so what i'll do is i'll just close everything so for kubernetes also i'm just going to create a separate folder inside this so i'm just going to name it as kubernetes so inside it i am going to create two files so first file is deployment.aml and the other file would be service.yaml so this service yeah so the deployment so first one we have to go is a deployment so you can use the web reference for um you know uh taking the uh template for the deployment but in my case uh i already so api version is it is app slash v1 so we can also refer the um official kubernetes documentation like we can just go for kubernetes dot io or you can just type kubernetes deployment then take the template from here so i'm going to copy the same thing so the api version is apps v1 kind is deployment metadata is in the next deployment so i'm just going to take it as [Music] just a name as python web app so labels i'm just using as web replicas i'm going to put it as to select as app is equal to web so let's come to the template section first inside the template metadata we have the labels so i'm going to create a label known as app is equal to web and for the containers we have the web app as the container and the container image name is subweb app 1.0 2.0 anything and the container port is basically 5000 and since this is the label for this particular container so we have to use the selector also the same thing app web so this is about the deployment file so now let's go to the service object so i'm going to do the same thing here and i'm going to search for service and i'm going to take this particular so we'll take the node port because we need to expose it outside the kubernetes cluster yeah so this is the node port so i'm going to take this file and then paste it so app v1 uh kindly service metadata is so i can just name it as web service type is not port selector app as well because that is the selector that we have used for the containers port is 80 but the target port is 5000 because the container ports are 5000 not put if you remove so automatically the node port is selected so this is the just the bare minimum one okay so this is what our service object looks like so now what i'll do is so i'm going to deploy both deployment service so i'll switch to the kubernetes folder and then i use cube ctl apply hyphen f deployment.tml so deployment is created if you just do cubectl getpod so you can see that both the ports are in the running state and now let us deploy the service object cube ctl apply hyphen net service so this service is of type node port service is also created cubesat get pod comma service so this is the node port of the service which is eight two seven so i'm going to use this port number so the ip address colon three zero three zero eight two seven three zero eight two seven enter so this is the output and then if you just fetch for the details so you can see that this is the response of the details endpoint now if you keep refreshing you're going to get the different information because now there are two parts answering your query so these parts are load balancing the request through the service object so for every click you are going to get a different response so these are the different kubernetes manifest manifest files so now what i will do is i am going to delete both these objects by using cubist and delete hyphen dot so whatever are the files existing in this directory so all will be uninstalled so now even q1 it is manifest files are done so our last step inside the readme.md file is to create a helm chart so how do we create a helm chart so first of all you have to have the helm cli so if you have the helm cli so what we can do is so we can create a template first so the template is hell create i'm going to name this template as web app so this is going to create a template now if you open the template so there is a charts directory templates directory help ignore char.yaml file values.tml file so i don't need this chart directory because there is only one chart so there are no dependencies i'm going to delete this and also inside the templates there is a test directory which i don't need i'm going to delete it and hp i don't need it i'll delete ingress also i don't need i'll delete it service i need service account i don't need it so inside the templates i only have four files one is the helpers file which i need deployment.yaml file which i'm going to edit service file this is for the us exposing the service and notes nodes.txt so these are the various files which i generally need so first of all before modifying any of the objects so let's see what are the values present in char.yaml file so chart.ml file will have all the metadata like the api version of the helm which is v2 that means we are using the helm3 so the name of the chart is webmap and various uh other parameters like type version of the application app version etc so if you go to the wireless.aml file so the default replica account is one i'm going to change it to two so repository information so repository is web app and the tag information is 1.0 or 2.0 if it is your wish and pull pulse if not present and we don't need service account pod annotations and all if you want you can just um you know write them uh and uh service type it is not clustered it is not port we are going to expose it so there is no target port here we are going to create them ingress is by default it is false because not we are not using an ingress controller so auto scaling is also false so everything is perfect in values.tml file so let us now go to the deployments.ml file and see what all changes we can do so the kind is deployment metadata is having some name and labels that is fine so what they are saying is if auto scaling is enabled then only reuse replicas will take effect so i don't want this so what i'll do is i'll just remove this auto scaling enable so replicas is taking values from values file replica account so if i see the values file this is the replica account and now this selector here it is taking from the helpers file template metadata annotations it is fine but i don't need any annotations so i'm just going to remove them or else even if you put the annotations it is completely fine so it is up to you so i am just removing them spec image pull secret so there are no image full secret so if you want you can keep it i'm just removing it so service second uh we don't have i'm removing it security context also we don't have we are removing it so container name security context also we don't have we are removing it so image is taking from the wireless.ml file pull policy is also the same port this a container port is marked as 80 but it is 5000 so what i'll do is i'll create one more value inside the service here so what i'll do is i'll create a different value known as target port and i'm going to put it as 5000 so what i'll do is i'll take this target port value and i'm going to put it here so the way i'll put it is so dot values dot service dot target port so this is the port that i wanted to replace here and the readiness and liveness probe and resources i don't have anything so i'm going to remove everything from this point so to make it very simple so this is the final template for the deployments file so i'm going to save it so the same thing i'm going to do for the service so for service name labels are fine spec it is the node port which we just changed and then uh service type yes port target port is http no target port is not http it is a 5000 which is the container port so basically we just did it in values.service.targetport and the port is 80 so this is also fine so now before applying this helm chart so i'm going to render it so how will i render it so using helm template and the name of the chart which is web app so i'm going to render it so this is how your complete manifest file looks like so there is a service object which is of notepad type container port is 5000 service port is 80. and coming to the deployment there are two replicas so these are the selectors and these are the labels for each container and then web 1.0 is the container image and these are the various ports so what i'll do will release this helm chart by using helm install name of the release as let's say web app and then the chart uh which is again a web app so it is up to you to name the release as web app or simply web and then the folder where you have the helm chart so this is going to release your chart so if you do helm list so you're going to get this particular release which is the name web release one and this status has deployed if you just do cube ctl get pod comma svc so there are two containers oh sorry two parts which are running state and the node port is as three two zero three zero two double one sorry three zero double two one so i'm going to change this port here three zero double to one and then slash so it is going to respond with the same thing and then check the health which is slash and also check the details so this time also there are two parts are answering your request so if you keep refreshing this page you are going to get the different information because there are two parts answering your query so now uh even the helm chart is also done so what we'll do is we'll uninstall this release using helm uninstall and the name of the release which is a web release so now even this is also uninstalled so we have started with writing this simple flask based application we have created three routes slash slash health and details then we executed this locally and then we containerized it by creating the docker file and also creating the docker compass file and then we created various kubernetes manifest files like at the deployment and service object and we had also seeing how to create a helm chart now i'm going to push all this content to the github so first i'll check this status using git status so all these files are to be added so i will just use git add hyphen a and then check the status again and then i will use git commit message so git commit hyphen name i will do commit message as a web app and then i'll just use gish git push origin main because this is the default branch so if you want you can also create a new branch before making all these changes and push it to that particular branch now if you go to the git and then refresh this page you're going to get all the information so that's it for this video if you like our video kindly subscribe to our youtube channel and join our facebook group the links are given in the description below thank you
Info
Channel: DevOps Made Easy
Views: 49,098
Rating: undefined out of 5
Keywords: #Python #DevOps #Docker #Kubernetes #Helm #AWS #Linux #Jenkins #ELK #Ansible #Git #IoT
Id: SdTzwYmsgoU
Channel Id: undefined
Length: 71min 55sec (4315 seconds)
Published: Sat Dec 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.