Jika runtime bawaan di Function Compute tidak memenuhi kebutuhan bisnis Anda, Anda dapat menggunakan fungsi Custom Container. Topik ini menjelaskan cara membuat fungsi Custom Container di Konsol Function Compute atau menggunakan Serverless Devs.
Perhatian
Untuk membuat fungsi Custom Container di Function Compute, Anda harus menggunakan gambar dari repositori Alibaba Cloud Container Registry yang berada di wilayah yang sama dan di bawah akun yang sama. Jika Anda membuat gambar pada komputer Mac dengan chip Apple atau perangkat lain berarsitektur ARM, pastikan untuk menentukan platform build gambar sebagai Linux/Amd64. Contohnya: docker build --platform linux/amd64 -t $IMAGE_NAME ..
Prasyarat
Container Registry
- Catatan
Anda dapat membuat instans Edisi Personal atau Edisi Perusahaan. Kami merekomendasikan penggunaan instans Edisi Perusahaan.
Container Registry (ACR) Edisi Personal ditujukan untuk pengembang individu. Tersedia sebagai uji coba gratis dengan kuota selama periode pratinjau publik. Layanan ini tidak menyediakan Service-Level Agreement (SLA) atau kompensasi atas kerugian, serta tunduk pada batasan penggunaan. Untuk informasi lebih lanjut mengenai batasan tersebut, lihat Catatan tentang pembuatan instans Edisi Personal.
Serverless Devs
Buat fungsi di konsol
Langkah 1: Buat layanan dan berikan izin
Buat layanan di Function Compute console.
Untuk informasi lebih lanjut, lihat Buat layanan.
Lampirkan kebijakan akses AliyunContainerRegistryReadOnlyAccess atau AliyunContainerRegistryFullAccess ke layanan target.
Untuk informasi lebih lanjut, lihat Berikan izin Function Compute untuk mengakses layanan Alibaba Cloud lainnya. Function Compute memerlukan izin dalam kebijakan yang ditentukan untuk mengambil akun sementara untuk instans default di Container Registry. Function Compute kemudian menggunakan akun sementara ini untuk mendorong gambar dari repositori gambar pribadi Anda.
Langkah 2: Buat fungsi
Masuk ke Konsol Function Compute. Di panel navigasi sebelah kiri, klik Services & Functions.
Di bilah navigasi atas, pilih wilayah. Pada halaman Services, klik layanan yang diinginkan.
- Pada halaman Functions, klik Create Function.
Pada halaman Create Function, pilih Use Container Image, konfigurasikan parameter berikut sesuai kebutuhan, lalu klik Create.
Basic Settings: Tentukan informasi dasar untuk fungsi, seperti nama fungsi dan tipe penanganan.
Configuration Item
Description
Function Name
Masukkan nama fungsi kustom. Jika Anda biarkan kosong, Function Compute akan membuatkan nama secara otomatis.
Handler Type
Pilih tipe fungsi. Ini menentukan apakah fungsi menangani permintaan event atau permintaan HTTP. Opsi yang tersedia:
Handle event requests: Fungsi dipicu oleh timer, panggilan API atau kit pengembangan perangkat lunak (SDK), atau pemicu dari layanan Alibaba Cloud lainnya.
Handle HTTP requests: Fungsi menangani permintaan HTTP atau WebSocket. Untuk skenario web, kami merekomendasikan agar Anda Use Custom Runtime.
Image Configuration: Pilih gambar yang digunakan untuk membuat fungsi.
Configuration Item
Description
Image Selection Method
Anda dapat Use Sample Image atau menggunakan gambar Anda sendiri untuk membuat fungsi.
Use Sample Image: Pilih gambar contoh yang disediakan oleh Function Compute.
Use an image from ACR: Klik Select Image from ACR di bawah parameter Container Image. Di panel Select Container Image yang muncul, pilih Container Instance dan ACR Image Repository. Lalu, temukan gambar target di daftar gambar dan klik Select di kolom Actions.
CatatanAnda tidak dapat menggunakan gambar publik dari repositori ACR milik akun lain.
Untuk Container Registry Edisi Perusahaan, Function Compute menggunakan tag gambar yang Anda pilih untuk membuat instans fungsi.
Jika Anda menggunakan gambar Edisi Perusahaan (Edisi Premium atau Edisi Standar), kami merekomendasikan agar Anda mengaktifkan pengaturan Immutable untuk image version. Jika tidak, ketika tag gambar diperbarui di tempat lain, Function Compute akan menarik data gambar terbaru untuk memulai instans. Untuk informasi lebih lanjut, lihat Enable immutable image versions.
Jika Anda menggunakan gambar Edisi Perusahaan (Edisi Dasar) atau ACR Edisi Personal, Anda harus memperbarui fungsi di Konsol Function Compute dengan informasi gambar baru setelah memperbarui tag gambar dengan nama yang sama di repositori gambar. Setelah itu, Function Compute akan menggunakan gambar baru untuk memulai instans.
Start Command
Perintah start untuk kontainer. Jika Anda biarkan kosong, Entrypoint atau CMD dari gambar akan digunakan secara default.
Listening Port
Port tempat server HTTP dalam gambar kontainer mendengarkan. Port default adalah 9000.
Advanced Configuration: Konfigurasikan pengaturan terkait instans dan periode timeout eksekusi untuk fungsi.
Configuration Item
Description
Use a GPU?
Berdasarkan kebutuhan bisnis Anda, pilih apakah akan menggunakan instans GPU-accelerated. Secara default, instans CPU digunakan dan instans GPU-accelerated tidak digunakan. Untuk informasi lebih lanjut, lihat Instance types. Untuk informasi penagihan berbagai tipe instans, lihat Billing overview.
Use GPU
Do not use GPU
Instance Specifications
Pilih Use GPU
Pilih GPU Card Type. Lalu, berdasarkan kebutuhan bisnis Anda, pilih spesifikasi GPU Memory yang sesuai. Function Compute secara otomatis memilih spesifikasi vCPU dan Memory berdasarkan pilihan GPU Anda. Rasio fleksibel tidak didukung.
Pilih Do not use GPU
Berdasarkan kebutuhan bisnis Anda, pilih atau masukkan kombinasi spesifikasi vCPU dan Memory yang sesuai.
Untuk informasi penagihan setiap resource, lihat Billing overview.
CatatanRasio vCPU (dalam core) terhadap memory (dalam GB) harus diatur pada rentang 1:1 hingga 1:4.
Ephemeral Disk Size
Berdasarkan kebutuhan bisnis Anda, pilih ukuran disk. Function Compute menyediakan kuota gratis hingga 512 MB untuk penggunaan disk. Untuk informasi lebih lanjut, lihat Billing overview.
Instance Concurrency
Tetapkan konkurensi instans fungsi. Untuk informasi lebih lanjut, lihat Set instance concurrency.
Execution Timeout
Tetapkan periode timeout. Nilai default adalah 60 detik. Nilai maksimum adalah 86400 detik. Kami merekomendasikan agar Anda menetapkan parameter ini menjadi 600 detik.
Time Zone
Pilih zona waktu untuk fungsi. Setelah Anda menetapkan zona waktu, variabel lingkungan bernama TZ akan ditambahkan secara otomatis ke fungsi. Nilai variabel ini adalah zona waktu yang Anda tetapkan.
Environment Variables: Tetapkan variabel lingkungan untuk lingkungan runtime fungsi. Untuk informasi lebih lanjut, lihat Environment variables.
Trigger Configurations: Konfigurasikan pemicu untuk mengeksekusi fungsi. Untuk informasi lebih lanjut, lihat Manage triggers.
Setelah fungsi dibuat, Anda dapat melihat dan memperbaruinya di daftar fungsi layanan target.
Saat memperbarui fungsi, Anda hanya dapat mengubah port listening yang dikonfigurasi. Anda tidak dapat menghapus port tersebut atau menambahkan port listening lain. Jika Anda mengonfigurasi port listening saat membuat fungsi, port tersebut akan tetap dipertahankan jika Anda tidak menentukan port listening selama pembaruan.
Gunakan Serverless Devs untuk membuat fungsi
Anda dapat menggunakan Serverless Devs untuk membangun gambar kontainer, mendorongnya ke repositori, dan menerapkan fungsi dalam satu langkah.
Jalankan perintah berikut untuk menginisialisasi proyek. Masukkan alamat repositori gambar ACR saat diminta.
sudo s init start-fc-custom-container-event-nodejs14Contoh output:
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.alibabacloud.com/ ACR: https://cr.console.alibabacloud.com/ * Note: The actions are declared in s.yaml: Pre-deployment execution: sudo s build --use-docker --dockerfile ./code/Dockerfile If you do not need to build the project every time, do not need to build it before deployment, or have already built it manually, you can comment out this part. > PS: You also need to modify the image field in s.yaml to the address of your ACR configuration during deployment. * The project is initialized. You can go to the project directory and use s deploy to deploy the project. 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? NoJalankan perintah berikut untuk masuk ke direktori proyek.
cd start-fc-custom-container-event-nodejs14Edit file
s.yaml. Untuk informasi lebih lanjut mengenai parameter dalam file YAML, lihat YAML specifications.Jalankan perintah berikut untuk menerapkan proyek.
sudo s deployContoh output:
[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: yourAccessKeyID [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeySecret: yourAccessKeySecret [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 Policy 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: 60Jalankan perintah berikut untuk menguji fungsi.
sudo s invoke -e "{\"key\":\"val\"}"Contoh output:
[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
Referensi
Jika Anda ingin menggunakan instans GPU-accelerated, Anda harus membuat fungsi Custom Container. Untuk informasi lebih lanjut mengenai cara memilih spesifikasi instans GPU, lihat Instance types and modes.
Fungsi Custom Container mungkin memiliki waktu cold start yang lebih lama karena lingkungan dasar gambar kontainer memerlukan waktu tambahan untuk mengunduh dan mendekompresi data. Untuk mengurangi waktu cold start, lihat Best practices for reducing cold starts.
Anda juga dapat memanggil API untuk membuat fungsi. Untuk informasi lebih lanjut, lihat CreateFunction.
Untuk informasi lebih lanjut mengenai skenario penggunaan dan perbedaan antara runtime bawaan, custom, dan custom container yang disediakan Function Compute, lihat Select a function runtime.