Knative Functions為您提供了一個簡單的方式來創建、構建和部署Knative服務。您無需深入了解底層技術棧(如Kubernetes、容器和Knative),通過使用Knative Functions,即可將無狀態、事件驅動的函數作為Knative服務部署到Kubernetes集群中。
前提條件
步驟一:安裝命令行工具
在funcrelease page頁面,下載適用于您操作系統的
func
二進制文件。本文以Linux系統為例,說明如何安裝func命令行工具。
下載完成后,執行以下命令,將二進制文件重新命名為
func
。mv <path-to-binary-file> func # <path-to-binary-file> 表示二進制文件的本地地址。如:func_darwin_amd64或func_linux_amd64。
執行以下命令,賦予二進制文件可執行權限。
chmod +x func
執行以下命令,將func命令移動到系統PATH中的某個目錄里,這樣才能在任何地方執行該命令。
mv func /usr/local/bin
執行以下命令,驗證func命令行工具是否已安裝成功。
func version
如果顯示已安裝的func工具的版本信息,則表明命令行工具已安裝成功。
步驟二:創建函數
Knative函數提供了創建基本函數的模板。您可以選擇函數的語言和調用方式。以下模板可用于CloudEvent和HTTP調用:
下文以Go語言的模板為例,介紹如何創建函數。
執行以下命令,創建函數。
func create -l <language> <function-name>
例如,創建一個Go語言的示例函數。
func create -l go hello
預期輸出:
Created go function in /usr/local/bin/hello
在
hello
目錄下執行ls
命令,查看創建的Project目錄。func.yaml go.mod handle.go handle_test.go README.md
在
hello
目錄下執行以下命令,查看自動創建出來的func.yaml文件。cat func.yaml
預期輸出:
specVersion: 0.35.0 name: hello runtime: go created: 2023-12-13T06:48:45.419587147Z
編輯func.yaml文件,自定義配置構建和部署的參數,以供后續使用。func.yaml文件包含函數項目的配置信息。詳細信息,請參見func_yaml.md。
func.yaml文件的示例代碼如下所示:
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
:表示構建之后,推送鏡像的目標倉庫地址。builderImages
:表示鏡像構建所使用的容器,例如registry-cn-beijing.ack.aliyuncs.com/acs/knative-func-builder-jammy-tiny:latest
。deploy
:表示部署相關的配置,如Namespace等。
步驟三:部署函數
在創建函數之后,可以直接部署函數。
執行以下命令,在函數項目中部署函數。
deploy命令默認使用函數項目名稱作為Knative服務名稱。構建函數時,將使用項目名稱和鏡像倉庫名稱來構造函數的完整鏡像地址。
func deploy
預期輸出:
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
執行以下命令,查看函數部署信息。
func info
預期輸出:
Function name: hello Function is built in image: Function is deployed in namespace: default Routes: http://hello.default.example.com
步驟四:調用函數
將Knative訪問網關地址與需要訪問的域名進行Host綁定,在Hosts文件中添加綁定信息。綁定樣例如下。
121.xx.xxx.xx hello.default.example.com # 121.xx.xxx.xx請替換為Knative服務的網關地址,hello.default.example.com請替換為您的服務域名。
執行以下命令,調用函數。
func invoke
預期輸出:
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:
結果表明,一個HTTP請求被發送到了Knative函數,函數調用成功。
執行以下命令,查看集群中Pod的狀態。
kubectl get pod
預期輸出:
NAME READY STATUS RESTARTS AGE hello-00001-deployment-7b65fcdc4c-gfcbp 2/2 Running 0 34
結果表明,函數調用成功觸發了Pod的創建。