Knative Functions lets you create, build, and deploy functions on Knative without managing Kubernetes or container infrastructure. After you scaffold a function project, the system builds a container image automatically and deploys it as a Knative Service.
Prerequisites
Before you begin, ensure that you have Knative deployed in your ACS cluster. See Deploy Knative.
Step 1: Install the func CLI
Download the func binary from the func releases page 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
functo a directory on your PATH so it can be run from anywhere:mv func /usr/local/bin -
Verify the installation:
func versionIf version information is returned, the installation is complete.
Step 2: Create a function
Knative Functions supports the following runtimes for both HTTP and CloudEvent invocations:
| Runtime | Template reference |
|---|---|
| Node.js | nodejs.md |
| Python | python.md |
| Go | golang.md |
| Quarkus | quarkus.md |
| Rust | rust.md |
| Spring Boot | springboot.md |
| TypeScript | typescript.md |
The following example creates a Go function named hello:
-
Scaffold the function project:
func create -l go helloExpected output:
Created go function in /usr/local/bin/hello -
Review the generated project files in the
hellodirectory:func.yaml go.mod handle.go handle_test.go README.md -
View the auto-generated
func.yaml:cat func.yamlExpected output:
specVersion: 0.35.0 name: hello runtime: go created: 2023-12-13T06:48:45.419587147Z -
Edit
func.yamlto configure the image registry and deployment namespace. The following example shows a customized configuration: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: defaultField Description registryThe container registry where the built image is pushed builderImagesThe builder image used to build the function. Example: registry-cn-beijing.ack.aliyuncs.com/acs/knative-func-builder-jammy-tiny:latestdeployDeployment configuration, including the namespace where the function is deployed For all available fields, see func_yaml.md.
Step 3: Deploy the function
Run the following command from inside the hello directory:
func deploy
The deploy command builds the container image, pushes it to the registry specified in func.yaml, and deploys the function as a Knative Service. The service name matches the function project name, and the full image address is generated from the project name and registry.
Expected output:
Building function image
Still building
Still building
Yes, still building
Don't give up on me
Still building
This is taking a while
Still building
Still building
Yes, still building
Function built: registry.cn-beijing.aliyuncs.com/knative-release-xxx/hello:latest
Pushing function image to the registry "registry.cn-beijing.aliyuncs.com" using the "xxx" user credentials
Deploying function to the cluster
Function deployed in namespace "default" and exposed at URL:
http://hello.default.example.com
To verify the deployment, run:
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 the HTTP request reached Knative Functions and the function was invoked successfully.
-
Confirm that pods were created after invocation:
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE hello-00001-deployment-7b65fcdc4c-gfcbp 2/2 Running 0 34
What's next
For common issues and solutions when using Knative, see Knative FAQ.