當Function Compute平台的內建運行時無法滿足您的業務需求時,可以選擇自訂容器函數。本文介紹如何在Function Compute控制台或使用Serverless Devs建立Custom Container函數。
注意事項
在Function Compute中,建立Custom Container函數必須使用同一帳號下相同地區內阿里雲鏡像服務倉庫中的鏡像。針對搭載Apple晶片的Mac電腦(或其他ARM架構的機器),構建鏡像時需要指定鏡像的編譯平台為Linux/Amd64,範例程式碼如docker build --platform linux/amd64 -t $IMAGE_NAME .
。
前提條件
Container Registry
- 說明
支援建立個人版或企業版執行個體,推薦您使用企業版容器鏡像執行個體建立函數。
容器鏡像個人版ACR面向個人開發人員,公測限額免費試用,無SLA承諾和受損賠償,且有使用限制。關於使用限制說明,請參見建立個人版執行個體注意事項。
Serverless Devs
在控制台建立函數
步驟一:建立服務並為服務設定許可權
在Function Compute控制台建立服務。
具體操作,請參見建立服務。
為目標服務綁定權限原則AliyunContainerRegistryReadOnlyAccess或者AliyunContainerRegistryFullAccess。
具體操作,請參見授予Function Compute訪問其他雲端服務的許可權。Function Compute需要使用上述策略中的許可權擷取Container Registry中預設執行個體的臨時帳號,然後利用該臨時帳號推送位於您的私人鏡像倉庫中的鏡像。
步驟二:建立函數
- 登入Function Compute控制台,在左側導覽列,單擊服務及函數。
- 在頂部功能表列,選擇地區,然後在服務列表頁面,單擊目標服務。
- 在函數管理頁面,單擊建立函數。
在建立函數頁面,選擇使用容器鏡像建立,按需配置以下配置項,然後單擊建立。
基本設定:配置函數的基本資料,包括函數名稱和請求處理常式類型。
配置項
說明
函數名稱
填寫自訂的函數名稱。如不填寫,Function Compute會自動為您建立。
請求處理常式類型
選擇函數的類型,即處理事件請求的函數還是HTTP函數。取值說明如下:
處理事件請求:通過定時器、調用API/SDK或其他阿里雲服務的觸發器來觸發函數執行。
處理 HTTP 要求:用於處理HTTP請求或WebSocket請求的函數。如果您的使用情境是Web情境,建議使用自訂運行時建立。
鏡像配置:配置建立函數的鏡像。
配置項
說明
鏡像選擇方式
您可以使用樣本鏡像或者您自己的鏡像建立函數。
使用樣本鏡像:選擇Function Compute內建的樣本鏡像。
使用 ACR 中的鏡像:單擊配置項容器鏡像下方的選擇 ACR 中的鏡像,在彈出的選擇容器鏡像面板,選擇已建立的容器鏡像執行個體和ACR 鏡像倉庫,然後在下方選擇鏡像地區找到目標鏡像並在其右側操作列單擊選擇。
說明不支援使用跨賬戶ACR中的公開鏡像建立函數。
針對企業版Container Registry,Function Compute會使用您在此處選擇的鏡像的Tag建立函數執行個體。
如果您使用的是企業版鏡像(進階版和標準版),建議您開啟鏡像版本的不可變開關。否則若該鏡像Tag在其他地方被更新,Function Compute會拉取最新的鏡像資料來啟動執行個體。更多資訊,請參見開啟鏡像版本不可變。
如果您使用的是企業版鏡像(基礎版)或ACR個人版,您在該鏡像倉庫更新同名鏡像Tag後,需要在Function Compute控制台以該鏡像資訊重新更新函數後,Function Compute才會使用此新鏡像啟動執行個體。
啟動命令
容器的啟動命令。如果不填寫,則預設使用鏡像中的Entrypoint或者CMD。
監聽連接埠
容器鏡像中的HTTP Server所監聽的連接埠。預設連接埠為9000。
進階配置:配置函數的執行個體相關資訊和執行逾時時間。
配置項
說明
是否使用GPU
根據您的業務情況,選擇是否使用GPU執行個體。預設使用CPU執行個體,不使用GPU執行個體。更多資訊,請參見執行個體類型。關於各種執行個體類型的計費詳情,請參見計費概述。
使用GPU
不使用GPU
規格方案
選擇使用GPU
選擇GPU 卡型,然後根據您的業務情況,選擇合理的GPU 顯存規格。Function Compute將根據您選擇的GPU規格自動選定vCPU 規格和記憶體規格,不支援靈活配比。
選擇不使用GPU
根據您的業務情況,選擇或手動輸入合理的vCPU 規格和記憶體規格組合。
關於各資源使用的計費詳情,請參見計費概述。
說明vCPU大小(單位為核)與記憶體大小(單位為GB)的比例必須設定在1∶1到1∶4之間。
臨時硬碟大小
根據您的業務情況,選擇硬碟大小。Function Compute為您提供512 MB以內的磁碟免費使用額度。更多資訊,請參見計費概述。
執行個體並發度
設定函數執行個體的並發度。具體資訊,請參見設定執行個體並發度。
執行逾時時間
設定逾時時間。預設為60秒,最長為86400秒,推薦您設定為600秒。
時區
選擇函數的時區。此處設定函數的時區後,將自動為函數添加一條環境變數TZ,其值為您設定的目標時區。
環境變數:設定函數運行環境中的環境變數。更多資訊,請參見環境變數。
觸發器配置:設定函數的觸發器,您可以使用觸發器觸發函數執行。更多資訊,請參見觸發器管理。
建立完成後,您可以在目標服務下的函數列表中查看和更新已建立的函數。
更新函數時,只能變更已設定的監聽連接埠,不能刪除或添加額外的監聽連接埠。如果建立函數時,配置了監聽連接埠,更新該函數時,不指定監聽連接埠,將保留建立函數時的監聽連接埠。
使用Serverless Devs建立
使用Serverless Devs可以一鍵構建、推送容器鏡像並部署函數。
執行以下命令,初始化專案。需根據提示輸入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
執行以下命令,進入專案目錄。
cd start-fc-custom-container-event-nodejs14
編輯
s.yaml
檔案。關於YAML檔案的參數詳解,請參見YAML規範。執行以下命令,部署專案。
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
相關文檔
如果您要使用GPU執行個體,則只能建立Custom Container函數。關於GPU執行個體規格的選擇,請參見執行個體類型及使用模式。
使用Custom Container函數,容器鏡像依賴的基礎環境會帶來額外的資料下載和解壓的時間,為了降低冷啟動時間,請參見冷啟動最佳化最佳實務。
您也可以調用API來建立函數,更多資訊,請參見CreateFunction - 建立函數。
關於Function Compute提供的內建運行時、自訂運行時和自訂容器運行時適用情境及差異,請參見函數運行時選型。