本文介绍如何在函数计算控制台或使用Serverless Devs创建Custom Container函数。
前提条件
- 容器镜像服务
- Serverless Devs
在控制台创建函数
本文以在/tmp目录中执行为例,介绍如何创建函数。本文示例中,函数计算的地域为华南1(深圳),镜像仓库名称为nodejs-express。
步骤一:推送镜像至默认实例镜像仓库
- 执行以下命令,进入/tmp目录。
cd /tmp
- 在/tmp目录,执行以下命令克隆示例工程。
git clone https://github.com/awesome-fc/custom-container-docs.git
- 执行以下命令,进入项目目录。
cd custom-container-docs/nodejs-express
- 执行以下命令,指定镜像仓库。
export IMAGE_NAME="registry.cn-shenzhen.aliyuncs.com/fc-demo/nodejs-express:v0.2"
- 执行以下命令,打包镜像。
sudo docker build -t $IMAGE_NAME .
说明 针对搭载Apple芯片的Mac电脑(或其他ARM架构的机器),构建镜像时需要指定镜像的编译平台为Linux/Amd64,实现跨平台编译,示例代码如docker build --platform linux/amd64 -t $IMAGE_NAME .
。 - 执行以下命令,推送镜像。
sudo docker push $IMAGE_NAME
步骤二:创建服务并为服务设置权限
- 在函数计算控制台创建服务。具体操作,请参见创建服务。
- 为目标服务绑定权限策略AliyunContainerRegistryReadOnlyAccess或者AliyunContainerRegistryFullAccess。具体操作,请参见授予函数计算访问其他云服务的权限。函数计算需要使用上述策略中的权限获取容器镜像服务中默认实例的临时账号,然后利用该临时账号推送位于您的私有镜像仓库中的镜像。
步骤三:创建函数
- 登录函数计算控制台,在左侧导航栏,单击服务及函数。
- 在顶部菜单栏,选择地域,然后在服务列表页面,单击目标服务。
- 在函数管理页面,单击创建函数。
- 在创建函数页面,选择使用容器镜像创建,按需配置以下配置项,然后单击创建。
- 基本设置:配置函数的基本信息,包括函数名称、Web Server模式和请求处理程序类型。
配置项 说明 函数名称 填写自定义的函数名称。如不填写,函数计算会自动为您创建。 Web Server 模式 选择是否启用Web Server模式。取值说明如下: - 是:启用Web Server模式。您需要在您的容器镜像中实现一个Web Server来监听端口并处理请求。
- 否:启用非Web Server模式。此时,请求处理程序类型仅支持处理事件请求,成功处理完请求后,您需要主动退出进程,并且需将
ExitCode
置为0。另外,请求的事件参数会以环境变量的形式传递给函数。
请求处理程序类型 选择函数的类型,即处理事件请求的函数还是HTTP函数。取值说明如下: - 处理事件请求:通过定时器、调用API/SDK或其他阿里云服务的触发器来触发函数执行。
- 处理 HTTP 请求:用于处理HTTP请求或Websocekt请求的函数。如果您的使用场景是Web场景,建议使用自定义运行时创建。
- 镜像配置:配置创建函数的镜像。
配置项 说明 镜像选择方式 您可以使用示例镜像或者您自己的镜像创建函数。 - 使用示例镜像:选择函数计算自带的示例镜像。
- 使用 ACR 中的镜像:单击配置项容器镜像下方的选择 ACR 中的镜像,在弹出的选择容器镜像面板,选择已创建的容器镜像实例和ACR 镜像仓库,然后在下方选择镜像区域找到目标镜像并在其右侧操作列单击选择。说明 针对企业版容器镜像服务,函数计算会使用您在此处选择的镜像的Tag创建函数实例。因此,如果您使用容器镜像服务的企业版实例,建议您打开镜像版本的不可变开关。否则如果您选择的镜像Tag在其他地方被修改,函数计算会尝试拉取最新的镜像数据来启动函数实例,可能造成线上多版本实例并存的情况。更多信息,请参见开启镜像版本不可变。
启动命令 容器的启动命令。如果不填写,则默认使用镜像中的Entrypoint或者CMD。 监听端口 容器镜像中的HTTP Server所监听的端口。默认端口为9000。如果不填写,默认使用非交互模式运行容器镜像。 - 高级配置:配置函数的实例相关信息、执行超时时间和请求处理程序。
配置项 说明 是否使用GPU 根据您的业务情况,选择是否使用GPU实例。默认使用弹性实例,不使用GPU实例。更多信息,请参见实例类型。关于各种实例类型的计费详情,请参见计费概述。 - 使用GPU
- 不使用GPU
规格方案 - 选择使用GPU
根据您的业务情况,选择合理的GPU规格。函数计算将根据您选择的GPU规格自动选定vCPU规格和内存规格,不支持灵活配比。
- 选择不使用GPU
根据您的业务情况,选择或手动输入合理的vCPU规格和内存规格组合。
说明- vCPU大小(单位为核)与内存大小(单位为GB)的比例必须设置在1∶1到1∶4之间。
- 存量迁移场景下,函数计算支持按照2∶3的比例抵扣vCPU和内存,即1 GB-秒=0.66 vCPU*秒+1 GB*秒。2022年11月03日之后,函数计算支持根据业务需求灵活配比。但vCPU的值必须以0.05核为阶梯递增,原折算比例中0.66 vCPU*秒的取值将不再适用,您可以选择0.60 vCPU*秒或0.65 vCPU*秒等。
硬盘大小 根据您的业务情况,选择硬盘大小。函数计算为您提供512 MB以内的磁盘免费使用额度。更多信息,请参见计费概述。 实例并发度 Web Server 模式选择否时,无需配置。设置函数实例的并发度。具体信息,请参见设置实例并发度。 执行超时时间 设置超时时间。默认为60秒,最长为86400秒,推荐您设置为600秒。 说明 超过设置的超时时间,函数将以执行失败结束。如需更长的超时时间限制,请加入钉钉用户群(钉钉群号11721331)申请。时区 选择函数的时区。此处设置函数的时区后,将自动为函数添加一条环境变量TZ,其值为您设置的目标时区。 - 环境变量:设置函数运行环境中的环境变量。更多信息,请参见环境变量。
- 触发器配置:设置函数的触发器,您可以使用触发器触发函数执行。更多信息,请参见触发器管理。
创建完成后,您可以在目标服务下的函数列表中查看和更新已创建的函数。 - 基本设置:配置函数的基本信息,包括函数名称、Web Server模式和请求处理程序类型。
说明 更新函数时,只能变更已设置的监听端口,不能删除或添加额外的监听端口。如果创建函数时,配置了监听端口,更新该函数时,不指定监听端口,将保留创建函数时的监听端口。
使用Serverless Devs创建
使用Serverless Devs可以一键构建、推送容器镜像并部署函数。
- 执行以下命令,初始化项目。
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
- 执行以下命令,进入项目目录。
cd start-fc-custom-container-event-nodejs14
- 编辑s.yaml文件。将该文件内的image参数信息修改为您的ACR镜像地址。
- 执行以下命令,部署项目。
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
- 执行以下命令,调试函数。
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