{"id":211,"date":"2021-03-10T21:08:38","date_gmt":"2021-03-10T21:08:38","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=211"},"modified":"2022-04-11T20:24:24","modified_gmt":"2022-04-11T20:24:24","slug":"100-days-of-learning-day-2-deploying-my-first-python-based-function","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/100-days-challenge\/100-days-of-learning-day-2-deploying-my-first-python-based-function\/","title":{"rendered":"100 Days of Learning: Day 2 \u2013 Deploying my first Python based function"},"content":{"rendered":"\n
Photo by James Harrison<\/a> on Unsplash<\/a><\/p>\n\n\n\n Here is my\u00a0Log book<\/a><\/p>\n\n\n\n For a warm up today in learning more about OpenFaaS and To get the list of available functions from the store<\/p>\n Learning action point:<\/strong> **How do I add more functions to the store? Can I host my own private functions and have the listed in the store? EDIT: There is a How do I install a new FaaS?<\/p>\n Lets try deploy<\/p>\n Aah! I bet it is because the OPENFAAS_URL is not set since I closed yesterday’s terminal session. Guess it is time I add this environment variable to my .zshrc. In my case it is actually "~\/.ajzsh\/extras\/zshrc.ajzsh" because I have my own zsh setup called ajzsh<\/a><\/p>\n Relaunch the terminal and verify<\/p>\n Profit!<\/p>\n Learning action point:<\/strong> How do I setup TLS on the Note that the store name was "NodeInfo" but the function to invoke is "nodeinfo". You will get an error if you tried to invoke using the "NodeInfo" name.<\/p>\n Also to note the Invocations number is how many times the function has been called.<\/p>\n I learned about this useful cli tool name jq<\/a> from Serverless for Everyone Else<\/a> (the book and workshop I am following at the moment for learning about OpenFaaS etc.)<\/p>\n Install Get info about a deployed function as JSON response (note: jq is here just used to prettify the output)<\/p>\n <\/p>\n To get a list of templates to start from<\/p>\n To get more information about a template<\/p>\n I will be deploying my function container to Docker Hub (turns out I already had an account on there). Set the Docker Hub prefix to use.<\/p>\n Create the new function from the python3 template<\/p>\n <\/p>\n Inside the directory where helloworld.yml lives I will try and build the docker image.<\/p>\n Error, mmkay. I do have docker installed (Docker Desktop on Mac)<\/p>\nfaas-cli warmup<\/h2>\n
faasd<\/code> I will first deploy the NodeInfo function using
faas-cli<\/code>.<\/p>\n
$ faas-cli store list\n\nFUNCTION DESCRIPTION\nNodeInfo Get info about the machine that you'r...\nalpine An Alpine Linux shell, set the "fproc...\nenv Print the environment variables prese...\nsleep Simulate a 2s duration or pass an X-S...\nshasum Generate a shasum for the given input\nFiglet Generate ASCII logos with the figlet CLI\ncurl Use curl for network diagnostics, pas...\n<\/code><\/pre>\n
--url<\/code> parameter and this is the way to private stores or other hosted stores.<\/p>\n
$ faas-cli store help\n...\nAvailable Commands:\n deploy Deploy OpenFaaS functions from a store\n describe Show details of OpenFaaS function from a store\n list List available OpenFaaS functions in a store\n...\n<\/code><\/pre>\n
$ faas-cli store deploy NodeInfo\n\nIs OpenFaaS deployed? Do you need to specify the --gateway flag?\nPut "http:\/\/127.0.0.1:8080\/system\/functions": dial tcp 127.0.0.1:8080: connect: connection refused\n\nFunction 'nodeinfo' failed to deploy with status code: 500\n<\/code><\/pre>\n
$ echo $OPENFAAS_URL\nhttp:\/\/192.168.64.4:8080\n<\/code><\/pre>\n
$ faas-cli store deploy NodeInfo\nWARNING! You are not using an encrypted connection to the gateway, consider using HTTPS.\n\nDeployed. 200 OK.\nURL: http:\/\/192.168.64.4:8080\/function\/nodeinfo\n\n$ echo "" | faas-cli invoke nodeinfo\nHostname: localhost\n\nArch: x64\nCPUs: 2\nTotal mem: 1988MB\nPlatform: linux\nUptime: 16999\n<\/code><\/pre>\n
faasd<\/code> gateway?<\/p>\n
Helpful faas-cli commands<\/h2>\n
\n
$ faas-cli store describe NAME_OF_FUNCTION -v\n<\/code><\/pre>\n
\n
$ faas-cli describe nodeinfo\nName: nodeinfo\nStatus: Ready\nReplicas: 1\nAvailable replicas: 1\nInvocations: 2\nImage:\nFunction process: node index.js\nURL: http:\/\/192.168.64.4:8080\/function\/nodeinfo\nAsync URL: http:\/\/192.168.64.4:8080\/async-function\/nodeinfo\nLabels:Annotations:%\n<\/code><\/pre>\n
\n
$ faas-cli remove NAME_OF_FUNCTION\n<\/code><\/pre>\n
Accessing the OpenFaaS gateway as a REST API<\/h2>\n
jq<\/code><\/p>\n
$ brew info jq\njq: stable 1.6 (bottled), HEAD\nLightweight and flexible command-line JSON processor\nhttps:\/\/stedolan.github.io\/jq\/\n\n$ brew install jq\n\n$ echo '{ "name" : "Andre" }' | jq\n{\n "name": "Andre"\n}\n<\/code><\/pre>\n
export PASSWORD="Insert the value you got earlier for faasd admin user"\nexport HOST="192.168.64.4:8080"\ncurl -s http:\/\/admin:$PASSWORD@$HOST\/system\/function\/nodeinfo | jq\n<\/code><\/pre>\n
Building my first OpenFaaS function<\/h2>\n
$ faas-cli template store list\n...\npython3 openfaas Classic Python 3.6 template\npython3-http openfaas Python 3.7 with Flask and HTTP\n...\nswift affix Swift 4.2 Template\n...\n<\/code><\/pre>\n
$ faas-cli template store describe python3\n\nName: python3\nPlatform: x86_64\nLanguage: Python\nSource: openfaas\nDescription: Classic Python 3.6 template\nRepository: https:\/\/github.com\/openfaas\/templates\nOfficial Template: true\n<\/code><\/pre>\n
export OPENFAAS_PREFIX=DOCKER_USER_NAME\n<\/code><\/pre>\n
\u256d ~\/Learning\/faasd\/helloworld [20:23:45]\n\u2570 $ faas-cli new --lang python3 helloworld\n<\/code><\/pre>\n
\u256d ~\/Learning\/faasd\/helloworld [20:33:56]\n\u2570 $ faas-cli up -f helloworld.yml\n...\nErrors received during build:\n- [helloworld] received non-zero exit code from build, error: Cannot connect to the Docker daemon at unix:\/\/\/var\/run\/docker.sock. Is the docker daemon running?\n<\/code><\/pre>\n