Knative Functions provides a simplified programming model for creating and deploying functions on Knative, hiding many underlying details of Knative, Kubernetes, and containers. You create a function and the system automatically generates the container image. After you customize the parameters for image building and deployment, you can deploy the function as a Knative Service.
Prerequisites
Knative is deployed in your cluster. For more information, see Deploy and manage Knative components.
Step 1: Install the command-line tool
On the func release page, download the
funcbinary file for your operating system.This topic uses Linux as an example to show how to install the func command-line tool.
After the download is complete, run the following command to rename the binary file to
func.mv <path-to-binary-file> func # <path-to-binary-file> indicates the local path of the binary file, for example, func_darwin_amd64 or func_linux_amd64.Run the following command to grant executable permissions on the binary file.
chmod +x funcRun the following command to move the func command to a directory in the system PATH so that you can run the command from anywhere.
mv func /usr/local/binRun the following command to verify that the func command-line tool is installed.
func versionIf the version information of the installed func tool is displayed, the command-line tool is installed.
Step 2: Create a function
Knative Functions provides templates for multiple languages and invocation methods. The following templates are available for CloudEvent and HTTP invocations.
The following example uses the Go template to show how to create a function.
Run the following command to create a function.
func create -l <language> <function-name>For example, create a sample function in Go.
func create -l go helloExpected output:
Created go function in /usr/local/bin/helloIn the
hellodirectory, run thelscommand to view the created project directory.func.yaml go.mod handle.go handle_test.go README.mdIn the
hellodirectory, run the following command to view the automatically created func.yaml file.cat func.yamlExpected output:
specVersion: 0.35.0 name: hello runtime: go created: 2023-12-13T06:48:45.419587147ZEdit the func.yaml file to customize the parameters for building and deployment for later use. The func.yaml file contains the configuration information of the function project. For details, see func_yaml.md.
The following is a sample 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 target repository address to which the image is pushed after the build.builderImages: the container used for image building, for example,registry-cn-beijing.ack.aliyuncs.com/acs/knative-func-builder-jammy-tiny:latest.deploy: the deployment-related configurations, such as the namespace.
Step 3: Deploy Knative Functions
After you create a function, you can deploy Knative Functions directly.
Run the following command to deploy the function in the function project.
deployuses the function project name as the Knative Service name by default. When the function is built, the project name and the image repository name are used to construct the full image address of the function.func deployRun the following command to view function deployment information.
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 Knative Functions
Map the Knative access gateway address to the domain name that you want to access by adding an entry to the hosts file.
The following is a sample binding.
121.xx.xxx.xx hello.default.example.com # 121.xx.xxx.xx: replace it with the gateway address of the Knative Service, and replace hello.default.example.com with your service domain name.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 expected output indicates that an HTTP request was sent to Knative Functions and the function invocation succeeded.
Run the following command to view the status of the pods in the cluster.
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE hello-00001-deployment-7b65fcdc4c-gfcbp 2/2 Running 0 34The expected output indicates that the function invocation triggered the creation of a pod.
References
For common issues that you may encounter when using Knative and their solutions, see Knative FAQ.