Knative Functions provides a simplified programming platform for creating and deploying functions in Knative, frees you from the underlying details of Knative, Kubernetes, and containers. You only need to create your function, and the system will automatically generate the container image. After customizing the relevant parameters for image building and deployment, you can complete the deployment of the function, which will exist in the form of a Knative Service.
Prerequisites
Knative is deployed in your cluster. For more information, see Deploy and manage Knative.
Step 1: Download and install a command-line tool
Go to the funcrelease page and download a binary file of the
funccommand-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 funcRun 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/binRun the following command to check whether func is installed:
func versionIf 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 helloExpected output:
Created go function in /usr/local/bin/helloRun the
lscommand in thehellodirectory to query the project directory that is created:func.yaml go.mod handle.go handle_test.go README.mdRun the following command in the
hellodirectory to query the func.yaml file that is automatically created:cat func.yamlExpected output:
specVersion: 0.35.0 name: hello runtime: go created: 2023-12-13T06:48:45.419587147ZModify 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: defaultregistry: 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 deployRun the following command to query the function that is deployed:
func infoExpected 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 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 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 podExpected output:
NAME READY STATUS RESTARTS AGE hello-00001-deployment-7b65fcdc4c-gfcbp 2/2 Running 0 34The output shows that pods are created after the invocation.
References
For more information about answers to some frequently asked questions (FAQs) about Knative in Container Service for Kubernetes (ACK), see Knative FAQ.