Skip to content

Instantly share code, notes, and snippets.

@TomerFi
Last active November 27, 2022 18:20
Show Gist options
  • Save TomerFi/b472312ad1a9048a5206de4a58c2d843 to your computer and use it in GitHub Desktop.
Save TomerFi/b472312ad1a9048a5206de4a58c2d843 to your computer and use it in GitHub Desktop.
Serverless Framework in a Knative-OpenShift Environment

Serverless Framework in a Knative-OpenShift Environment

The following are a recording of the steps performed for verifying the feasibility of Serverless Framework in a Knative-based OpenShift environment.

Versions used

Serverless Framework 3.21.0 OpenShift Container Platform 4.10 Knative Operator 1.6.1

Installing the Knative Operator

Original install instructions can be found here.

Note that for this POC, Knative-Eventing is not required.

Install the operator

oc apply -f https://github.com/knative/operator/releases/download/knative-v1.6.1/operator.yaml

Verify the operator deployment

$ oc get deployment knative-operator

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
knative-operator   1/1     1            1           74s

Initialize Knative-Serving

cat <<EOF | oc apply -f -
---
apiVersion: v1
kind: Namespace
metadata:
  name: knative-serving
---
apiVersion: operator.knative.dev/v1beta1
kind: KnativeServing
metadata:
  name: knative-serving
  namespace: knative-serving
spec:
  ingress:
    kourier:
      enabled: true
  config:
    network:
      ingress-class: "kourier.ingress.networking.knative.dev"
EOF

Verify the serving deployment

$ oc get deployment -n knative-serving

NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
3scale-kourier-gateway   1/1     1            1           7m40s
activator                1/1     1            1           7m45s
autoscaler               1/1     1            1           7m44s
autoscaler-hpa           1/1     1            1           7m42s
controller               1/1     1            1           7m44s
domain-mapping           1/1     1            1           7m43s
domainmapping-webhook    1/1     1            1           7m43s
net-kourier-controller   1/1     1            1           7m40s
webhook                  1/1     1            1           7m42s

Verify the serving status

$ oc get KnativeServing knative-serving -n knative-serving

NAME              VERSION   READY   REASON
knative-serving   1.6.0     True

Create a knative project

If you don't already have a project in place, create a sample one from knative's template. It's a simply containerized Python web application returning "Hello World".

serverless create --template knative-docker --path myksvc

Step into the new project folder and continue to the next steps:

cd myksvc

Make sure you have the DOCKER_HUB_USERNAME and DOCKER_HUB_PASSWORD environment variables set with your DockerHub credentials.

Patch the knative project

Update serverless-knative development dependency in your project to use the patched fork and branch:

npm i --save-dev serverless-knative@github:dmartinol/serverless-knative#master

Deploy the serverless function

Deploy the serverless function and note the returning URL:

$ serverless deploy

Building Docker image "tomerfi/myksvc-hello:1660907694762"...
Pushing "tomerfi/myksvc-hello:1660907694762" to Container Registry...
Deploying Kubernetes namespace "sls-myksvc-dev"...
Deploying Knative service for function "myksvc-hello"...
Service Information
service: myksvc
namespace: sls-myksvc-dev

Deployed functions
hello:
  - url: http://myksvc-hello.sls-myksvc-dev.example.com

If you lost the URL, get it back with oc get rt -n sls-myksvc-dev myksvc-hello -o jsonpath={.status.url}.

Get kourier's ingress external address

$ oc get svc kourier -n knative-serving -o jsonpath={.status.loadBalancer.ingress..hostname}

your-external-address.fake.co.m

Invoke the function

Use kourier's URL as the target and the knative service URL (minus the http part) as the value for the Host header:

$ curl -HHost:myksvc-hello.sls-myksvc-dev.example.com your-external-address.fake.co.m

Hello World
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment