Local Serverless Functions Deployment [Kubernetes + OpenFaas]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys my name is yankee and in this tutorial we are going to see how we can create deploy and test serverless function locally on our machine so for that we will be using a tool called open fas as in function as a service so open fast is a framework that is used to create serverless functions using docker and kubernetes it is highly scalable and comes with auto scaling and metrics so let's get started so to follow along in this tutorial we are going to be needing a bunch of tools and the first tool we are going to install is called arcade and this is going to help us install other tools so the arcade tool is uh created by alex ellis who is also the creator of open faz and this tool helps us to install you know cli related applications for kubernetes and it also allows us to install applications directly to kubernetes cluster so it's a pretty handy tool uh if you do not have it already uh you can get arc it from the instruction on the github page so i already have arcade installed on my machine so if you do arcade and you are going to be created with this help message and we are going to install a bunch of tools using this so the first tool is going to be arcade gate cubes etl so cubectl is a combined line application that allows us to communicate with kubernetes api so i already have it installed but yeah it's going to update the version and all the binary downloaded using arcade is stored in cd arcade bin on your home directory so it also gives you the instruction uh to you know like export path variable to that directory so the binary is accessible on your terminal so the next application we are going to install is called kind so kind uh kubernetes in docker uh that is what kind stands for and it allows us to you know bootstrap a local kubernetes cluster on our machine using docker uh so for this tutorial you also need docker on your machine if you do not have docker uh to install it uh using the official docker face so i have docker installed on my machine and the next tool we are going to install is called fast cli so arcade get fast cli so fast cli is a command line application uh that allows us to interact with you know open fast services in the kubernetes cluster it's a pretty handy tool to you know build deploy and run our serverless functions on kubernetes using open fast so a few words on arcade so arcade is a pretty handy tool like i've mentioned earlier and if you do arcade kit it shows you a bunch of tools that you can easily install on your machine so all of these tools are related to kubernetes so it's one-stop solution for them now that we have all the tools installed on our machine like kind cube ctl open fast the next thing we want to do is set up a local kubernetes cluster and to do that i'm going to head on to kinds documentation and head on to local registry so we are going to set up a local registry because to deploy application on kubernetes you need to package it as a docker image and deploy it so to do that we are going to set up a local docker registry and kind kindly offers us this script where we can bootstrap a kubernetes cluster and you know run a docker registry at the same time so i'm going to copy the link to this file get out to my terminal i'm going to create a directory called open fast cd into it then i'm going to download that file right so as you can see this is a cell script so we need to run it make it executable the q table so let's see the contents of this file so on the first few lines you can see we are registering our docker registry so the name of the registry is kind registry and it is running on localhost 5000 and to run this uh registry we are using this command docker run restart always port localhost 5000 you know and this is the command for creating a kind cluster so it uses the command kind create cluster dash test config and all the configuration related to our docker registry and finally we have this config map which describes how tools should communicate with a local registry from the kubernetes cluster so now that we have this and before we run this script what we need to do is head on to our docker desktop settings and add localhost 5000 as our insecure registries uh to allow you know http communication to the local registry else it's going to throw an error and our application will not be deployed so if you are using mac or wsl 2 on windows like i am you can go to the settings and under docker engine add this line if you are on linux or any version of linux variant of linux what you can do is uh head on to the docker docks and it contains all the information related to setting in secure registries so basically what you have to do is go to that uh slash atc slash docker slash tameon.json i believe so you can edit the configuration there and once we have that setting uh so i already have it here what i'm going to do is create a kind cluster a kubernetes cluster and a docker registry so simply kind with registry dot as such going to create a kubernetes cluster using a docker container so it might take a few time here [Music] so once this is done uh our cube ctl is already configured to use this uh kubernetes context so we can like do cube ctl uh config or into context and as you can see we are using kind context uh we must have our nodes ready so i cube ctl get nodes to get our nodes so yeah our control plane is not ready it's like a creating it might take few seconds to few minutes depending on what hardware you're on and to check if the services inside our kubernetes cluster are running or not we are going to use cube ctl uh get parts minus and cube system so all the services on our kubernetes clusters are running and we are ready to deploy our application on it so next i will also like to check uh docker locksmith f kind registry if our registry is working or not so as you can see our registry is up and running so next up we are going to install open fast onto our cluster and to do that we are going to use the arcade tool again so it's as simple as doing arcade uh install open faz so it's going to install open faz uh yeah going to take some time and boom uh open fizz is uh like installed on our kubernetes cluster uh cube ctl plus minus and so open pass as you can see uh kubernetes is creating uh services uh i mean like parts for our uh open fast cluster or open fast framework that supports serverless functions when we deploy to it it has got q worker prometheus snaps gateway basic auth and all these good things comes by uh built in let's use the watch mode so a lot manager is running so it might take some time i'll return back when everything is running so now i have all my services uh related to open fast up and running uh the next concept i would like to introduce is called templates in open fast so if you are going to create a serverless function using any languages openfast provides you with a template that you can use or to bootstrap functions easier and more quicker go to list all the templates what i'm going to do is fast cli template stored list as you can see there are a bunch of templates for different languages so it has got one for c sharp go java node python even one for rust so this is pretty much community driven it has got a bunch of template uh you can also go to the github page and see other information related to the template as well so what version it is running on right so note 14 12 python and all these things so back to my terminal uh for this tutorial i'm going to be using python 3 uh dash flask package so i can download this package onto my local machine using a fast tli template for a full python 3 flask so what this is going to do is download templates related to python 3 as flask and as you can see there is a template directory on my openfast folder so if i do t i don't have hold i will just cd into template and you can see there are a bunch of templates related to flask so we got one for 2.7 and all these other things for uh you know different architecture and different uh operating systems so flags deviant sdpdb and arm and things like that so the only template we are interested in is python 3.3 flask we can remove uh all the other things here as well p that's flask and if i go here so here is the main uh function folder so this is the function folder that is going to be created uh when we do fast cli new uh we are going to see it later on so all the contents inside this folder is going to be uh you know given to us to write our function and we also have this docker file so dockerfile is the main uh you know component uh or the glue that is binding together all the components of this template so it has got all the information all the commands that needs to be run to you know build and deploy your functions so it comes pre-packaged so you can just focus on writing your function and do not care about anything else uh indexed pi file is the entry point for the flag server we will be running so as you can see we have uh handler handler.handle and we are passing the request to that handle function so if you go and inside the function folder in the handler we have the handle function and it is getting the request pass down from this index.pi file so next we have requirements.txt and all the requirements that is required for you know serving this application and template.yml is something that we are going to see later on so that is the content of the template uh so if you want to you know make changes you can do so on the docker file as well but uh for the simplicity of this tutorial i will not be touching anything here so let me go back and let me go back so i'm going to create a new function using the python 3 flask template so what i'm going to do is pass cli new so the project we are going to create is a simple uh dictionary application whenever we uh give a word to it it is going to return a meaning on other things related to that word so it's called by dict so we are going to be using python 3 flex deviant template and boom we have a folder called pi dict uh that is the name we have given to our function and we have this by dig.yaml file that is the configuration file to deploy our application and if we go to the pi dect file you can see all the contents of the function folder we saw earlier in the templates folder it has got handler and if you want to write test it has got handle test requirementstox.ini and so on so next i am going to be creating my function uh get a step back and i'm going to open vs code on this directory so there are a bunch of things open i'm going to close everything the only thing i care about is the pi dict uh you know directory where my function is going to rest so for this project we are going to need a package called pi dictionary so this is all we are going to need and in the handler function what i'm going is uh top from pi o and a roi dictionary import by oh wait so it's both so once i have this imported uh what i'm going to do is get it so diction goes to by dictionary right so once i have that initiated all i have to do is return dictionary dot meaning and request which is going to be a word in this case so it's a pretty bare minimum you know function you can go and build much bigger functions with much more complexity but for the sake of this tutorial i'm going to create it pretty simple and we have our function ready now next what i'm going to do is head on to by dick.yaml file so to explain about this file it is using the version one uh you can think of this as a manifest file kind of like kubernetes but it talks with fast cli so yeah the provider is open fast gateway is uh localhost 8080 so gateway is the uh you know url where we are going to send our request to for functions so this endpoint is going to respond to our uh call to the functions and uh to see this i'm going to go to my cycle i and i do ptl uh it's services ports in this case so you can see the gateway service and it is running on port 8080 and later on we are going to put forward it to our localhost port 8080 and communicate to this gateway api but for now let's get back to the yaml file next we have a section called functions so this is going to hold you know information related to our functions so for now we just have a single function called pi dict but later on we might have multiple functions here and in pi dict we have this uh you know field called lang which points to what kind of template we are using so we are using python 3 flask deviant if i go to my template so there is python 3 flask devian and another one is handler so handler means where our function resides apparently it relies on dot class by dict meaning it's in the current uh directory inside of the pi data folder and the name of the image for the name of the image uh you know we have to put a fully qualified uh docker image name and in our case uh since we are running uh docker registry locally uh we need to add a local host 5000 as a suffix here else is not going to work let's head on to my terminal and what i'm going to do is fast cli build minus f by dict yaml so it is going to create a you know docker image out of the configuration mentioned in pi dick.yaml it might take some time you know all dependencies and run all the commands that are inside of this docker file yep so we have our image ready and if i go to my code editor i'm going to create a file called a folder called build and inside of there we have a pi dict of our function and it is going to replace everything uh inside of this as the content of pi dick so inside of the function we have our pi dict function that we just created and all the other things that are here are copied here so that's how it works next up we are going to see the image so docker images and we can see uh we have localhost 5000 pi digged and it was created 43 seconds ago and the size of a function is 355 mb so for a fairly small image i mean for a fairly small application it's a huge image and to optimize it uh like i've said earlier you can dive into the template directory and onto docker file you can you know change a bunch of stuffs to make it even smaller so maybe use alpine image something like that so go knock yourself out on this one and now we have our image ready uh next thing we need to do is push it to the docker registry so to do that i'm going to do fast cli push minus f by digit yaml again it's going to read the configuration there and it's going to you know look at the image section here finds this and pushes this image to the local docker registry uh if i go here docker locks minus f so yeah like you know pushing is this here not essential to see right now but anyway yeah our image is boost now next thing we need to do is uh we have our image built we post it to the docker registry now we need to deploy it to the kubernetes cluster and before we do that there are a bunch of things that we need to configure it's a small configuration but yeah bear with me so the next step is to uh you know put forward um open fast gateway service to our local host uh we need to do this because in order to communicate uh with the open fast service uh we need an outlet uh so since we are not running on managed cloud we do not have a public url that we can you know uh run our comm commands against so for this we are going to port forward so again uh cube cdl get service open fast so if you were uh running on you know managed cloud you will be uh using the ap uh external ip of external gateway external but for this we are going to port forward the gateway service and to do that uh what i'm going to do is cube ctl port forward minus and so the namespace is open fast and service gateway to our local host 80 so we are port forwarding to local host 8080 i'm going to just jump into a different terminal now and leave that port open and running uh back to my browser so since we have like port forwarded it let's go and do 8080 now uh it is like you know prompting us to input our username and password so the gateway is uh you know uh authenticated so we need access to this but where are we going to find it so when we were you know like installing open fast onto our kubernetes cluster it has also uh deployed secrets uh to open fast so we are going to get secret uh which is going to be our password and the username is going to be admin in this so back to the terminal again and we are going to see the secret in action so cube ctl get secret minus n open pass basic auth minus o as station so this is the secret that is inside our open fast uh namespace so as you can see the password that we are looking for is inside data and basic auth password and this is base64 encrypted we need to get this value and decrypt it using base64 so it's pretty straightforward uh so while i'm like decrypting this password i'm going to store it in a environment variable called password so so it's going to be password and i already have this command here so i'm evaluating you know this command and storing it to the password variable what i'm doing is cubectl uh getting the secret so most of the commands are like similar to a verb but uh what i'm doing extra is like passing the json path argument and getting the data from your password from dot data and basic auth passwords it's data and basic auth password and i'm going to pipe it uh pipe the output to base64 and decode the value and echo it and save it to password so once that is done i'm going to do echo dollar password and this is going to be the password for my open fas ui uh back to the browser so password admin and boom so this is the open fast you know ui uh where i can see bunch of functions that i deploy and i can even like trigger functions from here as well and there is this handy thing called open fast store where you can get functions from other places and deploy it to your cluster and test it out so it's pretty cool you can test it yourself but i'm not going to do this in this tutorial so once we have the password we also need to authenticate fast cli to communicate to the open fast service gateway because it's authenticated so for that what i'm going to do is echo minus n password and i'm just going to do login minus s to do the trick uh this is going to authenticate my fast cli to talk with the openfast gateway so once that is done uh now i can finally finally deploy my application so it's fast cli deploy minus f by dict dot yaml and enter so oh sorry i'm in my home directory i should be inside open fast and let me repeat that command again okay it's accepted and my function is deployed so uh to see uh the status of my function i can go to cube ctl hit pause minus n open fast minus f and namespace so it's already running uh let me go back to the browser and as you can see we have our function by dict here and you can trigger it from here as well so what i'm going to do is you know write a word gravity and i want to know the meaning of it so i'll just invoke it and it's going to give me this result now the use of brief expression the attribute of being brief or fleeting so yeah this is pretty cool i mean our function is uh ready to use and we get this cool ui where we can like test this function you can like pass around json but uh for this function is it is just text and if you are not a ui kind of person and you want things to be done in the terminal itself so cli got you covered as well so i'm going to do gravity again and pipe it to fast cli invoke and the name of our function so it's going to return the same result as before yep uh the use of brief expression yep the same thing again so this is pretty handy uh so now like that we have like authenticated fast cli and we have everything set up the next function you are going to deploy is going to be pretty simple you just have to you know bootstrap a new template and this uh deploy it and you know we have done three steps here or one is fast cli build another was fast cli push another was fast cli deploy all of these commands can be summarized into one called fast cli up so if i do help up so this is going to you know bootstrap all these commands uh which is going to like build push and deploy open fast functions so that is pretty handy as well so now we have all of our setup ready it only gets easier from here so we can like create a bunch of functions and just deploy it and i'm feeling a bit adventurous today so i'm going to create a new function as well so that we can see how we can handle multiple functions using open fast so again facial i knew it's going to be called a translator so translator so what this function is going to do is when we pass a word and uh the language we want it to be translated to it's going to like you know send it in that language so the language template is again uh python p flask uh debian and what i'm going to do here is append so what it is going to do is uh obtained uh you know uh the configuration related to this new translator function onto our existing pi dick.yaml else it is going to create a a separate yaml file called translator.yaml which i do not uh want because everything can be handled on a single yaml file so uh once we do that okay uh mistake there so once we do that we are going to have another directory called translator so again uh let me go back to my code editor go inside translator so again the same template again you know you can go inside the handler function and just write your code there so for this function as well we are going to have a single dependency it's going to be a package called google trends and i'm going to paint the version as 4.0.0 minus rc1 and next i'm going to go to my handler function again so i'm going to tendencies that is required here so from fans for translator and from flask port j75 because we want to you know send out a json response to the user later on so jason as well handle jason all right next what i'm going to do is uh this is going to be a json request uh in this case i'm going to parse the json so data is going to be load and it's going to be request next up we are going to extract the text that needs to be translated so data dot get it should be inside the text field uh next is test so test is going to be the language we want the text to be translated to so what i'm going to do is just get yes and by default nothing is provided i'm just going to translate english so let's get very close to translator oh translated is going to be translator dot translate all of course so translate text we want to translate uh destination what language we want to change it to and source is going to be auto it is going to auto detect uh what language we are passing as the text here so uh the only thing that remains is to return the response so it's going to be jsonify and translation going to be it dot text and source is going to be translated dot source and received is going to be the text that we received so this is the response we want to send so yeah our function is ready here as well again back to pi dictate yaml uh use localhost 5000 here again you know uh this uh template now not only holds by dick function but translator function as well so giving it a generic name actually makes sense so let me do that i'm going to do uh mv pi dot ammo to function camel so it's functions.yaml now uh changes will be expected here as well so now the only thing that i need to do is you know upload it or deploy it and that can be done using a single command that is fast cli deploy minus f functions.yaml and i'm going to filter it out to just deploy the translator function because we already have dictionary function deployed it's not deploy it's up to do uh you know belt push and deploy a single command so yep going to build the image then it is going to push it to our local docker registry and it is going to deploy it for open fast any time now all right our image is getting pushed to the registry and it is done now i do docker images so it's going to have you know a translator image which was built 11 seconds ago and now i can go to my fast cli store and we have this translator function here so what i am going to do here is text uh we need to find the session file and i'm going to do namaste here which is a nepali word for greeting someone and the destination is going to be let's say in this case arabic maybe all right let's see uh what is the response all right so this is the session so i need to select json here invoke okay internal server error okay there seems to be an error here so i think what the mistake is ah okay this would be s because you know we are loading string and not a file so again back to my terminal and fast cli up do this one more time so it's going to be pretty quick um because like docker has cast all the images and things like that so it should be faster than before all right it's done now back to our browser now let me run this command one more time and now yep we have the translated test for uh you know the word namaste and if i want to do this for korean i can do ko and it returns korean and for other languages like dutch so nl going to return hello so it's basically namaste means hello in nepali so yeah that's pretty cool and if you want to know the list of uh supported languages uh you can go to the uh you know official page uh documentation page for google trends uh in pi pi and you'll find it there now that we have our functions deployed the next thing i want to show you is the metrics server using prometheus so open fast deploys it when we are installing open fast to the kubernetes cluster so if i do cube serial gate pods and inside of open fast okay get parts minus n open files so you we can see uh we have prometheus here and let's see the service as well so yep it's running on port 9090 so we would also want to you know put forward it and use it on our browser so to do that uh what i'm going to do is give cdl port forward and pull fast gateway pass service from me yes and 1990 to right okay a mistake again all right uh now we are support forwarding prometheus to our local host port 9090 let me go to the browser and go to the host 90 and yep we have this uh prometheus ui in front of us and we can like you know get a bunch of things that we can test out so let's say uh function invocation total execute and it's going to show a graph of you know what kind of functions were triggered like we have pi tech translator was triggered multiple times so data you know like in 30 minutes 10 minutes and you get all these cool traps so that's pretty handy so that's it for this tutorial guys i hope you had fun learning about open fast and if you want to learn more and get deeper into this ecosystem you can go to the bottom of open fast official page and there is this workshop which is a walkthrough of different things you can do you know using open fast and you can join the twitter community or the slack community and ask your questions showcase your projects and things like that and if you are into open source most of the tools are open source so you can go to github and contribute there as well so that's it for this video i'll be seeing you in the next one if you like this video give us a thumbs up and if you have any queries or suggestions leave them down in the comments section below i'll see you next time
Info
Channel: Yankee Maharjan
Views: 3,220
Rating: undefined out of 5
Keywords: function as a service, faas, openfaas, serverless function, serverless, kubernetes
Id: 3JpnyF0agxY
Channel Id: undefined
Length: 37min 39sec (2259 seconds)
Published: Wed Apr 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.