Skip to content

Instantly share code, notes, and snippets.

@alexellis alexellis/README.md
Last active Jul 14, 2019

Embed
What would you like to do?
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"
@yogeek

This comment has been minimized.

Copy link

commented Apr 22, 2019

Hi ! The link to Setup outbound traffic in Istio is broken. Here is the good one apparently :
https://github.com/knative/docs/blob/master/docs/serving/outbound-network-access.md

@alexellis

This comment has been minimized.

Copy link
Owner Author

commented May 11, 2019

Thank you @yogeek! If you'd like to chat more, do you want to join Slack? https://openfaas.o6s.io/slack

@toshi0607

This comment has been minimized.

Copy link

commented Jul 14, 2019

@alexellis Thank you for interesting tutorial! I think OpenFaaS itself has great power of scaling. When used with Knative, what kind of features are added? Can OpenFaaS utilize Eventing and different type of scaling?

@toshi0607

This comment has been minimized.

Copy link

commented Jul 14, 2019

Oh... Does this tutorial show Function CRD can use not only OpenFaaS API Gateway scaling mechanism but also knative/serving's ?

@alexellis

This comment has been minimized.

Copy link
Owner Author

commented Jul 14, 2019

You should join OpenFaaS Slack using the link I sent before. Happy to chat in a week or two from now. Also see my personal blog and the OpenFaaS blog for more on knative with OpenFaaS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.