FastAPI Running on Kubernetes Pod

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys uh today i'll talk about fast api and kubernetes i'll show you step by step how to run faster api application on kubernetes platform and fast api application will run through docker container and this docker container will run on kubernetes port kubernetes port is a logical unit which acts like a platform which could run multiple containers and all the containers that are assigned to the same pot this contain all those containers they share some resources like the same ap they could share the storage and here which is important uh all the containers that belong to port they would always run on on the same note so uh let's see how it runs and uh let's jump um to actual demo and i'll try to explain it step by step so you could reproduce it easily on your own environment first of all for the demo i'll be using our open source products keeper which implements um workflow for ml applications but you could run any workload on on this um on this environment as well so for the demo i'll be using workload service and actually when you start with kubernetes you should know that kubernetes runs containers containers can be coming from docker or it could be another container platform doesn't matter in our case we're using docker so to run container in kubernetes first of all you need to have images docker images and when you have a set of services uh it's kind of cumbersome and uh it takes time to create each image separately so i would recommend to use docker compose and you run docker compose with lock built and in this in this case local compose would create the images but it would not start containers because containers we would start with kubernetes we don't need to start the containers with docker compose so in this way you build all the images that's the first step and then for the second step we would go to the service itself and we would check also how the docker file is being defined and this docker file was used in the first step by docker compose to build the image so docker docker file contains entry point and in this entry point we include a command which would start the process in this container and in our case we start fast api application so this is the command to start a fast api application on port 5000 and it will be exposed from the container so this is the docker file and we for the kubernetes we provide the image and once kubernetes starts the container from that image it automatic the first api process starts automatically because information instruction how to start fast api is being included in a docker file okay so we've got docker file we've got docker compose and we build the images we docker compose build what else we need uh metadata file for kubernetes uh where we will describe which image we would like to use uh some additional parameters maybe and how we would uh create this kubernetes uh port which would run the container and this is all the metadata is defined here in workflow port metadata file this is very simple one you could make it more complex if you want but my goal for this video was to have a simplest as possible example uh which would demonstrate how to start faster application in kubernetes with docker container so over here we have pod name then we have we can list containers and the same port can uh contain multiple containers as i said before in our case we just have one and this is certain architecture decision when you would like to have a single container import or maybe multiple and based on a best practice recommendation if you see that both containers must communicate between each other must work uh together then it makes sense to use uh to create all those containers under the single port if you see that containers are independent then there's no obvious communication between them then you could post them into different spots and this would allow to run containers on different nodes if you want in this case we have one container we specify the image path from where we get the image and i have a property called image pull policy if not present this means if the image is available locally on my machine then kubernetes would pull the image from from my local registry otherwise by default it will try to use on the docker registry online and if image is not pushed there then it will report the error and by the way to test kubernetes to play with kubernetes i'm using um docker environment and i enable kubernetes support in docker locally but the same metadata you put the same instructions you could run on the cloud environment as well and it should work okay then we have a name for the container which will be started by kubernetes and we expose port 5000 and this is hd pop http port for this container and we do additional check define the check here loudness pro which allows to verify if container is if processed in the container is up and running if our first a fast api application is running and it automatically does a check and and it pings the api endpoint from fast api if it will see that um endpoint doesn't respond until restart the port and start the application from from scratch okay and there's a way to define service interface in kubernetes which would allow to expose um service from uh from current container from current port to the outside but in our case workflow container is doesn't suppose is not supposed to be run externally so to test api from uh this container will do port forwarding but in general if you would like to expose a container to the outside you need to define service with load balancing a load balancer in kubernetes okay then there is a readme file over here and i have explained how to build and run kubernetes port for for this image and now let's let's test it let's see how it works i'll switch to development environment and this is the same readme file and i'll go step by step so first we need to build the image and with certain tag we should this command would use the docker file i'll not run this step because i already have the image and i can verify that by running docker images and this is the image which was built before okay sorry i was looking for a step where i explained how to build images and um yeah this is uh for the case when we uh run when we would like to run a container locally but this is the section for kubernetes and over here we have a command to create a port but in any case it was good to double check that we have an image otherwise pod creation would fail we have the image so we can go and copy this command we are using a cube ctl controller command which would from command line allows to execute operations towards kubernetes and execute the actions on kubernetes engine so with this command we execute metadata file which explains how to create the port which image to get for that port and basically which port to expose and so on okay you run the command port uh it reports that port is created but um this command run runs and synchronously it may be that uh port creation runs still in the background and you could check if it was really created you could list all the pots and that actually running state is good each 14 seconds zero restarts okay we could also if you want describe the port this prints a lot of information ap and so on so if you would like to explore more about the port you could check the output and then we would like somehow to check if fast api process is running but we don't want to define kubernetes service for this port because we think that services from current port would not be exposed to the public but this is another quick way we could do port forwarding so we could forward the port from from from the spot and we could call a container that runs on a given port from the we could container services that run on current port from the outside okay and then we could also print out locks that reported in the port over here we see that container was started in the port and because we have a liveness prop uh which runs each 10 seconds it tests it so the kubernetes is testing each 10 seconds if the first api process is running and we see in the log there are calls executed and all of them are responding correctly okay and just to be sure that port forwarding is working and containers up we could um access uh fast api um endpoints from the outside and we could execute uh test endpoint we get successful result and we could also execute another endpoint and we could check serving sync key execute and to get back the result and yeah if you want uh if you don't need the bot anymore you can always delete it with this command so thanks for watching and the goal of this video was to show and demo to you that how easy is to run a fast api application in docker container through on top of docker on top of kubernetes hopefully yeah actually right now i'm busy on adding kubernetes support for skipper so as long as i add more functionality i'll record more videos and hopefully you'll be able to learn something new as well because kubernetes is a complex topic and there are lots of resources online but um it's kind of hard to find answers because uh uh it's a bit cumbersome and complex and information is fragmented and when you build something practical uh you need some ready solutions or best practices so this is my goal to present something uh small practical which you could use straight away thanks for watching and stay tuned bye
Info
Channel: Andrej Baranovskij
Views: 565
Rating: 5 out of 5
Keywords: FastAPI, Kubernetes, Python
Id: K0oMkXm1YJg
Channel Id: undefined
Length: 12min 42sec (762 seconds)
Published: Sun Aug 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.