创建Custom Container函数

更新时间: 2024-01-19 18:18:21

当函数计算平台的标准运行时无法满足您的业务需求时,可以选择自定义容器函数。本文介绍如何在函数计算控制台或使用Serverless Devs创建Custom Container函数。

注意事项

在函数计算中,创建Custom Container函数必须使用相同地域内阿里云镜像服务仓库中的镜像。针对搭载Apple芯片的Mac电脑(或其他ARM架构的机器),构建镜像时需要指定镜像的编译平台为Linux/Amd64,示例代码如docker build --platform linux/amd64 -t $IMAGE_NAME .

前提条件

在控制台创建函数

步骤一:创建服务并为服务设置权限

  1. 函数计算控制台创建服务。

    具体操作,请参见创建服务

  2. 为目标服务绑定权限策略AliyunContainerRegistryReadOnlyAccess或者AliyunContainerRegistryFullAccess

    具体操作,请参见授予函数计算访问其他云服务的权限函数计算需要使用上述策略中的权限获取容器镜像服务中默认实例的临时账号,然后利用该临时账号推送位于您的私有镜像仓库中的镜像。

步骤二:创建函数

  1. 登录函数计算控制台,在左侧导航栏,单击服务及函数
  2. 在顶部菜单栏,选择地域,然后在服务列表页面,单击目标服务。
  3. 函数管理页面,单击创建函数
  4. 创建函数页面,选择使用容器镜像创建,按需配置以下配置项,然后单击创建

    • 基本设置:配置函数的基本信息,包括函数名称、Web Server模式和请求处理程序类型。

      配置项

      说明

      函数名称

      填写自定义的函数名称。如不填写,函数计算会自动为您创建。

      Web Server 模式

      选择是否启用Web Server模式。取值说明如下:

      • :启用Web Server模式。您需要在您的容器镜像中实现一个Web Server来监听端口并处理请求。

      • :启用非Web Server模式。此时,请求处理程序类型仅支持处理事件请求,成功处理完请求后,您需要主动退出进程,并且需将ExitCode置为0。另外,请求的事件参数会以环境变量的形式传递给函数。

      请求处理程序类型

      选择函数的类型,即处理事件请求的函数还是HTTP函数。取值说明如下:

      • 处理事件请求:通过定时器、调用API/SDK或其他阿里云服务的触发器来触发函数执行。

      • 处理 HTTP 请求:用于处理HTTP请求或WebSocket请求的函数。如果您的使用场景是Web场景,建议使用自定义运行时创建

    • 镜像配置:配置创建函数的镜像。

      配置项

      说明

      镜像选择方式

      您可以使用示例镜像或者您自己的镜像创建函数。

      • 使用示例镜像:选择函数计算自带的示例镜像。

      • 使用 ACR 中的镜像:单击配置项容器镜像下方的选择 ACR 中的镜像,在弹出的选择容器镜像面板,选择已创建的容器镜像实例ACR 镜像仓库,然后在下方选择镜像区域找到目标镜像并在其右侧操作列单击选择

        说明

        针对企业版容器镜像服务,函数计算会使用您在此处选择的镜像的Tag创建函数实例。

        • 如果您使用的是企业版镜像(高级版和标准版),建议您打开镜像版本不可变开关。否则若该镜像Tag在其他地方被更新,函数计算会拉取最新的镜像数据来启动实例。更多信息,请参见开启镜像版本不可变

        • 如果您使用的是企业版镜像(基础版)或ACR个人版,您在该镜像仓库更新同名镜像Tag后,需要在函数计算控制台以该镜像信息重新更新函数后,函数计算才会使用此新镜像启动实例。

      启动命令

      容器的启动命令。如果不填写,则默认使用镜像中的Entrypoint或者CMD。

      监听端口

      容器镜像中的HTTP Server所监听的端口。默认端口为9000。如果不填写,默认使用非交互模式运行容器镜像。

    • 高级配置:配置函数的实例相关信息、执行超时时间和请求处理程序。

      配置项

      说明

      是否使用GPU

      根据您的业务情况,选择是否使用GPU实例。默认使用弹性实例,不使用GPU实例。更多信息,请参见实例类型。关于各种实例类型的计费详情,请参见计费概述

      • 使用GPU

      • 不使用GPU

      规格方案

      • 选择使用GPU

        选择GPU 卡型,然后根据您的业务情况,选择合理的GPU 显存规格函数计算将根据您选择的GPU规格自动选定vCPU 规格内存规格,不支持灵活配比。

      • 选择不使用GPU

        根据您的业务情况,选择或手动输入合理的vCPU 规格内存规格组合。

      关于各资源使用的计费详情,请参见计费概述

      说明

      vCPU大小(单位为核)与内存大小(单位为GB)的比例必须设置在1∶1到1∶4之间。

      临时硬盘大小

      根据您的业务情况,选择硬盘大小。函数计算为您提供512 MB以内的磁盘免费使用额度。更多信息,请参见计费概述

      实例并发度

      Web Server 模式选择时,无需配置。设置函数实例的并发度。具体信息,请参见设置实例并发度

      执行超时时间

      设置超时时间。默认为60秒,最长为86400秒,推荐您设置为600秒。

      说明

      超过设置的超时时间,函数将以执行失败结束。如需更长的超时时间限制,请加入钉钉用户群(钉钉群号11721331)申请。

      时区

      选择函数的时区。此处设置函数的时区后,将自动为函数添加一条环境变量TZ,其值为您设置的目标时区。

    • 环境变量:设置函数运行环境中的环境变量。更多信息,请参见环境变量

    • 触发器配置:设置函数的触发器,您可以使用触发器触发函数执行。更多信息,请参见触发器管理

    创建完成后,您可以在目标服务下的函数列表中查看和更新已创建的函数。

