本文介绍如何在函数计算控制台或使用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"
- 执行以下命令打包镜像。
docker build -t $IMAGE_NAME .
说明 针对搭载Apple芯片的Mac电脑(或其他ARM架构的机器),构建镜像时需要指定镜像的编译平台为Linux/Amd64。实现跨平台编译,示例代码如docker build --platform linux/amd64 -t $IMAGE_NAME .
。 - 执行以下命令推送镜像。
docker push $IMAGE_NAME
步骤二:创建服务并为服务设置权限
- 在函数计算控制台创建服务。具体操作步骤,请参见创建服务。
- 为目标服务绑定权限策略AliyunContainerRegistryReadOnlyAccess或者AliyunContainerRegistryFullAccess。具体操作步骤,请参见授予函数计算访问其他云服务的权限。函数计算需要使用上述策略中的权限去获取容器镜像服务中默认实例的临时账号,然后利用该临时账号推送位于您的私有镜像仓库中的镜像。
步骤三:创建函数
- 登录函数计算控制台。
- 在左侧导航栏,单击服务及函数。
- 在顶部菜单栏,选择地域。
- 在服务列表页面,单击目标服务。
- 在函数管理页面,单击创建函数。
- 在创建函数页面,选择使用容器镜像(面向Kubernetes、Docker等用户),按需配置以下参数,然后单击创建。
- 基本设置:配置函数的基本信息,包括函数名称和请求处理程序类型。
- 处理事件请求:通过定时器、调用API/SDK或其他阿里云服务的触发器来触发函数执行。
- 处理 HTTP 请求:用于处理HTTP请求或Websocekt请求的函数。如果您的使用场景是Web场景,建议您使用自定义运行时平滑迁移Web Server。
- 镜像配置:配置创建函数的镜像。
参数 说明 镜像选择方式 您可以使用示例镜像或者您自己的镜像创建函数。 - 使用示例镜像:选择函数计算自带的示例镜像。
- 使用 ACR 中的镜像:单击参数容器镜像下方的选择 ACR 中的镜像,在弹出的选择容器镜像面板,选择已创建的容器镜像实例和ACR 镜像仓库,然后在下方选择镜像区域找到目标镜像并在其右侧操作列单击选择。
启动命令 容器的启动命令。如果不填写,则默认使用镜像中的Entrypoint或者CMD。 监听端口 容器镜像中的HTTP Server所监听的端口。默认端口为9000。 - 高级配置:配置函数的实例相关信息、执行超时时间和请求处理程序。
参数 说明 实例类型 选择适合您业务的实例类型。 - 弹性实例
- 性能实例
内存规格 设置函数执行内存。 - 选择输入:在下拉列表中选择所需内存。
- 手动输入:仅适用于弹性实例,单击手动输入内存大小,可自定义函数执行内存。取值范围[128, 3072],单位为MB。
说明 输入的内存必须为64 MB的倍数。
实例并发度 设置函数实例的并发度,具体信息,请参见设置实例并发度。 执行超时时间 设置超时时间。默认为60秒,最长为86400秒。 说明 超过设置的超时时间,函数将以执行失败结束。如需更长的超时时间限制,请提交工单申请。 - 环境变量:设置函数运行环境中的环境变量。更多信息,请参见环境变量。
- 触发器配置:设置函数的触发器,您可以使用触发器触发函数执行。更多信息,请参见触发器管理。
创建完成后,您可以在目标服务下的函数列表中看到刚才创建的函数。 - 基本设置:配置函数的基本信息,包括函数名称和请求处理程序类型。
使用Serverless Devs创建
使用Serverless Devs可以一键构建、推送容器镜像并部署函数。
- 执行以下命令,初始化项目。
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: 部署前执行: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镜像地址。 - 执行以下命令,部署项目。
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
- 执行以下命令,调试函数。
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