Knative Functions provides a simplified programming model for creating and deploying serverless functions on Knative. You write the function code, and Knative Functions automatically builds a container image and deploys it as a Knative Service — no Kubernetes or container expertise required.
Prerequisites
Before you begin, make sure you have:
-
Knative deployed in your ACK cluster. For setup instructions, see Deploy and manage Knative
Step 1: Install the func CLI
Download the func binary from the func release page and select the binary for your operating system.
The following example installs func on Linux:
-
Rename the binary to
func:mv <path-to-binary-file> funcReplace
<path-to-binary-file>with the path to the downloaded binary, for example,func_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 versionIf version information is returned,
funcis installed successfully.
Step 2: Create a function
Knative Functions supports the following runtimes, all of which work with both HTTP and CloudEvent invocations:
The following steps use Go as an example.
-
Create a function:
func create -l <language> <function-name>For example, create a Go function named
hello:func create -l go helloExpected output:
Created go function in /usr/local/bin/hello -
List the project files in the
hellodirectory:ls hello/Expected output:
func.yaml go.mod handle.go handle_test.go README.md -
Review the auto-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. The following is an example of a completefunc.yaml: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 registryThe container registry where built images are pushed. Example: registry.cn-beijing.aliyuncs.com/knative-release-xxxbuild.builderImages.packThe builder image used to build the function. Example: registry-cn-beijing.ack.aliyuncs.com/acs/knative-func-builder-jammy-tiny:latestdeploy.namespaceThe Kubernetes namespace where the function is deployed For a full list of
func.yamloptions, see thefunc.yamlreference.
Step 3: Deploy the function
Run func deploy from inside the hello directory. The command uses the function project name as the Knative Service name and combines the project name with the registry to construct the full image address.
func deploy
After deployment, verify the function details:
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 an entry to your hosts file to route the Knative Service domain name to the Knative gateway IP address:
121.xx.xxx.xx hello.default.example.comReplace
121.xx.xxx.xxwith the actual Knative gateway IP address andhello.default.example.comwith the domain name shown in the deployment output. -
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 that an HTTP request was successfully sent to and handled by the function.
-
Verify that pods were created for the function:
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE hello-00001-deployment-7b65fcdc4c-gfcbp 2/2 Running 0 34
What's next
For answers to common questions about Knative in ACK, see Knative FAQ.