Skip to content

Instantly share code, notes, and snippets.

@jaigouk
Forked from alexellis/README.md
Created July 27, 2018 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaigouk/aa32ea0e8dc161fa0fe576edb17eaf49 to your computer and use it in GitHub Desktop.
Save jaigouk/aa32ea0e8dc161fa0fe576edb17eaf49 to your computer and use it in GitHub Desktop.
OpenFaaS functions on knative

Portability with knative

Three functions from the OpenFaaS store have been packaged as "knative serving" definitions. No change to the container or code is needed.

  • Inception - identify the content of images with machine-learning - is it a bird, a plane or what?
  • Colorise - turn any black and white image into colour
  • NodeInfo - give system info, pass "verbose" as the body for network adapters etc.
  1. Clone gist

  2. Setup outbound traffic in Istio - https://github.com/knative/docs/blob/master/serving/outbound-network-access.md

  3. Deploy with kubectl apply -f

  4. Try inception function:

Find knative IP and set in export KNATIVE_IP=

kubectl get svc knative-ingressgateway -n istio-system

Find name of OpenFaaS function:

kubectl get services.serving.knative.dev inception-openfaas  -o=custom-columns=NAME:.metadata.name,DOMAIN:.status.domain
echo -n "https://digital-photography-school.com/wp-content/uploads/2007/02/black-and-white-tips.jpg" | \
curl -X POST --data-binary @- -H "Host: inception-openfaas.default.example.com" $KNATIVE_IP -i

HTTP/1.1 200 OK
content-length: 544
content-type: application/x-www-form-urlencoded
date: Thu, 26 Jul 2018 22:10:22 GMT
x-duration-seconds: 2.702005
x-envoy-upstream-service-time: 3008
server: envoy

[{"score": 0.9821120500564575, "name": "coil"}, {"score": 0.005453020334243774, "name": "snail"}, {"score": 0.0016122378874570131, "name": "chambered nautilus"}, {"score": 0.000917674507945776, "name": "nematode"}, {"score": 0.0008158141863532364, "name": "bannister"}, {"score": 0.0005424919654615223, "name": "ram"}, {"score": 0.00037825442268513143, "name": "hognose snake"}, {"score": 0.00021164452482480556, "name": "combination lock"}, {"score": 0.00019924681691918522, "name": "buckle"}, {"score": 0.0001905750104924664, "name": "hog"}]

You may have to run this several times since Istio will "time-out" the request. If you can figure out how to set a longer timeout that'd be better. Post a comment to the Gist.

Colorise will need a longer timeout.

NodeInfo will work with the default timeout, if you trigger auto-scaling you'll see the different hostnames coming back.

See the [OpenFaaS function store](Function store manifest: https://github.com/openfaas/store ) for more examples.

You can also generate/build/push your OpenFaaS functions with the OpenFaaS CLI and create your own serving YAML files for deployment onto knative.

  1. Building a new OpenFaaS Golang function and deploying onto knative:
brew install faas-cli # or curl -sLS https://cli.openfaas.comn | sh

faas-cli new --lang go go-native --prefix=alexellis2

# Edit go-native/handler.go

faas-cli build -f go-native.yml
faas-cli push -f go-native.yml (pushing docker.io/alexellis2/go-native:latest)

# Create serving.yaml definition or copy example below and set image to "docker.io/alexellis2/go-native:latest"
# Deploy with kubectl apply -f serving.yaml

The easy way

Deploy OpenFaaS with helm, open the UI and browse the store, then click "Deploy".

Then learn how to master Serverless Functions with OpenFaaS through the self-paced online workshop.

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: colorise-openfaas
namespace: default
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: alexellis2/openfaas-colorization:0.4.0
env:
- name: fprocess
value: "python -u index.py"
- name: read_timeout
value: "60s"
- name: write_timeout
value: "60s"
- name: url_mode
value: "true"
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: inception-openfaas
namespace: default
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: alexellis/inception:2.1
env:
- name: fprocess
value: "python3 index.py"
- name: read_timeout
value: "60s"
- name: write_timeout
value: "60s"
- name: content_type
value: "application/json"
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: nodeinfo-openfaas
namespace: default
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: functions/nodeinfo:latest
env:
- name: fprocess
value: "node main.js"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment