全部產品
Search
文件中心

Container Service for Kubernetes:基於Knative部署函數服務

更新時間:Jun 19, 2024

Knative Functions為您提供了一個簡單的方式來建立、構建和部署Knative服務。您無需深入瞭解底層技術棧(如Kubernetes、容器和Knative),通過使用Knative Functions,即可將無狀態、事件驅動的函數作為Knative服務部署到Kubernetes叢集中。

前提條件

已部署Knative

步驟一:安裝命令列工具

  1. funcrelease page頁面,下載適用於您作業系統的func二進位檔案。

    本文以Linux系統為例,說明如何安裝func命令列工具。

  2. 下載完成後,執行以下命令,將二進位檔案重新命名為func

    mv <path-to-binary-file> func # <path-to-binary-file> 表示二進位檔案的本地地址。如:func_darwin_amd64或func_linux_amd64。
  3. 執行以下命令,賦予二進位檔案可執行許可權。

    chmod +x func
  4. 執行以下命令,將func命令移動到系統PATH中的某個目錄裡,這樣才能在任何地方執行該命令。

    mv func /usr/local/bin
  5. 執行以下命令,驗證func命令列工具是否已安裝成功。

    func version

    如果顯示已安裝的func工具的版本資訊,則表明命令列工具已安裝成功。

步驟二:建立函數

Knative函數提供了建立基本函數的模板。您可以選擇函數的語言和調用方式。以下模板可用於CloudEvent和HTTP調用:

下文以Go語言的模板為例,介紹如何建立函數。

  1. 執行以下命令,建立函數。

    func create -l <language> <function-name>

    例如,建立一個Go語言的樣本函數。

    func create -l go hello

    預期輸出:

    Created go function in /usr/local/bin/hello
  2. hello目錄下執行ls命令,查看建立的Project目錄。

    func.yaml  go.mod  handle.go  handle_test.go  README.md
  3. hello目錄下執行以下命令,查看自動建立出來的func.yaml檔案。

    cat func.yaml

    預期輸出:

    specVersion: 0.35.0
    name: hello
    runtime: go
    created: 2023-12-13T06:48:45.419587147Z
  4. 編輯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等。

步驟三:部署函數

在建立函數之後,可以直接部署函數。

  1. 執行以下命令,在函數專案中部署函數。

    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
  2. 執行以下命令,查看函數部署資訊。

    func info

    預期輸出:

    Function name:
      hello
    Function is built in image:
    
    Function is deployed in namespace:
      default
    Routes:
      http://hello.default.example.com

步驟四:調用函數

  1. 將Knative訪問網關地址與需要訪問的網域名稱進行Host綁定,在Hosts檔案中添加綁定資訊。綁定範例如下。

    121.xx.xxx.xx hello.default.example.com # 121.xx.xxx.xx請替換為Knative服務的網關地址,hello.default.example.com請替換為您的服務網域名稱。
  2. 執行以下命令,調用函數。

    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函數,函數調用成功。

  3. 執行以下命令,查看叢集中Pod的狀態。

    kubectl get pod

    預期輸出:

    NAME                                      READY   STATUS    RESTARTS   AGE
    hello-00001-deployment-7b65fcdc4c-gfcbp   2/2     Running   0          34

    結果表明,函數調用成功觸發了Pod的建立。