Knative Functions provides a simple way to create, build, and deploy Knative Services. You can use Knative Functions to deploy stateless, event-driven functions as Knative Services to a Kubernetes cluster without the need to master the underlying technologies, such as Kubernetes, containers, and Knative.
Prerequisites
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 that can be used to create basic functions. You can select the following templates based on the programming language and invocation method that you want to use. The following templates allow you to invoke a function by using CloudEvent and HTTP requests.
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 a function
You can deploy a 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
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
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 the function
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 function 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.