说明

更新函数时,只能变更已设置的监听端口,不能删除或添加额外的监听端口。如果创建函数时,配置了监听端口,更新该函数时,不指定监听端口,将保留创建函数时的监听端口。

使用Serverless Devs创建

使用Serverless Devs可以一键构建、推送容器镜像并部署函数。

  1. 执行以下命令,初始化项目。需根据提示输入ACR镜像仓库地址。

    sudo s init start-fc-custom-container-event-nodejs14

    输出示例:

     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     Please input your project name (init dir) start-fc-custom-container-event-nodejs14
     file decompression completed
    
         ____  _     _ ___  _ _     _        _____ ____
        /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
        | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
        | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
        \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
     please select credential alias default
    
        Welcome to the Aliyun FC start application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
             ACR: https://cr.console.aliyun.com/
    
         * 额外说明:s.yaml中声明了actions:
            部署前执行:sudo s build --use-docker --dockerfile ./code/Dockerfile
           如果不需要每次都构建项目,或者部署前不需要构建,或者已经手动构建了,可以注释掉这部分内容。
           > PS:部署的时候还需要修改s.yaml中image字段为自己的ACR配置的地址。
         * 项目初始化完成,您可以直接进入项目目录下,并使用s deploy进行项目部署。
    
     Thanks for using Serverless-Devs
     You could [cd /test/test1/start-fc-custom-container-event-nodejs14] and enjoy your serverless journey!
     If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:https://github.com/Serverless-Devs/Serverless-Devs
     Do you want to deploy the project immediately? No
  2. 执行以下命令,进入项目目录。

    cd start-fc-custom-container-event-nodejs14
  3. 编辑s.yaml文件。关于YAML文件的参数详解,请参见YAML规范

  4. 执行以下命令,部署项目。

    sudo s deploy

    输出示例:

    [2021-12-15 07:54:30] [INFO] [S-CLI] - Start ...
    [2021-12-15 07:54:30] [INFO] [S-CLI] - Start the pre-action
    [2021-12-15 07:54:30] [INFO] [S-CLI] - Action: s build --use-docker --dockerfile ./code/Dockerfile
    [2021-12-15 07:54:31] [INFO] [S-CLI] - Start ...
    [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Build artifact start...
    [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Use docker for building.
    [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Building image...
    Sending build context to Docker daemon   5.12kB
    Step 1/7 : FROM node:14.5.0-alpine3.11
     ---> 5d97b3d11dc1
    ......
    Step 7/7 : ENTRYPOINT [ "node", "server.js" ]
     ---> Using cache
     ---> a5ef1c015e7e
    Successfully built a5ef1c015e7e
    Successfully tagged registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx
    SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
    Build image(registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx) successfully
    [2021-12-15 07:54:33] [INFO] [FC-BUILD] - Build artifact successfully.
    
    Tips for next step
    ======================
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
    [2021-12-15 07:54:33] [INFO] [S-CLI] - End the pre-action
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using access alias: default
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Checking Service hello-world-service exists
    [2021-12-15 07:54:35] [INFO] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    [2021-12-15 07:54:35] [INFO] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-12-15 07:54:35] [INFO] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-12-15 07:54:35] [INFO] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-12-15 07:54:35] [INFO] [FC-DEPLOY] - Checking Function nodejs14-event-function exists
    [2021-12-15 07:54:36] [INFO] [FC-DEPLOY] - Using image registry: registry.cn-hangzhou.aliyuncs.com
    [2021-12-15 07:54:36] [INFO] [FC-DEPLOY] - Try to use a temporary token for docker login
    Login to registry: registry.cn-hangzhou.aliyuncs.com with user: cr_temp_user
    Pushing docker image: registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx...
    The push refers to repository [registry.cn-hangzhou.aliyuncs.com/fc-example/test]
    cdf38e7753b7: Layer already exists                                                                                                                                                           43128f71725b: Layer already exists                                                                                                                                                           0fb36a16ab83: Layer already exists                                                                                                                                                           dd966b9fd474: Layer already exists                                                                                                                                                           a1915d7a1111: Layer already exists                                                                                                                                                           c4491b3ee709: Layer already exists                                                                                                                                                           9fb10d900487: Layer already exists                                                                                                                                                           3e207b409db3: Layer already exists                                                                                                                                                           nginx: digest: sha256:02b69157def85ceb72f32cb1c5845d00e1d8df19caf6eaf720a9bc77bb57db76 size: 1991
    √ Make service hello-world-service success.
    √ Make function hello-world-service/nodejs14-event-function success.
    [2021-12-15 07:54:39] [INFO] [FC-DEPLOY] - Checking Service hello-world-service exists
    [2021-12-15 07:54:39] [INFO] [FC-DEPLOY] - Checking Function nodejs14-event-function exists
    
    There is auto config in the service: hello-world-service
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       nodejs14-event-function
        runtime:    custom-container
        handler:    not-used
        memorySize: 256
        timeout:    60
  5. 执行以下命令,调试函数。

    sudo s invoke -e "{\"key\":\"val\"}"

    输出示例:

    [2021-12-15 08:00:17] [INFO] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 768945c8-f92d-428e-89c2-ecd50883****
    {"key":"val"}
    FC Invoke End RequestId: 768945c8-f92d-428e-89c2-ecd50883****
    
    Duration: 3.05 ms, Billed Duration: 4 ms, Memory Size: 256 MB, Max Memory Used: 10.77 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    OK
    
    
    End of method: invoke

相关文档

  • 如果您要使用GPU实例,则只能创建Custom Container函数。关于GPU实例规格的选择,请参见管理函数

  • 使用Custom Container函数,容器镜像依赖的基础环境会带来额外的数据下载和解压的时间,为了降低冷启动时间,请参见冷启动优化最佳实践

  • 您也可以调用API来创建函数,更多信息,请参见CreateFunction - 创建函数

  • 关于函数计算提供的内置运行时、自定义运行时和自定义容器运行时适用场景及差异,请参见函数运行时选型

阿里云首页 函数计算(旧版) 相关技术圈