Knative Functions enables you to easily create, build, and deploy functions on the Knative platform through simple programming. It saves you the need to worry about the complexity of Knative, Kubernetes, and containers. After you create a function, the system can automatically create a container image, and define the image build and deployment parameters to help you deploy the function as a Knative Service.
Prerequisites
Knative is deployed in the ACS cluster. For more information, see Deploy Knative.
Step 1: Download and install a command-line tool
Go to the funcrelease page and download a binary file of the
func
command-line tool based on the OS that you use.The following example shows how to install func on Linux.
After you download the binary file, run the following command to rename the file as
func
:mv <path-to-binary-file> func # <path-to-binary-file> is the local path of the binary file. Example: func_darwin_amd64 or func_linux_amd64.
Run the following command to make the file executable:
chmod +x func
Run the following command to move the func binary file to a directory that is contained in the PATH variable of Linux. This way, the binary file can be executed from any path.
mv func /usr/local/bin
Run the following command to check whether func is installed:
func version
If information about the func version is returned, func is installed.
Step 2: Create a function
Knative Functions provides templates supporting multiple programming language and invocation methods. The following templates are applicable to both CloudEvent and HTTP invocations:
The following example shows how to use a Go template to create a function.
Run the following command to create a function:
func create -l <language> <function-name>
For example, create a demo function written in Go.
func create -l go hello
Expected output:
Created go function in /usr/local/bin/hello
Run the
ls
command in thehello
directory to query the project directory that is created:func.yaml go.mod handle.go handle_test.go README.md
Run the following command in the
hello
directory to query the func.yaml file that is automatically created:cat func.yaml
Expected output:
specVersion: 0.35.0 name: hello runtime: go created: 2023-12-13T06:48:45.419587147Z
Modify the func.yaml file by customizing parameters for function building and deployment. The func.yaml file contains the configurations of the function project. For more information, see func_yaml.md.
The following code block provides an example of the func.yaml file:
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: default
registry
: the registry to which images are pushed after they are built.builderImages
: the image used by the image builder. Example:registry-cn-beijing.ack.aliyuncs.com/acs/knative-func-builder-jammy-tiny:latest
.deploy
: the configurations for deploying the function, such as the namespace where the function is deployed.
Step 3: Deploy to Knative Functions
You can deploy the function after it is created.
Run the following command to deploy a function.
By default, the deploy command deploys the function as a Knative Service that is named after the function project. The project name and registry name are used to generate the full image address when the function is built.
func deploy
Run the following command to query the function that is deployed:
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 from Knative Functions
Add the following configuration to the hosts file to point the domain name of the Knative Service to the IP address of the Knative gateway. Example:
121.xx.xxx.xx hello.default.example.com # Replace 121.xx.xxx.xx with the actual Knative gateway IP address. Replace hello.default.example.com with the domain name of the Knative Service.
Run the following command to invoke the function:
func invoke
Expected 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 shows that an HTTP request is sent to the Knative Functions and the function is successfully invoked.
Run the following command to query pods in the cluster:
kubectl get pod
Expected output:
NAME READY STATUS RESTARTS AGE hello-00001-deployment-7b65fcdc4c-gfcbp 2/2 Running 0 34
The output shows that pods are created after the invocation.
References
For more information about the common issues that you may encounter when you use Knative and the corresponding solutions, see Knative FAQ.