Knative Functions provides a simplified programming model for creating and deploying functions on Knative, abstracting Knative, Kubernetes, and container details. Create your function, let the platform build the container image, customize image build and deployment settings, and deploy it as a Knative Service.
Prerequisites
Make sure you have:
-
Knative is deployed in your ACK cluster. See Deploy and manage Knative.
Step 1: Install the func CLI
Download the func binary for your operating system from the func release page.
Example for Linux:
-
Rename the binary to
func:mv <path-to-binary-file> funcReplace
<path-to-binary-file>with the downloaded binary path, such asfunc_linux_amd64. -
Make the binary executable:
chmod +x func -
Move the binary to a directory in your
PATH:mv func /usr/local/bin -
Verify the installation:
func versionVersion output confirms successful installation.
Step 2: Create a function
Knative Functions supports the following runtimes, all compatible with HTTP and CloudEvent invocations:
This example uses Go.
-
Create a function:
func create -l <language> <function-name>Example: Create a Go function named
hello:func create -l go helloExpected output:
Created go function in /usr/local/bin/hello -
List the files in the
hellodirectory:ls hello/Expected output:
func.yaml go.mod handle.go handle_test.go README.md -
Review the generated
func.yamlin thehellodirectory:cat func.yamlExpected output:
specVersion: 0.35.0 name: hello runtime: go created: 2023-12-13T06:48:45.419587147Z -
Edit
func.yamlto configure the registry and deployment settings. Complete example:specVersion: 0.35.0 name: hello runtime: go created: 2023-11-29T14:47:34.101658+08:00 registry: registry.cn-beijing.aliyuncs.com/knative-release-xxx image: registry.cn-beijing.aliyuncs.com/knative-release-xxx/hello:latest build: builderImages: pack: registry-cn-beijing.ack.aliyuncs.com/acs/knative-func-builder-jammy-tiny:latest deploy: namespace: defaultKey fields:
Field Description registryContainer registry for built images. Example: registry.cn-beijing.aliyuncs.com/knative-release-xxxbuild.builderImages.packBuilder image for the function. Example: registry-cn-beijing.ack.aliyuncs.com/acs/knative-func-builder-jammy-tiny:latestdeploy.namespaceTarget Kubernetes namespace for the function. See the
func.yamlreference for all available options.
Step 3: Deploy the function
Run func deploy from the hello directory. The command uses the project name as the Knative Service name and combines it with the registry to construct the image address.
func deploy
Verify the deployment:
func info
Expected output:
Function name:
hello
Function is built in image:
Function is deployed in namespace:
default
Routes:
http://hello.default.example.com
Step 4: Invoke the function
-
Add a hosts file entry to map the Knative Service domain to the gateway IP:
121.xx.xxx.xx hello.default.example.comReplace
121.xx.xxx.xxwith your Knative gateway IP andhello.default.example.comwith your deployment domain. -
Invoke the function:
func invokeExpected output:
POST / HTTP/1.1 hello.default.example.com Content-Type: application/json Forwarded: for=192.168.102.101;proto=http Knative-Serving-Default-Route: true X-Forwarded-Proto: http User-Agent: Go-http-client/1.1 Content-Length: 25 Accept-Encoding: gzip K-Proxy-Request: activator X-Forwarded-For: 192.168.102.101, 192.168.102.97 X-Request-Id: 3d82cfc8-f9df-4935-84cd-c6561b4587f6 Body:The output confirms the function received and handled the HTTP request.
-
Verify that function pods were created:
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE hello-00001-deployment-7b65fcdc4c-gfcbp 2/2 Running 0 34
Next steps
See Knative FAQ for common questions about Knative in ACK.