Knative is integrated with Function Compute. You can use Knative to deploy a function as a Service by using the following methods: create a deployment file that contains the function code, pull the function code file from Object Storage Service (OSS), and use a container image. This topic describes how to use Knative to deploy a function as a Service.

Create a deployment file that contains the function code

  1. Create a file named coffee.yaml.
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: coffee
      annotations:
        workload.serving.knative.aliyun.com/class: "fc"
    spec:
      template:
        metadata:
          annotations:
            fc.revision.serving.knative.aliyun.com/code-space: "inline"
        spec:
          containers:
            - image: nodejs8
              command:
              - |
                var getRawBody = require('raw-body')
                var version = 'coffee-default'
                module.exports.handler = function (request, response, context) {
                    var respBody = new Buffer('Hello ' + version + '\n')
                    response.setStatusCode(200)
                    response.setHeader('content-type', 'application/json')
                    response.send(respBody)
                };

    The image parameter specifies that Node.js is used to deploy the function and the command parameter specifies the code of the function. Modify these parameters based on the function that you want to deploy.

  2. Deploy the function as a Service.
    kubectl apply -f coffee.yaml

Pull the function code file that is archived by Function Compute from OSS

You can deploy a function as a Service by downloading the archive of the function code file from OSS. The function code file is archived by Function Compute.

  1. Create a file named coffee-oss.yaml.
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: coffee
      annotations:
        workload.serving.knative.aliyun.com/class: "fc"
    spec:
      template:
        metadata:
          annotations:
            fc.revision.serving.knative.aliyun.com/code-space: "oss"
        spec:
          containers:
            - image: nodejs8
              command:
              - fc-zip
              - knative-demo/node-demo.zip

    The image and command parameters specify that a function code file that is archived by Function Compute is pulled from OSS. The following content describes the image and command parameters:

    • image: the runtime of the function, for example, nodejs8.
    • command: an array that consists of BucketName and ObjectName.
  2. Deploy the function as a Service.
    kubectl apply -f coffee-oss.yaml

Use a container image

Function Compute provides a custom container runtime to simplify developer experience and improve the efficiency of development and delivery. Developers can interact with Function Compute over HTTP and deliver functions as container images. For more information about the custom container runtime, see Overview.

Limits

Configuration item Limit
Image size
  • If the memory required to run a function is less than 1 GB, the size of the container image cannot exceed 256 MB before it is decompressed.
  • If the memory required to run a function is 1 GB or greater, the size of the container image cannot exceed 1,024 MB before it is decompressed.
Image repository Only image repositories of Container Registry Personal Edition are supported. Other image repositories will soon be supported. For more information about Container Registry Personal Edition, see What is Container Registry.
Supported images Only images in a private image repository that belongs to the same account and region as the Service are supported. Public images will soon be supported.
Read and write permissions of the container The run-as-user UID of the container is randomly selected from 10000 to 10999. By default, the container has the write permissions on the /tmp directory. The read and write permissions on other directories depend on the file system of the container image. If a UID does not have permissions to read or write a file, you can modify the Dockerfile.
Storage space limit of the writable container layer The data that is generated by the writable container layer cannot exceed 512 MB.
Permission policies When you create a Service, you must attach the AliyunContainerRegistryReadOnlyAccess or AliyunContainerRegistryFullAccess permission policy to the service linked role. You must create a service linked role in Resource Access Management (RAM) and then attach the AliyunContainerRegistryReadOnlyAccess or AliyunContainerRegistryFullAccess permission policy to the service linked role. In the Basic Information section on the RAM Roles page, you can obtain the Alibaba Cloud Resource Name (ARN). For more information, see Grant permissions to a RAM role and RAM role overview.
  1. Create a file named coffee-image.yaml.
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: coffee
      annotations:
        workload.serving.knative.aliyun.com/class: "fc"
    spec:
      template:
        metadata:
          annotations:
            fc.revision.serving.knative.aliyun.com/code-space: "image"
            fc.revision.serving.knative.aliyun.com/role-arm: "acs:ram::1041208914252405:role/fc-yuanyi-test"
        spec:
          containers:
            - image: registry.cn-shenzhen.aliyuncs.com/aliknative/nodejs-express:v3.0

    The annotations and image parameters specify that a container image is used to deploy the function as a Service. The following describes the parameters:

    • fc.revision.serving.knative.aliyun.com/code-space: image: the type of container image.
    • fc.revision.serving.knative.aliyun.com/role-arm: the ARN that is used to attach the AliyunContainerRegistryReadOnlyAccess or AliyunContainerRegistryFullAccess permission policy.
    • image: the name of the container image.
  2. Deploy the function as a Service.
    kubectl apply -f coffee-oss.yaml

Verify the result

You can check whether a function is deployed as a Service. In the following example, the function is deployed by using a deployment file that contains the function code.

  1. Query the status of the Service.
    kubectl get ksvc

    Expected output:

    NAME     URL                                                                                                                  LATESTCREATED   LATESTREADY    READY   REASON
    coffee   https://198639303048****.cn-beijing.fc.aliyuncs.com/2016-08-15/proxy/kn_default_coffee.http-prd/kn_default_coffee/   coffee-5bqdr    coffee-5bqdr   True
  2. Run the curl command to access the Service.
    curl https://198639303048****.cn-beijing.fc.aliyuncs.com/2016-08-15/proxy/kn_default_coffee.http-prd/kn_default_coffee/

    Expected output:

    Hello coffee-default
  3. Check the Service and function in the Function Compute console.
    1. Log on to the Function Compute console.
    2. In the top navigation bar, select a region.
    3. In the left-side navigation pane, click Services and Functions. On the Services and Functions page, the kn_default_coffee Service appears.
    4. Click kn_default_coffee. On the Functions tab, the kn_default_coffee function appears.