1. บทนำ
ภาพรวม
ฟังก์ชัน Cloud Run คือข้อเสนอ Functions-as-a-Service ของ Google Cloud ที่ขับเคลื่อนโดย Cloud Run และ Eventarc ซึ่งช่วยให้คุณควบคุมประสิทธิภาพและความสามารถในการปรับขนาดได้ขั้นสูงยิ่งขึ้น รวมถึงควบคุมรันไทม์ของฟังก์ชันและทริกเกอร์จากแหล่งที่มาของเหตุการณ์กว่า 90 รายการได้มากขึ้น
Codelab นี้จะแนะนำวิธีสร้างฟังก์ชัน Cloud Run ที่ตอบสนองต่อการเรียกใช้ HTTP และทริกเกอร์โดยข้อความ Pub/Sub และบันทึกการตรวจสอบ Cloud
นอกจากนี้ Codelab นี้ยังใช้การอัปเดตรูปภาพฐานอัตโนมัติสำหรับการติดตั้งใช้งานฟังก์ชันโดยการระบุรูปภาพฐานโดยใช้แฟล็ก --base-image
การอัปเดตอิมเมจพื้นฐานอัตโนมัติสำหรับ Cloud Run ช่วยให้ Google สามารถสร้างแพตช์ความปลอดภัยให้กับระบบปฏิบัติการและคอมโพเนนต์รันไทม์ของภาษาในอิมเมจพื้นฐานได้โดยอัตโนมัติ คุณไม่ต้องสร้างใหม่หรือติดตั้งใช้งานบริการอีกครั้งเพื่อให้ระบบอัปเดตอิมเมจพื้นฐาน ดูข้อมูลเพิ่มเติมได้ที่การอัปเดตรูปภาพฐานอัตโนมัติ
หากไม่ต้องการใช้การอัปเดตรูปภาพฐานอัตโนมัติ คุณสามารถนำแฟล็ก --base-image
ออกจากตัวอย่างที่แสดงใน Codelab นี้ได้
สิ่งที่คุณจะได้เรียนรู้
- ภาพรวมของฟังก์ชัน Cloud Run และวิธีใช้การอัปเดตอิมเมจพื้นฐานอัตโนมัติ
- วิธีเขียนฟังก์ชันที่ตอบสนองต่อการเรียกใช้ HTTP
- วิธีเขียนฟังก์ชันที่ตอบกลับข้อความ Pub/Sub
- วิธีเขียนฟังก์ชันที่ตอบสนองต่อเหตุการณ์ใน Cloud Storage
- วิธีแยกการรับส่งข้อมูลระหว่าง 2 รีวิชัน
- วิธีกำจัด Cold Start ด้วยอินสแตนซ์ขั้นต่ำ
2. การตั้งค่าและข้อกำหนด
สร้างโฟลเดอร์รูท
สร้างโฟลเดอร์รูทสำหรับตัวอย่างทั้งหมด
mkdir crf-codelab cd crf-codelab
ตั้งค่าตัวแปรสภาพแวดล้อม
ตั้งค่าตัวแปรสภาพแวดล้อมที่จะใช้ตลอด Codelab นี้
gcloud config set project <YOUR-PROJECT-ID> REGION=<YOUR_REGION> PROJECT_ID=$(gcloud config get-value project)
เปิดใช้ API
เปิดใช้บริการที่จำเป็นทั้งหมด
gcloud services enable \ artifactregistry.googleapis.com \ cloudbuild.googleapis.com \ eventarc.googleapis.com \ run.googleapis.com \ logging.googleapis.com \ pubsub.googleapis.com
3. ฟังก์ชัน HTTP
สำหรับฟังก์ชันแรก ให้สร้างฟังก์ชัน Node.js ที่ตรวจสอบสิทธิ์แล้วซึ่งตอบสนองต่อคำขอ HTTP นอกจากนี้ เราจะใช้การหมดเวลา 10 นาทีเพื่อแสดงให้เห็นว่าฟังก์ชันมีเวลาตอบสนองต่อคำขอ HTTP มากขึ้นได้อย่างไร
สร้าง
สร้างโฟลเดอร์สำหรับแอปแล้วไปที่โฟลเดอร์นั้น
mkdir hello-http cd hello-http
สร้างindex.js
ไฟล์ที่ตอบสนองต่อคำขอ HTTP ดังนี้
const functions = require('@google-cloud/functions-framework'); functions.http('helloWorld', (req, res) => { res.status(200).send('HTTP with Node.js in Cloud Run functions!'); });
สร้างไฟล์ package.json
เพื่อระบุทรัพยากร Dependency
{ "name": "nodejs-run-functions-codelab", "version": "0.0.1", "main": "index.js", "dependencies": { "@google-cloud/functions-framework": "^2.0.0" } }
ติดตั้งใช้งาน
ทำให้ฟังก์ชันใช้งานได้
gcloud run deploy nodejs-run-function \ --source . \ --function helloWorld \ --base-image nodejs22 \ --region $REGION \ --timeout 600 \ --no-allow-unauthenticated
คำสั่งนี้ใช้ Buildpack เพื่อเปลี่ยนซอร์สโค้ดของฟังก์ชันเป็นอิมเมจคอนเทนเนอร์ที่พร้อมใช้งานจริง
โปรดทราบว่า
- ใช้แฟล็ก
--source
เพื่อบอกให้ Cloud Run สร้างฟังก์ชันเป็นบริการที่ใช้คอนเทนเนอร์ที่เรียกใช้ได้ - โดยใช้แฟล็ก
--function
(ใหม่) เพื่อตั้งค่าจุดแรกเข้าของบริการใหม่ให้เป็นลายเซ็นฟังก์ชันที่คุณต้องการเรียกใช้ - แฟล็ก
--base-image
(ใหม่) ระบุสภาพแวดล้อมของอิมเมจฐานสำหรับฟังก์ชัน เช่นnodejs22
,python312
,go123
,java21
,dotnet8
,ruby33
หรือphp83
ดูรายละเอียดเพิ่มเติมเกี่ยวกับอิมเมจพื้นฐานและแพ็กเกจที่รวมอยู่ในแต่ละอิมเมจได้ที่อิมเมจพื้นฐานของรันไทม์ - (ไม่บังคับ) แฟล็ก
--timeout
ช่วยให้ฟังก์ชันมีระยะหมดเวลาที่นานขึ้นเพื่อตอบสนองต่อคำขอ HTTP ในตัวอย่างนี้ เราใช้ 600 วินาทีเพื่อแสดงเวลาตอบกลับ 10 นาที - (ไม่บังคับ)
--no-allow-unauthenticated
เพื่อป้องกันไม่ให้เรียกใช้ฟังก์ชันของคุณแบบสาธารณะ
ทดสอบ
ทดสอบฟังก์ชันด้วยคำสั่งต่อไปนี้
# get the Service URL SERVICE_URL="$(gcloud run services describe nodejs-run-function --region $REGION --format 'value(status.url)')" # invoke the service curl -H "Authorization: bearer $(gcloud auth print-identity-token)" -X GET $SERVICE_URL
คุณควรเห็นข้อความ HTTP with Node.js in Cloud Run functions!
เป็นคำตอบ
4. ฟังก์ชัน Pub/Sub
สำหรับฟังก์ชันที่ 2 ให้สร้างฟังก์ชัน Python ที่ทริกเกอร์โดยข้อความ Pub/Sub ที่เผยแพร่ไปยังหัวข้อที่เฉพาะเจาะจง
ตั้งค่าโทเค็นการตรวจสอบสิทธิ์ Pub/Sub
หากเปิดใช้บัญชีบริการ Pub/Sub ในวันที่ 8 เมษายน 2021 หรือก่อนหน้านั้น ให้มอบบทบาท iam.serviceAccountTokenCreator
ให้กับบัญชีบริการ Pub/Sub โดยทำดังนี้
PROJECT_NUMBER=$(gcloud projects list --filter="project_id:$PROJECT_ID" --format='value(project_number)') gcloud projects add-iam-policy-binding $PROJECT_ID \ --member serviceAccount:service-$PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com \ --role roles/iam.serviceAccountTokenCreator
สร้าง
สร้างหัวข้อ Pub/Sub เพื่อใช้กับตัวอย่างโดยทำดังนี้
TOPIC=cloud-run-functions-pubsub-topic gcloud pubsub topics create $TOPIC
สร้างโฟลเดอร์สำหรับแอปแล้วไปที่โฟลเดอร์นั้น
mkdir ../hello-pubsub cd ../hello-pubsub
สร้างไฟล์ main.py
ที่บันทึกข้อความซึ่งมีรหัส CloudEvent
import functions_framework @functions_framework.cloud_event def hello_pubsub(cloud_event): print('Pub/Sub with Python in Cloud Run functions! Id: ' + cloud_event['id'])
สร้างไฟล์ requirements.txt
ที่มีเนื้อหาต่อไปนี้เพื่อระบุทรัพยากร Dependency
functions-framework==3.*
ติดตั้งใช้งาน
ทำให้ฟังก์ชันใช้งานได้
gcloud run deploy python-pubsub-function \ --source . \ --function hello_pubsub \ --base-image python313 \ --region $REGION \ --no-allow-unauthenticated
เรียกหมายเลขโปรเจ็กต์ที่จะใช้สำหรับข้อมูลประจำตัวของบัญชีบริการ
PROJECT_NUMBER=$(gcloud projects list --filter="project_id:$PROJECT_ID" --format='value(project_number)')
สร้างทริกเกอร์
gcloud eventarc triggers create python-pubsub-function-trigger \ --location=$REGION \ --destination-run-service=python-pubsub-function \ --destination-run-region=$REGION \ --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \ --transport-topic=projects/$PROJECT_ID/topics/$TOPIC \ --service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com
ทดสอบ
ทดสอบฟังก์ชันโดยส่งข้อความไปยังหัวข้อ
gcloud pubsub topics publish $TOPIC --message="Hello World"
คุณควรเห็น CloudEvent ที่ได้รับในบันทึก
gcloud run services logs read python-pubsub-function --region $REGION --limit=10
5. ฟังก์ชัน Cloud Storage
สำหรับฟังก์ชันถัดไป เรามาสร้างฟังก์ชัน Node.js ที่ตอบสนองต่อเหตุการณ์จากที่เก็บข้อมูล Cloud Storage กัน
ตั้งค่า
หากต้องการใช้ฟังก์ชัน Cloud Storage ให้มอบpubsub.publisher
บทบาท IAM ให้กับบัญชีบริการ Cloud Storage โดยทำดังนี้
SERVICE_ACCOUNT=$(gsutil kms serviceaccount -p $PROJECT_NUMBER) gcloud projects add-iam-policy-binding $PROJECT_ID \ --member serviceAccount:$SERVICE_ACCOUNT \ --role roles/pubsub.publisher
สร้าง
สร้างโฟลเดอร์สำหรับแอปแล้วไปที่โฟลเดอร์นั้น
mkdir ../hello-storage cd ../hello-storage
สร้างไฟล์ index.js
ที่ตอบสนองต่อเหตุการณ์ Cloud Storage อย่างง่ายๆ ดังนี้
const functions = require('@google-cloud/functions-framework'); functions.cloudEvent('helloStorage', (cloudevent) => { console.log('Cloud Storage event with Node.js in Cloud Run functions!'); console.log(cloudevent); });
สร้างไฟล์ package.json
เพื่อระบุทรัพยากร Dependency
{ "name": "nodejs-crf-cloud-storage", "version": "0.0.1", "main": "index.js", "dependencies": { "@google-cloud/functions-framework": "^2.0.0" } }
ติดตั้งใช้งาน
ก่อนอื่น ให้สร้างที่เก็บข้อมูล Cloud Storage (หรือใช้ที่เก็บข้อมูลที่มีอยู่แล้ว) โดยทำดังนี้
export BUCKET_NAME="gcf-storage-$PROJECT_ID" export BUCKET="gs://gcf-storage-$PROJECT_ID" gsutil mb -l $REGION $BUCKET
ทำให้ฟังก์ชันใช้งานได้
gcloud run deploy nodejs-crf-cloud-storage \ --source . \ --base-image nodejs22 \ --function helloStorage \ --region $REGION \ --no-allow-unauthenticated
เมื่อทำให้ฟังก์ชันใช้งานได้แล้ว คุณจะเห็นฟังก์ชันดังกล่าวในส่วน Cloud Run ของ Cloud Console
ตอนนี้ให้สร้างทริกเกอร์ Eventarc
BUCKET_REGION=$REGION gcloud eventarc triggers create nodejs-crf-cloud-storage-trigger \ --location=$BUCKET_REGION \ --destination-run-service=nodejs-crf-cloud-storage \ --destination-run-region=$REGION \ --event-filters="type=google.cloud.storage.object.v1.finalized" \ --event-filters="bucket=$BUCKET_NAME" \ --service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com
ทดสอบ
ทดสอบฟังก์ชันโดยการอัปโหลดไฟล์ไปยังที่เก็บข้อมูล
echo "Hello World" > random.txt gsutil cp random.txt $BUCKET/random.txt
คุณควรเห็น CloudEvent ที่ได้รับในบันทึก
gcloud run services logs read nodejs-crf-cloud-storage --region $REGION --limit=10
6. บันทึกการตรวจสอบ Cloud
สำหรับฟังก์ชันถัดไป ให้สร้างฟังก์ชัน Node.js ที่รับเหตุการณ์ Cloud Audit Log เมื่อมีการสร้างอินสแตนซ์ VM ของ Compute Engine ในการตอบสนอง ระบบจะเพิ่มป้ายกำกับลงใน VM ที่สร้างขึ้นใหม่ โดยระบุผู้สร้าง VM
ระบุ VM ของ Compute Engine ที่สร้างขึ้นใหม่
Compute Engine จะปล่อยบันทึกการตรวจสอบ 2 รายการเมื่อมีการสร้าง VM
โดยจะมีการปล่อยรายการแรกเมื่อเริ่มต้นการสร้าง VM ส่วนรายการที่ 2 จะแสดงหลังจากสร้าง VM แล้ว
ในบันทึกการตรวจสอบ ฟิลด์การดำเนินการจะแตกต่างกัน โดยมีค่า first: true
และ last: true
บันทึกการตรวจสอบที่ 2 มีข้อมูลทั้งหมดที่เราต้องการเพื่อติดป้ายกำกับอินสแตนซ์ ดังนั้นเราจะใช้แฟล็ก last: true
เพื่อตรวจหาในฟังก์ชัน Cloud Run
ตั้งค่า
หากต้องการใช้ฟังก์ชันบันทึกการตรวจสอบของ Cloud คุณต้องเปิดใช้บันทึกการตรวจสอบสำหรับ Eventarc นอกจากนี้ คุณยังต้องใช้บัญชีบริการที่มีบทบาท eventarc.eventReceiver
- เปิดใช้บันทึกการตรวจสอบระบบคลาวด์ ประเภทบันทึก Admin Read, Data Read และ Data Write สำหรับ Compute Engine API
- มอบ
eventarc.eventReceiver
บทบาท IAM ให้กับบัญชีบริการ Compute Engine เริ่มต้น
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role roles/eventarc.eventReceiver
สร้างฟังก์ชัน
Codelab นี้ใช้ node.js แต่คุณดูตัวอย่างอื่นๆ ได้ที่ https://github.com/GoogleCloudPlatform/eventarc-samples
สร้างไฟล์ package.json
{
"dependencies": {
"googleapis": "^84.0.0"
}
}
สร้างไฟล์ node.js
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const { google } = require("googleapis");
var compute = google.compute("v1");
exports.labelVmCreation = async (cloudevent) => {
const data = cloudevent.body;
// in case an event has >1 audit log
// make sure we respond to the last event
if (!data.operation || !data.operation.last) {
console.log("Operation is not last, skipping event");
return;
}
// projects/dogfood-gcf-saraford/zones/us-central1-a/instances/instance-1
var resourceName = data.protoPayload.resourceName;
var resourceParts = resourceName.split("/");
var project = resourceParts[1];
var zone = resourceParts[3];
var instanceName = resourceParts[5];
var username = data.protoPayload.authenticationInfo.principalEmail.split("@")[0];
console.log(`Setting label username: ${username} to instance ${instanceName} for zone ${zone}`);
var authClient = await google.auth.getClient({
scopes: ["https://www.googleapis.com/auth/cloud-platform"]
});
// per docs: When updating or adding labels in the API,
// you need to provide the latest labels fingerprint with your request,
// to prevent any conflicts with other requests.
var labelFingerprint = await getInstanceLabelFingerprint(authClient, project, zone, instanceName);
var responseStatus = await setVmLabel(
authClient,
labelFingerprint,
username,
project,
zone,
instanceName
);
// log results of setting VM label
console.log(JSON.stringify(responseStatus, null, 2));
};
async function getInstanceLabelFingerprint(authClient, project, zone, instanceName) {
var request = {
project: project,
zone: zone,
instance: instanceName,
auth: authClient
};
var response = await compute.instances.get(request);
var labelFingerprint = response.data.labelFingerprint;
return labelFingerprint;
}
async function setVmLabel(authClient, labelFingerprint, username, project, zone, instanceName) {
var request = {
project: project,
zone: zone,
instance: instanceName,
resource: {
labels: { "creator": username },
labelFingerprint: labelFingerprint
},
auth: authClient
};
var response = await compute.instances.setLabels(request);
return response.statusText;
}
ติดตั้งใช้งาน
ทำให้ฟังก์ชันใช้งานได้
gcloud run deploy gce-vm-labeler \ --source . \ --function labelVmCreation \ --region $REGION \ --no-allow-unauthenticated
ตอนนี้ให้สร้างทริกเกอร์ โปรดสังเกตว่าฟังก์ชันนี้กรองบันทึกการตรวจสอบสำหรับการแทรก Compute Engine ด้วยแฟล็ก --trigger-event-filters
gcloud eventarc triggers create gce-vm-labeler-trigger \ --location=$REGION \ --destination-run-service=gce-vm-labeler \ --destination-run-region=$REGION \ --event-filters="type=google.cloud.audit.log.v1.written,serviceName=compute.googleapis.com,methodName=v1.compute.instances.insert" \ --service-account=$ROJECT_NUMBER-compute@developer.gserviceaccount.com
ทดสอบ
ตั้งค่าตัวแปรสภาพแวดล้อม
# if you're using europe-west1 as your region
ZONE=europe-west1-d
VM_NAME=codelab-crf-auditlog
เรียกใช้คำสั่งต่อไปนี้เพื่อสร้าง VM
gcloud compute instances create $VM_NAME --zone=$ZONE --machine-type=e2-medium --image-family=debian-11 --image-project=debian-cloud
เมื่อสร้าง VM เสร็จแล้ว คุณควรเห็นป้ายกำกับ creator
ที่เพิ่มใน VM ใน Cloud Console ในส่วนข้อมูลพื้นฐาน หรือใช้คำสั่งต่อไปนี้
gcloud compute instances describe $VM_NAME --zone=$ZONE
คุณควรเห็นป้ายกำกับในเอาต์พุตเหมือนตัวอย่างต่อไปนี้
... labelFingerprint: ULU6pAy2C7s= labels: creator: atameldev ...
ล้างข้อมูล
อย่าลืมลบอินสแตนซ์ VM และจะไม่นำมาใช้ในห้องทดลองนี้อีก
gcloud compute instances delete $VM_NAME --zone=$ZONE
7. การแยกการเข้าชม
ฟังก์ชัน Cloud Run รองรับฟังก์ชันหลายเวอร์ชัน โดยจะแยกการรับส่งข้อมูลระหว่างเวอร์ชันต่างๆ และย้อนฟังก์ชันกลับไปเป็นเวอร์ชันก่อนหน้า
ในขั้นตอนนี้ คุณจะติดตั้งใช้งานฟังก์ชัน 2 รีวิชัน แล้วแยกการเข้าชมระหว่างรีวิชันทั้ง 2 เป็น 50-50
สร้าง
สร้างโฟลเดอร์สำหรับแอปแล้วไปที่โฟลเดอร์นั้น
mkdir ../traffic-splitting cd ../traffic-splitting
สร้างไฟล์ main.py
ด้วยฟังก์ชัน Python ที่อ่านตัวแปรสภาพแวดล้อมสีและตอบกลับด้วย Hello World
ในสีพื้นหลังนั้น
import os color = os.environ.get('COLOR') def hello_world(request): return f'<body style="background-color:{color}"><h1>Hello World!</h1></body>'
สร้างไฟล์ requirements.txt
ที่มีเนื้อหาต่อไปนี้เพื่อระบุทรัพยากร Dependency
functions-framework==3.*
ติดตั้งใช้งาน
ติดตั้งใช้งานฟังก์ชันเวอร์ชันแรกที่มีพื้นหลังสีส้มโดยทำดังนี้
COLOR=orange gcloud run deploy hello-world-colors \ --source . \ --base-image python313 \ --function hello_world \ --region $REGION \ --allow-unauthenticated \ --update-env-vars COLOR=$COLOR
ในตอนนี้ หากคุณทดสอบฟังก์ชันโดยดูทริกเกอร์ HTTP (เอาต์พุต URI ของคำสั่งการติดตั้งใช้งานข้างต้น) ในเบราว์เซอร์ คุณควรเห็น Hello World
ที่มีพื้นหลังสีส้ม
โดยปรับใช้การแก้ไขครั้งที่ 2 ที่มีพื้นหลังสีเหลือง
COLOR=yellow gcloud run deploy hello-world-colors \ --source . \ --base-image python313 \ --function hello_world \ --region $REGION \ --allow-unauthenticated \ --update-env-vars COLOR=$COLOR
เนื่องจากนี่คือการแก้ไขล่าสุด หากคุณทดสอบฟังก์ชันนี้ คุณควรเห็น Hello World
ที่มีพื้นหลังสีเหลือง
แยกการเข้าชม 50-50
หากต้องการแยกการเข้าชมระหว่างรีวิชันสีส้มและสีเหลือง คุณต้องหารหัสรีวิชันของบริการ Cloud Run นี่คือคำสั่งสำหรับดูรหัสการแก้ไข
gcloud run revisions list --service hello-world-colors \ --region $REGION --format 'value(REVISION)'
เอาต์พุตควรมีลักษณะคล้ายกับตัวอย่างต่อไปนี้
hello-world-colors-00001-man hello-world-colors-00002-wok
ตอนนี้ ให้แบ่งการเข้าชมระหว่างการแก้ไข 2 รายการนี้ดังนี้ (อัปเดต X-XXX
ตามชื่อการแก้ไข)
gcloud run services update-traffic hello-world-colors \ --region $REGION \ --to-revisions hello-world-colors-0000X-XXX=50,hello-world-colors-0000X-XXX=50
ทดสอบ
ทดสอบฟังก์ชันโดยไปที่ URL สาธารณะของฟังก์ชัน ครึ่งหนึ่งของเวลาที่คุณดู คุณจะเห็นการแก้ไขสีส้ม และอีกครึ่งหนึ่งจะเห็นการแก้ไขสีเหลือง
ดูข้อมูลเพิ่มเติมได้ที่การย้อนกลับ การเปิดตัวแบบค่อยเป็นค่อยไป และการย้ายข้อมูลการเข้าชม
8. อินสแตนซ์ขั้นต่ำ
ในฟังก์ชัน Cloud Run คุณสามารถระบุจำนวนอินสแตนซ์ฟังก์ชันขั้นต่ำที่จะคงสถานะพร้อมใช้งานและพร้อมให้บริการคำขอ ซึ่งจะเป็นประโยชน์ในการจำกัดจำนวน Cold Start
ในขั้นตอนนี้ คุณจะติดตั้งใช้งานฟังก์ชันที่มีการเริ่มต้นที่ช้า คุณจะเห็นปัญหา Cold Start จากนั้นคุณจะติดตั้งใช้งานฟังก์ชันโดยตั้งค่าอินสแตนซ์ขั้นต่ำเป็น 1 เพื่อกำจัด Cold Start
สร้าง
สร้างโฟลเดอร์สำหรับแอปแล้วไปที่โฟลเดอร์นั้นโดยทำดังนี้
mkdir ../min-instances cd ../min-instances
สร้างไฟล์ main.go
บริการ Go นี้มีinit
ฟังก์ชันที่หยุดทำงานเป็นเวลา 10 วินาทีเพื่อจำลองการเริ่มต้นที่ใช้เวลานาน นอกจากนี้ ยังมีฟังก์ชัน HelloWorld
ที่ตอบสนองต่อการเรียกใช้ HTTP ด้วย
package p import ( "fmt" "net/http" "time" ) func init() { time.Sleep(10 * time.Second) } func HelloWorld(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Slow HTTP Go in Cloud Run functions!") }
ติดตั้งใช้งาน
ทําการแก้ไขฟังก์ชันครั้งแรกโดยมีค่าอินสแตนซ์ขั้นต่ำเริ่มต้นเป็น 0 ดังนี้
gcloud run deploy go-slow-function \ --source . \ --base-image go123 \ --function HelloWorld \ --region $REGION \ --no-allow-unauthenticated
ทดสอบฟังก์ชันด้วยคำสั่งนี้
# get the Service URL SERVICE_URL="$(gcloud run services describe go-slow-function --region $REGION --format 'value(status.url)')" # invoke the service curl -H "Authorization: bearer $(gcloud auth print-identity-token)" -X GET $SERVICE_URL
คุณจะเห็นว่าการโทรครั้งแรกจะล่าช้า 10 วินาที (การเริ่มต้นแบบเย็น) จากนั้นจึงจะเห็นข้อความ การเรียกใช้ครั้งต่อๆ ไปควรแสดงผลทันที
ตั้งค่าอินสแตนซ์ขั้นต่ำ
หากต้องการหลีกเลี่ยงการเริ่มแบบเย็นในคำขอแรก ให้ติดตั้งฟังก์ชันใหม่โดยตั้งค่า Flag --min-instances
เป็น 1 ดังนี้
gcloud run deploy go-slow-function \ --source . \ --base-image go123 \ --function HelloWorld \ --region $REGION \ --no-allow-unauthenticated \ --min-instances 1
ทดสอบ
ทดสอบฟังก์ชันอีกครั้ง
curl -H "Authorization: bearer $(gcloud auth print-identity-token)" -X GET $SERVICE_URL
คุณไม่ควรเห็นการหน่วงเวลา 10 วินาทีอีกต่อไปในคำขอแรก ปัญหา Cold Start สำหรับการเรียกใช้ครั้งแรก (หลังจากไม่ได้เรียกใช้เป็นเวลานาน) ได้รับการแก้ไขแล้วด้วยอินสแตนซ์ขั้นต่ำ
ดูข้อมูลเพิ่มเติมได้ที่การใช้อินสแตนซ์ขั้นต่ำ
9. ยินดีด้วย
ขอแสดงความยินดีที่ทำ Codelab นี้เสร็จสมบูรณ์
สิ่งที่เราได้พูดถึง
- ภาพรวมของฟังก์ชัน Cloud Run และวิธีใช้การอัปเดตอิมเมจพื้นฐานอัตโนมัติ
- วิธีเขียนฟังก์ชันที่ตอบสนองต่อการเรียกใช้ HTTP
- วิธีเขียนฟังก์ชันที่ตอบกลับข้อความ Pub/Sub
- วิธีเขียนฟังก์ชันที่ตอบสนองต่อเหตุการณ์ใน Cloud Storage
- วิธีแยกการรับส่งข้อมูลระหว่าง 2 รีวิชัน
- วิธีกำจัด Cold Start ด้วยอินสแตนซ์ขั้นต่ำ