文档

基于Knative部署函数服务

更新时间:

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的创建。

  • 本页导读 (1)
文档反馈